@contentful/optimization-node 0.1.0-alpha14 → 0.1.0-alpha16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -49
- package/THIRD_PARTY_NOTICES.txt +2096 -0
- package/dist/260.mjs +1 -1
- package/dist/constants.cjs +1 -1
- package/dist/index.cjs +4 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -37
- package/dist/index.d.mts +7 -37
- package/dist/index.d.ts +7 -37
- package/dist/index.mjs +3 -59
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://www.contentful.com/developers/docs/personalization/">
|
|
3
|
-
<img alt="Contentful Logo" title="Contentful" src="https://raw.githubusercontent.com/contentful/optimization/v0.1.0-
|
|
3
|
+
<img alt="Contentful Logo" title="Contentful" src="https://raw.githubusercontent.com/contentful/optimization/v0.1.0-alpha16/contentful-icon.png" width="150">
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
<div align="center">
|
|
12
12
|
|
|
13
13
|
[Guides](https://contentful.github.io/optimization/documents/Documentation.Guides.html) ·
|
|
14
|
-
[Reference](https://contentful.github.io/optimization) · [Contributing](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
14
|
+
[Reference](https://contentful.github.io/optimization) · [Contributing](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/CONTRIBUTING.md)
|
|
15
15
|
|
|
16
16
|
</div>
|
|
17
17
|
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
> The Optimization SDK Suite is pre-release (alpha). Breaking changes can be published at any time.
|
|
21
21
|
|
|
22
22
|
The Optimization Node SDK implements stateless server-side optimization behavior on top of the
|
|
23
|
-
[Optimization Core SDK](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
23
|
+
[Optimization Core SDK](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/packages/universal/core-sdk/README.md). Use it for server rendering, server
|
|
24
24
|
functions, and Node services that need request-scoped profile evaluation or event emission.
|
|
25
25
|
|
|
26
26
|
If you are integrating a Node application, start with [Getting Started](#getting-started), then use
|
|
@@ -51,7 +51,7 @@ source of truth for exported API signatures.
|
|
|
51
51
|
Install using an NPM-compatible package manager, pnpm for example:
|
|
52
52
|
|
|
53
53
|
```sh
|
|
54
|
-
pnpm
|
|
54
|
+
pnpm add @contentful/optimization-node
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
Import the Optimization class; both CJS and ESM module systems are supported, ESM preferred:
|
|
@@ -66,9 +66,7 @@ Create the SDK once per module or process, then bind consent and request context
|
|
|
66
66
|
const optimization = new ContentfulOptimization({
|
|
67
67
|
clientId: 'your-client-id',
|
|
68
68
|
environment: 'main',
|
|
69
|
-
|
|
70
|
-
default: 'en-US',
|
|
71
|
-
},
|
|
69
|
+
locale: 'en-US',
|
|
72
70
|
})
|
|
73
71
|
|
|
74
72
|
function appPolicyAllowsOptimizationEvent(req: { cookies?: Record<string, string> }): boolean {
|
|
@@ -79,18 +77,20 @@ async function renderRequest(
|
|
|
79
77
|
req: { cookies?: Record<string, string>; headers: { 'accept-language'?: string } },
|
|
80
78
|
profileId: string,
|
|
81
79
|
) {
|
|
82
|
-
const
|
|
80
|
+
const appLocale = getAppLocale(req)
|
|
83
81
|
const requestOptimization = optimization.forRequest({
|
|
84
82
|
consent: {
|
|
85
83
|
events: appPolicyAllowsOptimizationEvent(req),
|
|
86
84
|
persistence: appPolicyAllowsOptimizationEvent(req),
|
|
87
85
|
},
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
locale: appLocale,
|
|
87
|
+
eventContext: { locale: appLocale },
|
|
90
88
|
profile: { id: profileId },
|
|
91
89
|
})
|
|
92
90
|
|
|
93
|
-
|
|
91
|
+
const { accepted, data } = await requestOptimization.page()
|
|
92
|
+
|
|
93
|
+
return accepted ? data : undefined
|
|
94
94
|
}
|
|
95
95
|
```
|
|
96
96
|
|
|
@@ -105,7 +105,7 @@ are part of the same application.
|
|
|
105
105
|
|
|
106
106
|
The Node SDK is stateless. It does not maintain consent, profile, or browser persistence state
|
|
107
107
|
between requests. For cross-SDK consent guidance, see
|
|
108
|
-
[Consent management in the Optimization SDK Suite](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
108
|
+
[Consent management in the Optimization SDK Suite](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/documentation/concepts/consent-management-in-the-optimization-sdk-suite.md).
|
|
109
109
|
|
|
110
110
|
| Option | Required? | Default | Description |
|
|
111
111
|
| ------------------- | --------- | ---------------------- | ----------------------------------------------------------- |
|
|
@@ -113,7 +113,7 @@ between requests. For cross-SDK consent guidance, see
|
|
|
113
113
|
| `environment` | No | `'main'` | Contentful environment identifier |
|
|
114
114
|
| `api` | No | See API options below | Experience API and Insights API endpoint options |
|
|
115
115
|
| `app` | No | `undefined` | Application metadata attached to outgoing event context |
|
|
116
|
-
| `
|
|
116
|
+
| `locale` | No | `undefined` | Default SDK Experience API and event locale |
|
|
117
117
|
| `fetchOptions` | No | SDK defaults | Fetch timeout and retry behavior |
|
|
118
118
|
| `allowedEventTypes` | No | `['identify', 'page']` | Event types allowed before request event consent is granted |
|
|
119
119
|
| `eventBuilder` | No | Node SDK defaults | Event metadata overrides for SDK-layer authors |
|
|
@@ -137,30 +137,26 @@ client:
|
|
|
137
137
|
| `plainText` | Sends performance-critical Experience API endpoints as text |
|
|
138
138
|
| `preflight` | Aggregates a new profile state without storing it |
|
|
139
139
|
|
|
140
|
+
Request-scoped Insights options belong in `insightsOptions`:
|
|
141
|
+
|
|
142
|
+
| Option | Description |
|
|
143
|
+
| -------- | ----------------------------------------------------------------- |
|
|
144
|
+
| `beacon` | Last-chance sender for serialized Insights API batches if needed. |
|
|
145
|
+
|
|
146
|
+
Use the request-scoped top-level `locale` on `forRequest()` as the promoted path for localized
|
|
147
|
+
Experience API responses and default event context. `experienceOptions.locale` remains available as
|
|
148
|
+
an advanced low-level pass-through when `locale` is not supplied. If both are provided, request
|
|
149
|
+
`locale` wins.
|
|
150
|
+
|
|
140
151
|
Common `fetchOptions` are `fetchMethod`, `requestTimeout`, `retries`, `intervalTimeout`,
|
|
141
152
|
`onFailedAttempt`, and `onRequestTimeout`. Default retries intentionally apply only to HTTP `503`
|
|
142
153
|
responses.
|
|
143
154
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const optimization = new ContentfulOptimization({
|
|
150
|
-
clientId: 'your-client-id',
|
|
151
|
-
contentfulLocales: {
|
|
152
|
-
default: 'en-US',
|
|
153
|
-
supported: ['en-US', 'de-DE', 'fr-FR'],
|
|
154
|
-
},
|
|
155
|
-
})
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Copy `contentfulLocales.default` and optional `contentfulLocales.supported` from Contentful locale
|
|
159
|
-
settings or the CMA locale list. The returned `contentfulLocale`, when present, is the configured
|
|
160
|
-
Contentful locale code to use for CDA fetches and the Experience API request option. Use
|
|
161
|
-
`eventLocale` separately in event context. Merge tags that reference localized profile fields such
|
|
162
|
-
as `location.city` and `location.country` then resolve in a language consistent with the rendered
|
|
163
|
-
content. See
|
|
155
|
+
Choose the application Contentful locale in your router, i18n layer, or request policy. Pass that
|
|
156
|
+
value directly to Contentful CDA requests, and pass the same value to
|
|
157
|
+
`forRequest({ locale: appLocale })` when Experience API responses and event context should use the
|
|
158
|
+
same language. Merge tags that reference localized profile fields such as `location.city` and
|
|
159
|
+
`location.country` then resolve in a language consistent with the rendered content. See
|
|
164
160
|
[Locale handling in the Optimization SDK Suite](https://contentful.github.io/optimization/documents/Documentation.Concepts.Locale_handling_in_the_Optimization_SDK_Suite.html)
|
|
165
161
|
for the full locale model.
|
|
166
162
|
|
|
@@ -179,23 +175,23 @@ the returned request object:
|
|
|
179
175
|
import type { Request } from 'express'
|
|
180
176
|
|
|
181
177
|
app.get('/products/:slug', async (req, res) => {
|
|
182
|
-
const
|
|
178
|
+
const appLocale = getAppLocale(req)
|
|
183
179
|
const requestOptimization = optimization.forRequest({
|
|
184
180
|
consent: {
|
|
185
181
|
events: appPolicyAllowsOptimizationEvent(req),
|
|
186
182
|
persistence: appPolicyAllowsOptimizationEvent(req),
|
|
187
183
|
},
|
|
184
|
+
locale: appLocale,
|
|
188
185
|
eventContext: {
|
|
189
|
-
locale:
|
|
186
|
+
locale: appLocale,
|
|
190
187
|
},
|
|
191
|
-
experienceOptions: contentfulLocale ? { locale: contentfulLocale } : undefined,
|
|
192
188
|
profile: { id: req.cookies.profileId },
|
|
193
189
|
})
|
|
194
|
-
const optimizationData = await requestOptimization.page({
|
|
190
|
+
const { accepted, data: optimizationData } = await requestOptimization.page({
|
|
195
191
|
properties: { path: req.path },
|
|
196
192
|
})
|
|
197
193
|
|
|
198
|
-
if (requestOptimization.canPersistProfile && optimizationData?.profile.id) {
|
|
194
|
+
if (accepted && requestOptimization.canPersistProfile && optimizationData?.profile.id) {
|
|
199
195
|
persistProfileId(res, optimizationData.profile.id)
|
|
200
196
|
}
|
|
201
197
|
|
|
@@ -209,7 +205,8 @@ request-specific consent lookup with accepted consent:
|
|
|
209
205
|
```ts
|
|
210
206
|
const requestOptimization = optimization.forRequest({
|
|
211
207
|
consent: { events: true, persistence: true },
|
|
212
|
-
|
|
208
|
+
locale: appLocale,
|
|
209
|
+
eventContext: { locale: appLocale },
|
|
213
210
|
profile,
|
|
214
211
|
})
|
|
215
212
|
```
|
|
@@ -235,13 +232,12 @@ const resolvedEntry = optimization.resolveOptimizedEntry(
|
|
|
235
232
|
)
|
|
236
233
|
```
|
|
237
234
|
|
|
238
|
-
Fetch entries with one CDA locale in the app layer. For localized apps, configure
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
direct single-locale field values. See
|
|
235
|
+
Fetch entries with one CDA locale in the app layer. For localized apps, configure your application
|
|
236
|
+
locale and pass it directly to Contentful CDA requests. Pass the same value to
|
|
237
|
+
`forRequest({ locale: appLocale })` when MergeTags that read localized profile fields should match
|
|
238
|
+
the rendered entry language. Do not pass all-locale CDA responses from `withAllLocales` or
|
|
239
|
+
`locale=*` into `resolveOptimizedEntry()`; the resolver expects direct single-locale field values.
|
|
240
|
+
See
|
|
245
241
|
[Entry personalization and variant resolution](https://contentful.github.io/optimization/documents/Documentation.Concepts.Entry_personalization_and_variant_resolution.html#single-locale-cda-entry-contract)
|
|
246
242
|
for the entry contract and
|
|
247
243
|
[Locale handling in the Optimization SDK Suite](https://contentful.github.io/optimization/documents/Documentation.Concepts.Locale_handling_in_the_Optimization_SDK_Suite.html)
|
|
@@ -288,9 +284,9 @@ package directory.
|
|
|
288
284
|
step-by-step server-side integration guide
|
|
289
285
|
- [Node SDK generated reference](https://contentful.github.io/optimization/modules/_contentful_optimization-node.html) -
|
|
290
286
|
exported API reference
|
|
291
|
-
- [Node reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
287
|
+
- [Node reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/implementations/node-sdk/README.md) - server-rendered
|
|
292
288
|
implementation using request-scoped SDK calls
|
|
293
|
-
- [Node + Web reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
289
|
+
- [Node + Web reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/implementations/node-sdk+web-sdk/README.md) -
|
|
294
290
|
hybrid SSR and browser implementation
|
|
295
|
-
- [Optimization Web SDK](https://github.com/contentful/optimization/blob/v0.1.0-
|
|
291
|
+
- [Optimization Web SDK](https://github.com/contentful/optimization/blob/v0.1.0-alpha16/packages/web/web-sdk/README.md) - browser SDK used when the same application
|
|
296
292
|
also needs client-side consent, persistence, tracking, or live updates
|