@ebitex/content-sdk 0.17.80 → 0.19.82
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/dist/content/addressing.d.ts +58 -0
- package/dist/content/addressing.d.ts.map +1 -0
- package/dist/content/addressing.js +78 -0
- package/dist/content/addressing.js.map +1 -0
- package/dist/content/client.d.ts +29 -1
- package/dist/content/client.d.ts.map +1 -1
- package/dist/content/client.js +281 -45
- package/dist/content/client.js.map +1 -1
- package/dist/content/paths.d.ts +6 -0
- package/dist/content/paths.d.ts.map +1 -1
- package/dist/content/paths.js +8 -4
- package/dist/content/paths.js.map +1 -1
- package/dist/content/persistence.d.ts +8 -0
- package/dist/content/persistence.d.ts.map +1 -1
- package/dist/content/persistence.js +2 -0
- package/dist/content/persistence.js.map +1 -1
- package/dist/content/site.d.ts +10 -0
- package/dist/content/site.d.ts.map +1 -1
- package/dist/content/site.js +13 -0
- package/dist/content/site.js.map +1 -1
- package/dist/delivery/client.d.ts +25 -3
- package/dist/delivery/client.d.ts.map +1 -1
- package/dist/delivery/client.js +5 -0
- package/dist/delivery/client.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/sitemap/xml.d.ts +27 -1
- package/dist/sitemap/xml.d.ts.map +1 -1
- package/dist/sitemap/xml.js +68 -9
- package/dist/sitemap/xml.js.map +1 -1
- package/dist/types.d.ts +81 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/content/client.js
CHANGED
|
@@ -3,12 +3,13 @@ import { normalizePath, serializeContext } from '../delivery/query.js';
|
|
|
3
3
|
import { ContentDeliveryError, ContentSdkError } from '../errors.js';
|
|
4
4
|
import { hasAdapter, isReference, isResolved, isUnresolvable } from '../guards.js';
|
|
5
5
|
import { isPreviewActivated } from '../preview/activation.js';
|
|
6
|
+
import { activeAddressing, addressKey, localeForHost, localeFromPrefix, rootLocale } from './addressing.js';
|
|
6
7
|
import { RequestCache } from './cache.js';
|
|
7
8
|
import { browserLanguages, expandLanguageCandidates, selectLocale } from './locale.js';
|
|
8
9
|
import { applyContextualOverlay } from './overlay.js';
|
|
9
10
|
import { createNavigationTree } from './navigation.js';
|
|
10
11
|
import { ContentPersistence } from './persistence.js';
|
|
11
|
-
import { currentHostname, matchSite } from './site.js';
|
|
12
|
+
import { currentHostname, currentLocation, matchSite } from './site.js';
|
|
12
13
|
import { MAX_RESOLVE_DEPTH, depthFor, strategyKey } from './strategy.js';
|
|
13
14
|
import { walkComponentValues } from './walk.js';
|
|
14
15
|
/** True only when this page is framed/opened by another window with `?ebitex-preview=1` — the guard both persistence reads and writes share (SPEC.md Business Logic §6). */
|
|
@@ -33,7 +34,11 @@ export function createContentClient(options) {
|
|
|
33
34
|
const persistOption = options.cache === false ? undefined : options.cache?.persist;
|
|
34
35
|
const persistence = persistOption ? new ContentPersistence(typeof persistOption === 'object' ? persistOption : {}) : undefined;
|
|
35
36
|
let sitePromise;
|
|
37
|
+
/** The `GET /sites` list behind `sitePromise` — kept so a site named by an override can have its addressing looked up without listing again (spec 712). */
|
|
38
|
+
let sitesPromise;
|
|
36
39
|
let localePromise;
|
|
40
|
+
/** `GET /locales`, once — shared by the browser-language selection and, under a site with a locale addressing strategy, the address-derived locale (spec 712). */
|
|
41
|
+
let treePromise;
|
|
37
42
|
/**
|
|
38
43
|
* Spec 416-sites-locales-edge-cache: `options.env` up front, else — under `cache.persist` — a
|
|
39
44
|
* previous session's own persisted identity (a repeat visitor's next full page load, without the
|
|
@@ -135,8 +140,7 @@ export function createContentClient(options) {
|
|
|
135
140
|
return Promise.resolve({ rootNodeId: options.site });
|
|
136
141
|
}
|
|
137
142
|
sitePromise ??= (async () => {
|
|
138
|
-
const sites = await
|
|
139
|
-
deliveryEnvironmentId ??= sites.find((s) => s.deliveryEnvironmentId !== undefined)?.deliveryEnvironmentId;
|
|
143
|
+
const sites = await listSitesOnce();
|
|
140
144
|
const chosen = typeof options.site === 'function' ? options.site(sites) : matchSite(sites, currentHostname());
|
|
141
145
|
if (chosen === undefined) {
|
|
142
146
|
throw new ContentSdkError('site_unresolved', sites.length === 0
|
|
@@ -148,11 +152,36 @@ export function createContentClient(options) {
|
|
|
148
152
|
return chosen;
|
|
149
153
|
})();
|
|
150
154
|
sitePromise.catch(() => {
|
|
151
|
-
|
|
155
|
+
// Never memoize a failure — including the list behind it, so a retry re-lists exactly as it
|
|
156
|
+
// did before the list was kept separately.
|
|
157
|
+
sitePromise = undefined;
|
|
158
|
+
sitesPromise = undefined;
|
|
152
159
|
});
|
|
153
160
|
return sitePromise;
|
|
154
161
|
}
|
|
155
|
-
function
|
|
162
|
+
function listSitesOnce() {
|
|
163
|
+
sitesPromise ??= delivery.listSites({ env: deliveryEnvironmentId }).then((sites) => {
|
|
164
|
+
deliveryEnvironmentId ??= sites.find((s) => s.deliveryEnvironmentId !== undefined)?.deliveryEnvironmentId;
|
|
165
|
+
return sites;
|
|
166
|
+
});
|
|
167
|
+
sitesPromise.catch(() => {
|
|
168
|
+
sitesPromise = undefined;
|
|
169
|
+
});
|
|
170
|
+
return sitesPromise;
|
|
171
|
+
}
|
|
172
|
+
function localeTree() {
|
|
173
|
+
treePromise ??= delivery.listLocales({ env: deliveryEnvironmentId });
|
|
174
|
+
treePromise.catch(() => {
|
|
175
|
+
treePromise = undefined;
|
|
176
|
+
});
|
|
177
|
+
return treePromise;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The locale an `invariant` site is asked in: the `locale` option, else the browser's languages
|
|
181
|
+
* against the tree. Exactly the pre-712 `resolveLocale`, kept separate so a caller that resolves
|
|
182
|
+
* the site alongside it (`resolveLocationImpl`) does not serialize the two (spec 367).
|
|
183
|
+
*/
|
|
184
|
+
function invariantLocale() {
|
|
156
185
|
if (typeof options.locale === 'string') {
|
|
157
186
|
return Promise.resolve(options.locale);
|
|
158
187
|
}
|
|
@@ -161,7 +190,7 @@ export function createContentClient(options) {
|
|
|
161
190
|
if (candidates !== null && candidates.length === 0) {
|
|
162
191
|
return undefined; // Node, or a browser with no languages — nothing to match, let the server default
|
|
163
192
|
}
|
|
164
|
-
const tree = await
|
|
193
|
+
const tree = await localeTree();
|
|
165
194
|
return typeof options.locale === 'function' ? options.locale(tree) : selectLocale(candidates, tree);
|
|
166
195
|
})();
|
|
167
196
|
localePromise.catch(() => {
|
|
@@ -169,6 +198,62 @@ export function createContentClient(options) {
|
|
|
169
198
|
});
|
|
170
199
|
return localePromise;
|
|
171
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Spec 712: the locale an **address** names under a site with a locale addressing strategy — its
|
|
203
|
+
* leading segment (`pathPrefix`) or its host (`localeHost`), else the tree's root locale. Never
|
|
204
|
+
* `navigator.languages`, and never the `locale` option: choosing a language for a visitor who has
|
|
205
|
+
* not asked for one is a first-visit landing, which is the consumer's plumbing, not the SDK's.
|
|
206
|
+
*/
|
|
207
|
+
async function localeOfAddress(addressing, pathname, hostname) {
|
|
208
|
+
if (addressing.strategy === 'localeHost') {
|
|
209
|
+
return localeForHost(addressing, hostname) ?? rootLocale(await localeTree());
|
|
210
|
+
}
|
|
211
|
+
return localeFromPrefix(pathname, await localeTree());
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* The client's own site and its strategy, when a site resolution is **already** under way or
|
|
215
|
+
* done — never starting one. A location-less call (`resolveReference`) must not begin listing
|
|
216
|
+
* sites an `invariant` client never listed before, or that client's requests stop being
|
|
217
|
+
* byte-identical; in normal use the page's own resolve has already started it.
|
|
218
|
+
*/
|
|
219
|
+
async function knownAddressedSite() {
|
|
220
|
+
if (sitePromise === undefined) {
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
const site = await sitePromise;
|
|
225
|
+
const addressing = activeAddressing(site);
|
|
226
|
+
return addressing === undefined ? undefined : { site: site.rootNodeId, addressing };
|
|
227
|
+
}
|
|
228
|
+
catch {
|
|
229
|
+
return undefined;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** The strategy of a site named by id (an override, an owner), from a `/sites` list already fetched — never fetching one. */
|
|
233
|
+
async function addressingOfId(rootNodeId) {
|
|
234
|
+
if (sitesPromise === undefined) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
try {
|
|
238
|
+
return activeAddressing((await sitesPromise).find((s) => s.rootNodeId === rootNodeId));
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
return undefined;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* The public `resolveLocale`: `invariantLocale` for an `invariant` site; for a site with a locale
|
|
246
|
+
* addressing strategy (spec 712), the locale the browser's current address names — which is what
|
|
247
|
+
* a component, list or stream call has to be asked in, having no address of its own.
|
|
248
|
+
*/
|
|
249
|
+
async function resolveLocale() {
|
|
250
|
+
const [scope, locale] = await Promise.all([knownAddressedSite(), invariantLocale()]);
|
|
251
|
+
if (scope === undefined) {
|
|
252
|
+
return locale;
|
|
253
|
+
}
|
|
254
|
+
const here = currentLocation();
|
|
255
|
+
return localeOfAddress(scope.addressing, here?.pathname ?? '/', here?.hostname);
|
|
256
|
+
}
|
|
172
257
|
async function supplyContext(override) {
|
|
173
258
|
if (override !== undefined) {
|
|
174
259
|
return override;
|
|
@@ -189,13 +274,13 @@ export function createContentClient(options) {
|
|
|
189
274
|
return cache.getOrCreate(key, (shared) => factory(shared), overrides.signal);
|
|
190
275
|
}
|
|
191
276
|
/** Primes one cache entry per expanded reference found anywhere in `document` (spec 394 cache seeding) — the child's own `resolveReference` key, so a later traversal there is a hit. */
|
|
192
|
-
function seedFromDocument(document, locale, ctx) {
|
|
277
|
+
function seedFromDocument(document, locale, ctx, site) {
|
|
193
278
|
if (cache === null) {
|
|
194
279
|
return;
|
|
195
280
|
}
|
|
196
281
|
for (const candidate of walkComponentValues(document)) {
|
|
197
282
|
if (isReference(candidate) && isResolved(candidate)) {
|
|
198
|
-
const key = componentKey(candidate.provider, candidate.key, locale, ctx, candidate.contextualValues);
|
|
283
|
+
const key = componentKey(candidate.provider, candidate.key, locale, ctx, candidate.contextualValues, site);
|
|
199
284
|
cache.seed(key, candidate);
|
|
200
285
|
}
|
|
201
286
|
}
|
|
@@ -221,11 +306,17 @@ export function createContentClient(options) {
|
|
|
221
306
|
*/
|
|
222
307
|
const keySuffix = referencePaths ? ['refPaths'] : [];
|
|
223
308
|
/** The shared key format `componentKey`/`peekReference` both build from — one scheme, so an in-memory hit and a persisted hit can never disagree about identity (spec 413). */
|
|
224
|
-
|
|
225
|
-
|
|
309
|
+
/**
|
|
310
|
+
* `site` is passed only under a site with a locale addressing strategy (spec 712), where the
|
|
311
|
+
* request carries `site=` too — and appended only then, so an `invariant` client's persisted keys
|
|
312
|
+
* stay byte-identical (the spec 565 emit-only-when-on discipline).
|
|
313
|
+
*/
|
|
314
|
+
function componentKeyParts(provider, key, locale, serializedCtx, contextualValues, site) {
|
|
315
|
+
const siteSuffix = site === undefined ? [] : [`@site:${site}`];
|
|
316
|
+
return JSON.stringify(['component', provider, key, locale ?? '', serializedCtx, serializeOverlay(contextualValues), ...keySuffix, ...siteSuffix]);
|
|
226
317
|
}
|
|
227
|
-
function componentKey(provider, key, locale, ctx, contextualValues) {
|
|
228
|
-
return componentKeyParts(provider, key, locale, serializeContext(ctx), contextualValues);
|
|
318
|
+
function componentKey(provider, key, locale, ctx, contextualValues, site) {
|
|
319
|
+
return componentKeyParts(provider, key, locale, serializeContext(ctx), contextualValues, site);
|
|
229
320
|
}
|
|
230
321
|
/** The shared key format `resolveLocationImpl`/`peekLocation` both build from — see `componentKeyParts`. */
|
|
231
322
|
function pathKeyParts(site, locale, serializedCtx, effectiveStrategy, path) {
|
|
@@ -243,17 +334,40 @@ export function createContentClient(options) {
|
|
|
243
334
|
* recorded. `deliveryEnvironmentId` (spec 416) rides along the same way: once known (from
|
|
244
335
|
* `options.env`, a prior session, or a live `/sites` response), every later write keeps it.
|
|
245
336
|
*/
|
|
246
|
-
function persistIdentity(site, locale, ctx) {
|
|
337
|
+
function persistIdentity(site, locale, ctx, addressing) {
|
|
247
338
|
if (persistence === undefined)
|
|
248
339
|
return;
|
|
249
340
|
const current = persistence.readIdentity();
|
|
341
|
+
// A write that names a site states that site's strategy (absent = invariant); one that names
|
|
342
|
+
// none (a reference resolve) keeps whatever the recorded site's was. Emitted only when present,
|
|
343
|
+
// so an invariant identity record is byte-identical to a pre-712 one (spec 712).
|
|
344
|
+
const recorded = site !== undefined ? addressing : current?.addressing;
|
|
250
345
|
persistence.writeIdentity({
|
|
251
346
|
site: site ?? current?.site ?? '',
|
|
252
347
|
locale: locale ?? '',
|
|
253
348
|
ctx: serializeContext(ctx),
|
|
254
349
|
deliveryEnvironmentId: deliveryEnvironmentId ?? current?.deliveryEnvironmentId,
|
|
350
|
+
...(recorded === undefined ? {} : { addressing: recorded }),
|
|
255
351
|
});
|
|
256
352
|
}
|
|
353
|
+
/** The `presentation` result both branches of `resolveLocation` build — one shape, so the two can never drift. */
|
|
354
|
+
function presentationResult(resolved, site, locale, effective) {
|
|
355
|
+
return {
|
|
356
|
+
kind: 'presentation',
|
|
357
|
+
nodeId: resolved.nodeId,
|
|
358
|
+
path: resolved.path,
|
|
359
|
+
// Spread rather than assigned, so an origin predating spec 693 leaves the members absent
|
|
360
|
+
// rather than present-and-undefined — the difference a persisted entry preserves. Spec 712's
|
|
361
|
+
// `localeSlot` follows the same rule, which is what keeps an invariant result byte-identical.
|
|
362
|
+
...(resolved.title === undefined ? {} : { title: resolved.title }),
|
|
363
|
+
...(resolved.titleSource === undefined ? {} : { titleSource: resolved.titleSource }),
|
|
364
|
+
...(resolved.localeSlot === undefined ? {} : { localeSlot: resolved.localeSlot }),
|
|
365
|
+
presentation: resolved.presentation,
|
|
366
|
+
site,
|
|
367
|
+
locale,
|
|
368
|
+
strategy: effective,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
257
371
|
async function resolveLocationImpl(location, overrides = {}) {
|
|
258
372
|
const path = normalizePath(pathnameOf(location));
|
|
259
373
|
// Site and locale resolve from independent endpoints (`/sites`, `/locales`) — neither's
|
|
@@ -262,8 +376,14 @@ export function createContentClient(options) {
|
|
|
262
376
|
// the actual content request even started).
|
|
263
377
|
const [site, locale] = await Promise.all([
|
|
264
378
|
overrides.site !== undefined ? Promise.resolve({ rootNodeId: overrides.site }) : resolveSite(),
|
|
265
|
-
overrides.locale !== undefined ? Promise.resolve(overrides.locale) :
|
|
379
|
+
overrides.locale !== undefined ? Promise.resolve(overrides.locale) : invariantLocale(),
|
|
266
380
|
]);
|
|
381
|
+
// Spec 712: a site whose addresses carry the locale is addressed by the address alone. Every
|
|
382
|
+
// line below this branch is the invariant path, unchanged.
|
|
383
|
+
const addressing = overrides.site !== undefined ? await addressingOfId(overrides.site) : activeAddressing(site);
|
|
384
|
+
if (addressing !== undefined) {
|
|
385
|
+
return resolveAddressedLocation(location, path, site, addressing, overrides);
|
|
386
|
+
}
|
|
267
387
|
const ctx = await supplyContext(overrides.context);
|
|
268
388
|
const serializedCtx = serializeContext(ctx);
|
|
269
389
|
const effective = concreteStrategy(overrides.strategy ?? strategy, locationLatchKey(site.rootNodeId, locale, serializedCtx, path));
|
|
@@ -277,19 +397,7 @@ export function createContentClient(options) {
|
|
|
277
397
|
return { kind: 'redirect', targetPath: resolved.targetPath, targetNodeId: resolved.targetNodeId ?? null };
|
|
278
398
|
}
|
|
279
399
|
seedFromDocument(resolved.presentation, locale, ctx);
|
|
280
|
-
return
|
|
281
|
-
kind: 'presentation',
|
|
282
|
-
nodeId: resolved.nodeId,
|
|
283
|
-
path: resolved.path,
|
|
284
|
-
// Spread rather than assigned, so an origin predating spec 693 leaves the members absent
|
|
285
|
-
// rather than present-and-undefined — the difference a persisted entry preserves.
|
|
286
|
-
...(resolved.title === undefined ? {} : { title: resolved.title }),
|
|
287
|
-
...(resolved.titleSource === undefined ? {} : { titleSource: resolved.titleSource }),
|
|
288
|
-
presentation: resolved.presentation,
|
|
289
|
-
site,
|
|
290
|
-
locale,
|
|
291
|
-
strategy: effective,
|
|
292
|
-
};
|
|
400
|
+
return presentationResult(resolved, site, locale, effective);
|
|
293
401
|
}
|
|
294
402
|
catch (error) {
|
|
295
403
|
if (error instanceof ContentDeliveryError && error.status === 404 && error.code === 'path_not_found') {
|
|
@@ -309,6 +417,62 @@ export function createContentClient(options) {
|
|
|
309
417
|
}
|
|
310
418
|
return result;
|
|
311
419
|
}
|
|
420
|
+
/**
|
|
421
|
+
* Spec 712 — `resolveLocation` for a site whose addresses carry the locale.
|
|
422
|
+
*
|
|
423
|
+
* The pathname goes through as it is (`/fr/cafes/x`) under `pathPrefix`, with `site=` and never
|
|
424
|
+
* `locale=`; under `localeHost` the request names the host instead of the site and the locale
|
|
425
|
+
* (`/cafes/x?host=fr.example.com`), and the server turns the host into both in one lookup. The
|
|
426
|
+
* cache key is the address, never a chosen locale, so two spellings of one page cannot split.
|
|
427
|
+
*
|
|
428
|
+
* The locale the address names is derived client-side as well — for seeding the children this
|
|
429
|
+
* resolve primes, and for the calls a page makes that have no address of their own — but the
|
|
430
|
+
* request never waits on it: the server does not need it, and the result prefers the server's own
|
|
431
|
+
* answer.
|
|
432
|
+
*/
|
|
433
|
+
async function resolveAddressedLocation(location, path, site, addressing, overrides) {
|
|
434
|
+
const hostname = hostnameOf(location) ?? currentLocation()?.hostname;
|
|
435
|
+
const slot = addressKey(addressing, hostname);
|
|
436
|
+
if (slot === undefined) {
|
|
437
|
+
throw new ContentSdkError('invalid_options', `Site ${site.rootNodeId} serves each locale from its own host, so resolving "${path}" needs the hostname the page is served under. ` +
|
|
438
|
+
'Pass a URL (or { pathname, hostname }) to resolveLocation; a server supplies it from its own request, since the SDK never reads a forwarded host.', { site: site.rootNodeId, path });
|
|
439
|
+
}
|
|
440
|
+
const addressLocale = localeOfAddress(addressing, path, hostname);
|
|
441
|
+
addressLocale.catch(() => { }); // awaited below only on a presentation; never an unhandled rejection
|
|
442
|
+
const ctx = await supplyContext(overrides.context);
|
|
443
|
+
const serializedCtx = serializeContext(ctx);
|
|
444
|
+
const effective = concreteStrategy(overrides.strategy ?? strategy, locationLatchKey(site.rootNodeId, slot, serializedCtx, path));
|
|
445
|
+
const key = pathKeyParts(site.rootNodeId, slot, serializedCtx, effective, path);
|
|
446
|
+
const result = await withCache(key, overrides, async (signal) => {
|
|
447
|
+
try {
|
|
448
|
+
const resolved = await delivery.resolvePath(path, {
|
|
449
|
+
...(addressing.strategy === 'localeHost' ? { host: hostname } : { site: site.rootNodeId }),
|
|
450
|
+
ctx,
|
|
451
|
+
resolve: pathResolveDepth(effective),
|
|
452
|
+
referencePaths,
|
|
453
|
+
signal,
|
|
454
|
+
});
|
|
455
|
+
if (resolved.kind === 'redirect') {
|
|
456
|
+
// Already in the site's own URL space — the server composed it (spec 712).
|
|
457
|
+
return { kind: 'redirect', targetPath: resolved.targetPath, targetNodeId: resolved.targetNodeId ?? null };
|
|
458
|
+
}
|
|
459
|
+
const locale = typeof resolved.locale === 'string' ? resolved.locale : await addressLocale;
|
|
460
|
+
seedFromDocument(resolved.presentation, locale, ctx, site.rootNodeId);
|
|
461
|
+
return presentationResult(resolved, site, locale, effective);
|
|
462
|
+
}
|
|
463
|
+
catch (error) {
|
|
464
|
+
if (error instanceof ContentDeliveryError && error.status === 404 && error.code === 'path_not_found') {
|
|
465
|
+
return { kind: 'notFound', path };
|
|
466
|
+
}
|
|
467
|
+
throw error;
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
if (persistence !== undefined && !previewActive() && result.kind === 'presentation') {
|
|
471
|
+
persistence.write(key, result);
|
|
472
|
+
persistIdentity(site.rootNodeId, result.locale, ctx, addressing);
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
312
476
|
/**
|
|
313
477
|
* `getComponent(provider, key, {resolve: depth})`, cached by `(provider, key, locale, ctx,
|
|
314
478
|
* depth)` alone — deliberately *not* by any edge's overlay, since this is the raw stored document,
|
|
@@ -317,22 +481,24 @@ export function createContentClient(options) {
|
|
|
317
481
|
* own edge's overlay on top of this) and `fetchOwnerDocument`'s `component`-kind owner resolution
|
|
318
482
|
* (which needs the owner's raw content, not any one edge's view of it).
|
|
319
483
|
*/
|
|
320
|
-
async function fetchRawComponent(provider, key, depth, locale, ctx, overrides) {
|
|
321
|
-
const cacheKey = JSON.stringify(['rawComponent', provider, key, locale ?? '', serializeContext(ctx), depth]);
|
|
484
|
+
async function fetchRawComponent(provider, key, depth, locale, ctx, overrides, site) {
|
|
485
|
+
const cacheKey = JSON.stringify(['rawComponent', provider, key, locale ?? '', serializeContext(ctx), depth, ...(site === undefined ? [] : [`@site:${site}`])]);
|
|
322
486
|
return withCache(cacheKey, overrides, async (signal) => {
|
|
323
|
-
|
|
324
|
-
|
|
487
|
+
// `site` only under an addressed site (spec 712): it tells the server which `paths` entries
|
|
488
|
+
// are cross-site and so carry a `url`. Sent only then, so an invariant URL is unchanged.
|
|
489
|
+
const fetched = await delivery.getComponent(provider, key, { site, locale, ctx, resolve: depth, referencePaths, signal });
|
|
490
|
+
seedFromDocument(fetched.content, locale, ctx, site);
|
|
325
491
|
return fetched;
|
|
326
492
|
});
|
|
327
493
|
}
|
|
328
494
|
/** One owner-resolution attempt: `depth` 1 first, `MAX_RESOLVE_DEPTH` ("full") on escalation — `undefined` when the owner itself resolves to nothing usable (a redirect/notFound page, most plausibly a since-unpublished owner). */
|
|
329
|
-
async function fetchOwnerDocument(owner, depth, locale, ctx, overrides) {
|
|
495
|
+
async function fetchOwnerDocument(owner, depth, locale, ctx, overrides, site) {
|
|
330
496
|
if (owner.kind === 'location') {
|
|
331
497
|
const strategyForAttempt = depth >= MAX_RESOLVE_DEPTH ? { strategy: 'full', depth } : { strategy: 'normalized', depth };
|
|
332
498
|
const result = await resolveLocationImpl({ pathname: owner.path }, { site: owner.site, locale, context: ctx, strategy: strategyForAttempt, signal: overrides.signal, fresh: overrides.fresh });
|
|
333
499
|
return result.kind === 'presentation' ? result.presentation : undefined;
|
|
334
500
|
}
|
|
335
|
-
const fetched = await fetchRawComponent(owner.provider, owner.key, depth, locale, ctx, overrides);
|
|
501
|
+
const fetched = await fetchRawComponent(owner.provider, owner.key, depth, locale, ctx, overrides, site);
|
|
336
502
|
return fetched.content;
|
|
337
503
|
}
|
|
338
504
|
/**
|
|
@@ -343,18 +509,18 @@ export function createContentClient(options) {
|
|
|
343
509
|
* the owner at `full` once — bounded, and cached the same way any other `full`-strategy resolve
|
|
344
510
|
* is, so a repeat visit or a second edge sharing the same owner doesn't escalate again.
|
|
345
511
|
*/
|
|
346
|
-
async function resolveAdaptedViaOwner(descriptor, owner, locale, ctx, overrides) {
|
|
512
|
+
async function resolveAdaptedViaOwner(descriptor, owner, locale, ctx, overrides, site) {
|
|
347
513
|
// Extraction matches on `edgeIdentity`, which already includes the edge's own overlay — a
|
|
348
514
|
// match therefore already carries server-applied overlay content for *this exact* edge, not a
|
|
349
515
|
// different one with the same target, so no further client-side overlay pass is needed here
|
|
350
516
|
// (contrast the un-adapted fetch path below, which fetches the *target* directly and applies
|
|
351
517
|
// the overlay itself).
|
|
352
|
-
const shallow = await fetchOwnerDocument(owner, 1, locale, ctx, overrides);
|
|
518
|
+
const shallow = await fetchOwnerDocument(owner, 1, locale, ctx, overrides, site);
|
|
353
519
|
const foundShallow = shallow === undefined ? undefined : extractEdge(shallow, descriptor);
|
|
354
520
|
if (foundShallow !== undefined) {
|
|
355
521
|
return foundShallow;
|
|
356
522
|
}
|
|
357
|
-
const full = await fetchOwnerDocument(owner, MAX_RESOLVE_DEPTH, locale, ctx, overrides);
|
|
523
|
+
const full = await fetchOwnerDocument(owner, MAX_RESOLVE_DEPTH, locale, ctx, overrides, site);
|
|
358
524
|
const foundFull = full === undefined ? undefined : extractEdge(full, descriptor);
|
|
359
525
|
if (foundFull !== undefined) {
|
|
360
526
|
return foundFull;
|
|
@@ -375,33 +541,38 @@ export function createContentClient(options) {
|
|
|
375
541
|
if (!isReference(descriptor) || isUnresolvable(descriptor)) {
|
|
376
542
|
throw new ContentSdkError('invalid_options', 'resolveReference needs an unexpanded reference descriptor ({ provider, key, … }).');
|
|
377
543
|
}
|
|
544
|
+
// Spec 712: under a site whose addresses carry the locale, a reference is asked in the locale the
|
|
545
|
+
// page's address names (`resolveLocale` derives it) and carries the site, so the server can mark
|
|
546
|
+
// cross-site `paths` entries. Neither applies to an invariant site, whose requests are unchanged.
|
|
547
|
+
const scope = await knownAddressedSite();
|
|
548
|
+
const site = scope?.site;
|
|
378
549
|
const locale = overrides.locale ?? (await resolveLocale());
|
|
379
550
|
const ctx = await supplyContext(overrides.context);
|
|
380
551
|
if (hasAdapter(descriptor)) {
|
|
381
552
|
if (owner === undefined) {
|
|
382
553
|
throw new ContentSdkError('adapter_requires_server_resolve', `The binding to ${descriptor.provider}:${descriptor.key} carries an Adapter, which only a resolve of its owning document can apply -- pass \`owner\`, or use the 'full' strategy (docs/content-delivery-api.md §5b).`, { provider: descriptor.provider, key: descriptor.key, adapter: descriptor.adapter });
|
|
383
554
|
}
|
|
384
|
-
const key = componentKey(descriptor.provider, descriptor.key, locale, ctx, descriptor.contextualValues);
|
|
555
|
+
const key = componentKey(descriptor.provider, descriptor.key, locale, ctx, descriptor.contextualValues, site);
|
|
385
556
|
// The factory's own `signal` argument is the *shared* cache signal (aborted only once every
|
|
386
557
|
// subscriber of this entry has gone) — pass that down, not `overrides.signal` (one caller's
|
|
387
558
|
// own signal), so nested owner-fetches inherit the same "abort only when nobody's left"
|
|
388
559
|
// semantics `withCache` gives every other call in this file. `fresh` still needs to reach the
|
|
389
560
|
// owner-fetch's own cache lookups too (an island refresh bypasses those as well), so the
|
|
390
561
|
// derived overrides carry it through unchanged.
|
|
391
|
-
const resolved = await withCache(key, overrides, (signal) => resolveAdaptedViaOwner(descriptor, owner, locale, ctx, { ...overrides, signal }));
|
|
562
|
+
const resolved = await withCache(key, overrides, (signal) => resolveAdaptedViaOwner(descriptor, owner, locale, ctx, { ...overrides, signal }, site));
|
|
392
563
|
persistReferenceResult(key, resolved, locale, ctx);
|
|
393
564
|
// The one assertion `TContent` stands for, made once here rather than at every call site
|
|
394
565
|
// (spec 576). Nothing downstream of this point knows the caller's Contract, and neither does
|
|
395
566
|
// the server response — this is expression, not validation.
|
|
396
567
|
return resolved;
|
|
397
568
|
}
|
|
398
|
-
const key = componentKey(descriptor.provider, descriptor.key, locale, ctx, descriptor.contextualValues);
|
|
569
|
+
const key = componentKey(descriptor.provider, descriptor.key, locale, ctx, descriptor.contextualValues, site);
|
|
399
570
|
// Two layers: this outer cache is keyed by the *edge* (provider+key+overlay), avoiding
|
|
400
571
|
// recomputing the overlay merge for a repeated identical edge; `fetchRawComponent`'s own inner
|
|
401
572
|
// cache is keyed by the *target* alone (no overlay), so a second edge to the same Component with
|
|
402
573
|
// different `contextualValues` shares the one underlying network fetch rather than issuing two.
|
|
403
574
|
const resolved = await withCache(key, overrides, async (signal) => {
|
|
404
|
-
const fetched = await fetchRawComponent(descriptor.provider, descriptor.key, 1, locale, ctx, { ...overrides, signal });
|
|
575
|
+
const fetched = await fetchRawComponent(descriptor.provider, descriptor.key, 1, locale, ctx, { ...overrides, signal }, site);
|
|
405
576
|
return {
|
|
406
577
|
...descriptor,
|
|
407
578
|
contract: fetched.contract ?? descriptor.contract,
|
|
@@ -427,11 +598,17 @@ export function createContentClient(options) {
|
|
|
427
598
|
if (identity === undefined)
|
|
428
599
|
return undefined;
|
|
429
600
|
const path = normalizePath(pathnameOf(location));
|
|
601
|
+
// Spec 712: under an addressed site the key's locale position holds the address instead — the
|
|
602
|
+
// one thing a peek can rebuild with no locale tree in hand.
|
|
603
|
+
const addressing = activeAddressing(identity);
|
|
604
|
+
const slot = addressing === undefined ? identity.locale : addressKey(addressing, hostnameOf(location) ?? currentLocation()?.hostname);
|
|
605
|
+
if (slot === undefined)
|
|
606
|
+
return undefined;
|
|
430
607
|
// Spec 606: named, so a peek at the location the latch already claimed predicts `full` — the key
|
|
431
608
|
// a real resolve of it would actually build — instead of the `normalized` a location-blind
|
|
432
609
|
// prediction returns once anything at all has resolved.
|
|
433
|
-
const effective = predictStrategy(strategy, locationLatchKey(identity.site,
|
|
434
|
-
const key = pathKeyParts(identity.site,
|
|
610
|
+
const effective = predictStrategy(strategy, locationLatchKey(identity.site, slot, identity.ctx, path));
|
|
611
|
+
const key = pathKeyParts(identity.site, slot, identity.ctx, effective, path);
|
|
435
612
|
const entry = persistence.read(key);
|
|
436
613
|
return entry === undefined ? undefined : { result: entry.result, storedAt: entry.storedAt };
|
|
437
614
|
}
|
|
@@ -441,7 +618,8 @@ export function createContentClient(options) {
|
|
|
441
618
|
const identity = persistence.readIdentity();
|
|
442
619
|
if (identity === undefined)
|
|
443
620
|
return undefined;
|
|
444
|
-
const
|
|
621
|
+
const site = activeAddressing(identity) === undefined ? undefined : identity.site;
|
|
622
|
+
const key = componentKeyParts(descriptor.provider, descriptor.key, identity.locale, identity.ctx, descriptor.contextualValues, site);
|
|
445
623
|
// Same assertion as `resolveReference`'s own, on the value that fetch persisted.
|
|
446
624
|
const entry = persistence.read(key);
|
|
447
625
|
return entry === undefined ? undefined : { result: entry.result, storedAt: entry.storedAt };
|
|
@@ -464,7 +642,11 @@ export function createContentClient(options) {
|
|
|
464
642
|
}
|
|
465
643
|
async function getNavigationImpl(anchor, options = {}) {
|
|
466
644
|
const site = await resolveSite();
|
|
467
|
-
const
|
|
645
|
+
const addressing = activeAddressing(site);
|
|
646
|
+
if (addressing !== undefined) {
|
|
647
|
+
return getAddressedNavigation(site, addressing, anchor, options);
|
|
648
|
+
}
|
|
649
|
+
const locale = await invariantLocale();
|
|
468
650
|
const key = navigationKeyParts(site.rootNodeId, locale, anchor, options);
|
|
469
651
|
const result = await withCache(key, options, (signal) => delivery.listNodes({
|
|
470
652
|
site: site.rootNodeId,
|
|
@@ -483,13 +665,58 @@ export function createContentClient(options) {
|
|
|
483
665
|
}
|
|
484
666
|
return createNavigationTree(result);
|
|
485
667
|
}
|
|
668
|
+
/**
|
|
669
|
+
* Spec 712 — `getNavigation` for a site whose addresses carry the locale. A **path** anchor is an
|
|
670
|
+
* address, so it is sent as one: with its prefix and `site=` under `pathPrefix`, with `host=`
|
|
671
|
+
* under `localeHost`, and never with `locale=` (the server refuses it). A node-id or absent anchor
|
|
672
|
+
* has no address to carry a locale, so it keeps `site=` + `locale=`, asked in the locale the
|
|
673
|
+
* page's own address names.
|
|
674
|
+
*/
|
|
675
|
+
async function getAddressedNavigation(site, addressing, anchor, options) {
|
|
676
|
+
const here = options.location === undefined ? currentLocation() : { pathname: pathnameOf(options.location), hostname: hostnameOf(options.location) };
|
|
677
|
+
const hostname = here?.hostname ?? currentLocation()?.hostname;
|
|
678
|
+
const byPath = anchor !== undefined && anchor.startsWith('/');
|
|
679
|
+
const locale = await localeOfAddress(addressing, byPath ? anchor : (here?.pathname ?? '/'), hostname);
|
|
680
|
+
let slot = locale;
|
|
681
|
+
if (byPath) {
|
|
682
|
+
slot = addressKey(addressing, hostname);
|
|
683
|
+
if (slot === undefined) {
|
|
684
|
+
throw new ContentSdkError('invalid_options', `Site ${site.rootNodeId} serves each locale from its own host, so navigating from "${anchor}" needs the hostname the page is served under. ` +
|
|
685
|
+
'Pass `location` (the request URL) to getNavigation; a browser supplies its own.', { site: site.rootNodeId, anchor });
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
const key = navigationKeyParts(site.rootNodeId, slot, anchor, options);
|
|
689
|
+
const result = await withCache(key, options, (signal) => delivery.listNodes({
|
|
690
|
+
...(byPath && addressing.strategy === 'localeHost' ? { host: hostname } : { site: site.rootNodeId }),
|
|
691
|
+
from: anchor,
|
|
692
|
+
include: options.include,
|
|
693
|
+
maxDepth: options.maxDepth,
|
|
694
|
+
locale: byPath ? undefined : locale,
|
|
695
|
+
signal,
|
|
696
|
+
}));
|
|
697
|
+
if (persistence !== undefined && !previewActive()) {
|
|
698
|
+
persistence.write(key, result);
|
|
699
|
+
persistIdentity(site.rootNodeId, locale, {}, addressing);
|
|
700
|
+
}
|
|
701
|
+
return createNavigationTree(result);
|
|
702
|
+
}
|
|
486
703
|
function peekTree(anchor, options = {}) {
|
|
487
704
|
if (persistence === undefined || previewActive())
|
|
488
705
|
return undefined;
|
|
489
706
|
const identity = persistence.readIdentity();
|
|
490
707
|
if (identity === undefined)
|
|
491
708
|
return undefined;
|
|
492
|
-
|
|
709
|
+
// Spec 712: a path anchor under an addressed site is keyed by its address, exactly as the fetch
|
|
710
|
+
// keyed it; a node-id anchor by the recorded locale, as ever.
|
|
711
|
+
const addressing = activeAddressing(identity);
|
|
712
|
+
let slot = identity.locale;
|
|
713
|
+
if (addressing !== undefined && anchor !== undefined && anchor.startsWith('/')) {
|
|
714
|
+
const hostname = (options.location === undefined ? undefined : hostnameOf(options.location)) ?? currentLocation()?.hostname;
|
|
715
|
+
slot = addressKey(addressing, hostname);
|
|
716
|
+
if (slot === undefined)
|
|
717
|
+
return undefined;
|
|
718
|
+
}
|
|
719
|
+
const entry = persistence.read(navigationKeyParts(identity.site, slot, anchor, options));
|
|
493
720
|
return entry === undefined ? undefined : createNavigationTree(entry.result);
|
|
494
721
|
}
|
|
495
722
|
return {
|
|
@@ -508,7 +735,9 @@ export function createContentClient(options) {
|
|
|
508
735
|
cache?.invalidate();
|
|
509
736
|
persistence?.clear();
|
|
510
737
|
sitePromise = undefined;
|
|
738
|
+
sitesPromise = undefined;
|
|
511
739
|
localePromise = undefined;
|
|
740
|
+
treePromise = undefined;
|
|
512
741
|
},
|
|
513
742
|
};
|
|
514
743
|
}
|
|
@@ -522,6 +751,13 @@ function pathnameOf(location) {
|
|
|
522
751
|
}
|
|
523
752
|
return location.pathname;
|
|
524
753
|
}
|
|
754
|
+
/** The hostname a location names, when it names one: a full URL, a `URL`, or `{ pathname, hostname }`. A bare path names none. */
|
|
755
|
+
function hostnameOf(location) {
|
|
756
|
+
if (typeof location === 'string') {
|
|
757
|
+
return /^[a-z][a-z0-9+.-]*:\/\//i.test(location) ? new URL(location).hostname || undefined : undefined;
|
|
758
|
+
}
|
|
759
|
+
return typeof location.hostname === 'string' && location.hostname ? location.hostname : undefined;
|
|
760
|
+
}
|
|
525
761
|
function serializeOverlay(contextualValues) {
|
|
526
762
|
if (!contextualValues) {
|
|
527
763
|
return '';
|