@contentful/optimization-node 0.1.0-alpha14 → 0.1.0-alpha15

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 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-alpha14/contentful-icon.png" width="150">
3
+ <img alt="Contentful Logo" title="Contentful" src="https://raw.githubusercontent.com/contentful/optimization/v0.1.0-alpha15/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-alpha14/CONTRIBUTING.md)
14
+ [Reference](https://contentful.github.io/optimization) · [Contributing](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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-alpha14/packages/universal/core-sdk/README.md). Use it for server rendering, server
23
+ [Optimization Core SDK](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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
@@ -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
- contentfulLocales: {
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 { contentfulLocale, eventLocale } = optimization.resolveRequestLocale(req)
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
- eventContext: { locale: eventLocale },
89
- experienceOptions: contentfulLocale ? { locale: contentfulLocale } : undefined,
86
+ locale: appLocale,
87
+ eventContext: { locale: appLocale },
90
88
  profile: { id: profileId },
91
89
  })
92
90
 
93
- return await requestOptimization.page()
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-alpha14/documentation/concepts/consent-management-in-the-optimization-sdk-suite.md).
108
+ [Consent management in the Optimization SDK Suite](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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
- | `contentfulLocales` | No | `undefined` | Contentful locale codes used by `resolveRequestLocale()` |
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
+ | `beaconHandler` | Custom handler for enqueueing Insights API batches. |
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
- Use `contentfulLocales.default` for single-locale apps. For apps that match request locale input to
145
- multiple Contentful locales, keep `default` as the fallback and list the supported Contentful locale
146
- codes:
147
-
148
- ```ts
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 { contentfulLocale, eventLocale } = optimization.resolveRequestLocale(req)
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: eventLocale,
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
- eventContext: { locale: eventLocale },
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
- `contentfulLocales`, then use the `contentfulLocale` returned by `resolveRequestLocale()` for CDA
240
- fetches and the per-call `{ locale }` request option so MergeTags that read localized profile fields
241
- match the entry language. When `contentfulLocale` is absent because no `contentfulLocales` config is
242
- present, omit the CDA and Experience API locale options intentionally. Do not pass all-locale CDA
243
- responses from `withAllLocales` or `locale=*` into `resolveOptimizedEntry()`; the resolver expects
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-alpha14/implementations/node-sdk/README.md) - server-rendered
287
+ - [Node reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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-alpha14/implementations/node-sdk+web-sdk/README.md) -
289
+ - [Node + Web reference implementation](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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-alpha14/packages/web/web-sdk/README.md) - browser SDK used when the same application
291
+ - [Optimization Web SDK](https://github.com/contentful/optimization/blob/v0.1.0-alpha15/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