@decocms/blocks 7.72.0 → 8.0.0-next.0
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/bin/deco.js +13 -0
- package/package.json +32 -3
- package/src/cms/schema.ts +1 -1
- package/src/hooks/LiveControls.tsx +2 -0
- package/src/hooks/useLoadMore.ts +2 -0
- package/src/index.ts +37 -0
- package/src/protocol/assets.ts +124 -0
- package/src/protocol/canonical.ts +7 -0
- package/src/protocol/ciphertext.ts +6 -0
- package/src/protocol/client.ts +170 -0
- package/src/protocol/conformance/cases/assets.ts +81 -0
- package/src/protocol/conformance/cases/guards.ts +314 -0
- package/src/protocol/conformance/cases/reads.ts +168 -0
- package/src/protocol/conformance/cases/types.ts +9 -0
- package/src/protocol/conformance/cases/wire.ts +138 -0
- package/src/protocol/conformance/cases/writes.ts +345 -0
- package/src/protocol/conformance/contentHashFixtures.ts +79 -0
- package/src/protocol/conformance/context.ts +222 -0
- package/src/protocol/conformance/index.ts +140 -0
- package/src/protocol/errors.ts +142 -0
- package/src/protocol/index.ts +39 -0
- package/src/protocol/keys.ts +279 -0
- package/src/protocol/params.ts +67 -0
- package/src/protocol/secrets.ts +231 -0
- package/src/protocol/server/assets.ts +119 -0
- package/src/protocol/server/auth.ts +49 -0
- package/src/protocol/server/bodyCache.ts +84 -0
- package/src/protocol/server/content.ts +175 -0
- package/src/protocol/server/core.ts +112 -0
- package/src/protocol/server/handler.ts +102 -0
- package/src/protocol/server/http.ts +139 -0
- package/src/protocol/server/index.ts +11 -0
- package/src/protocol/server/methods/apply.ts +341 -0
- package/src/protocol/server/methods/read.ts +156 -0
- package/src/protocol/server/rpc.ts +136 -0
- package/src/protocol/storage/fs/hash.ts +21 -0
- package/src/protocol/storage/fs/index.ts +338 -0
- package/src/protocol/storage/fs/lock.ts +68 -0
- package/src/protocol/storage/fs/transaction.ts +183 -0
- package/src/protocol/storage/fs/unsupported.ts +15 -0
- package/src/protocol/storage/memory.ts +238 -0
- package/src/protocol/storage.ts +182 -0
- package/src/protocol/types.ts +172 -0
- package/src/v8/analytics.ts +50 -0
- package/src/v8/background.ts +107 -0
- package/src/v8/builtins/data.ts +23 -0
- package/src/v8/builtins/index.ts +26 -0
- package/src/v8/builtins/lazy.ts +23 -0
- package/src/v8/builtins/legacy.ts +65 -0
- package/src/v8/builtins/matchers.ts +31 -0
- package/src/v8/builtins/multivariate.ts +25 -0
- package/src/v8/builtins/secret.ts +82 -0
- package/src/v8/canonical.ts +117 -0
- package/src/v8/ciphertext.ts +158 -0
- package/src/v8/cli/builtins.ts +20 -0
- package/src/v8/cli/check/index.ts +774 -0
- package/src/v8/cli/check/messages.ts +168 -0
- package/src/v8/cli/content.ts +287 -0
- package/src/v8/cli/index.ts +17 -0
- package/src/v8/cli/log.ts +18 -0
- package/src/v8/cli/main.ts +12 -0
- package/src/v8/cli/root.ts +66 -0
- package/src/v8/cli/run.ts +236 -0
- package/src/v8/cli/schema/builtinSchemas.ts +236 -0
- package/src/v8/cli/schema/generate.ts +507 -0
- package/src/v8/cli/schema/index.ts +63 -0
- package/src/v8/cli/schema/tsProgram.ts +497 -0
- package/src/v8/cli/schema/typeToSchema.ts +689 -0
- package/src/v8/cli/serve/server.ts +303 -0
- package/src/v8/cli/watch.ts +86 -0
- package/src/v8/client.ts +147 -0
- package/src/v8/cms.ts +237 -0
- package/src/v8/content.ts +195 -0
- package/src/v8/draft.ts +136 -0
- package/src/v8/errors.ts +59 -0
- package/src/v8/fetch.ts +152 -0
- package/src/v8/identity.ts +65 -0
- package/src/v8/index.ts +39 -0
- package/src/v8/json.ts +74 -0
- package/src/v8/matchRoute.ts +204 -0
- package/src/v8/remoteLoader.ts +282 -0
- package/src/v8/resolver.ts +278 -0
- package/src/v8/secrets.ts +17 -0
- package/src/v8/telemetry.ts +533 -0
- package/src/v8/types.ts +240 -0
- package/src/cms/blockSource.test.ts +0 -76
- package/src/cms/client.browserBundle.test.ts +0 -44
- package/src/cms/deferredTrigger.test.ts +0 -32
- package/src/cms/draftSource.test.ts +0 -519
- package/src/cms/layoutCacheRace.test.ts +0 -181
- package/src/cms/loadDecofileDirectory.test.ts +0 -109
- package/src/cms/loader.test.ts +0 -557
- package/src/cms/registry.test.ts +0 -110
- package/src/cms/renderJson.test.ts +0 -167
- package/src/cms/resolve.test.ts +0 -1492
- package/src/cms/schema.test.ts +0 -356
- package/src/cms/sectionLoaderContext.test.ts +0 -111
- package/src/cms/sectionLoaders.test.ts +0 -625
- package/src/cms/sectionMixins.test.ts +0 -167
- package/src/cms/stickyFlags.test.ts +0 -117
- package/src/flags/flags.test.ts +0 -148
- package/src/hooks/Image.test.ts +0 -239
- package/src/hooks/RenderSection.test.tsx +0 -81
- package/src/hooks/Stats.test.ts +0 -112
- package/src/hooks/useLoadMore.test.ts +0 -205
- package/src/matchers/builtins.test.ts +0 -318
- package/src/matchers/override.test.ts +0 -205
- package/src/middleware/hydrationContext.test.ts +0 -61
- package/src/middleware/observability.test.ts +0 -335
- package/src/middleware/validateSection.test.ts +0 -147
- package/src/preview/DraftPreviewBadge.render.test.tsx +0 -67
- package/src/preview/DraftPreviewBadge.test.ts +0 -55
- package/src/sdk/abTesting.test.ts +0 -381
- package/src/sdk/cacheHeaders.test.ts +0 -258
- package/src/sdk/cacheStorage.test.ts +0 -242
- package/src/sdk/cachedLoader.test.ts +0 -122
- package/src/sdk/cachedLoaderMaxBytes.test.ts +0 -23
- package/src/sdk/cachedLoaderModule.test.ts +0 -209
- package/src/sdk/cachedLoaderSizeMetric.test.ts +0 -93
- package/src/sdk/cn.test.ts +0 -34
- package/src/sdk/composite.test.ts +0 -121
- package/src/sdk/cookie.test.ts +0 -108
- package/src/sdk/crypto.test.ts +0 -65
- package/src/sdk/deepOmit.test.ts +0 -45
- package/src/sdk/encoding.test.ts +0 -71
- package/src/sdk/env.test.ts +0 -33
- package/src/sdk/experiments.test.ts +0 -328
- package/src/sdk/fetchCache.test.ts +0 -164
- package/src/sdk/flags.test.ts +0 -91
- package/src/sdk/http.test.ts +0 -71
- package/src/sdk/inflightTimeout.test.ts +0 -35
- package/src/sdk/instrumentedFetch.test.ts +0 -559
- package/src/sdk/invoke.test.ts +0 -115
- package/src/sdk/logger.test.ts +0 -432
- package/src/sdk/otel.test.ts +0 -691
- package/src/sdk/otelAdapters.test.ts +0 -89
- package/src/sdk/otelHttpLog.test.ts +0 -457
- package/src/sdk/otelHttpMeter.test.ts +0 -395
- package/src/sdk/otelHttpTracer.test.ts +0 -474
- package/src/sdk/replaceStream.test.ts +0 -117
- package/src/sdk/requestContextStorage.browser.test.ts +0 -29
- package/src/sdk/splitExactRedirects.test.ts +0 -154
- package/src/sdk/urlRedaction.test.ts +0 -73
- package/src/sdk/urlUtils.test.ts +0 -34
- package/src/sdk/useDevice.test.ts +0 -130
- package/src/sdk/useScript.test.ts +0 -128
- package/src/sdk/useSuggestions.test.ts +0 -230
- package/tsconfig.json +0 -7
package/src/cms/resolve.test.ts
DELETED
|
@@ -1,1492 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
|
|
3
|
-
// Mock dependencies before importing the module under test
|
|
4
|
-
vi.mock("./sectionLoaders", () => ({
|
|
5
|
-
isLayoutSection: () => false,
|
|
6
|
-
runSingleSectionLoader: vi.fn(async (section: any) => section),
|
|
7
|
-
}));
|
|
8
|
-
|
|
9
|
-
vi.mock("../sdk/normalizeUrls", () => ({
|
|
10
|
-
normalizeUrlsInObject: vi.fn(<T>(x: T) => x),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
vi.mock("./loader", () => ({
|
|
14
|
-
findPageByPath: vi.fn(),
|
|
15
|
-
loadBlocks: vi.fn(() => ({})),
|
|
16
|
-
}));
|
|
17
|
-
|
|
18
|
-
vi.mock("./registry", () => ({
|
|
19
|
-
getSection: vi.fn(),
|
|
20
|
-
getOnBeforeResolveProps: vi.fn(),
|
|
21
|
-
}));
|
|
22
|
-
|
|
23
|
-
import { normalizeUrlsInObject } from "../sdk/normalizeUrls";
|
|
24
|
-
import { findPageByPath } from "./loader";
|
|
25
|
-
import { getSection } from "./registry";
|
|
26
|
-
import type { AsyncRenderingConfig, DeferredSection, MatcherContext } from "./resolve";
|
|
27
|
-
import {
|
|
28
|
-
asResolved,
|
|
29
|
-
clearCommerceLoaders,
|
|
30
|
-
layoutCacheKey,
|
|
31
|
-
DEFAULT_FOLD_THRESHOLD,
|
|
32
|
-
extractSeoFromProps,
|
|
33
|
-
getAsyncRenderingConfig,
|
|
34
|
-
isDeferred,
|
|
35
|
-
isEagerRequest,
|
|
36
|
-
reExtractRawProps,
|
|
37
|
-
registerCommerceLoader,
|
|
38
|
-
registerMatcher,
|
|
39
|
-
resolveDeferred,
|
|
40
|
-
registerEagerSections,
|
|
41
|
-
registerAlwaysDeferSections,
|
|
42
|
-
registerNeverDeferSections,
|
|
43
|
-
resolveDecoPage,
|
|
44
|
-
resolveDeferredSectionFull,
|
|
45
|
-
resolvePageSeoBlock,
|
|
46
|
-
resolveSectionsList,
|
|
47
|
-
resolveValue,
|
|
48
|
-
setAsyncRenderingConfig,
|
|
49
|
-
shouldDeferSection,
|
|
50
|
-
WELL_KNOWN_TYPES,
|
|
51
|
-
} from "./resolve";
|
|
52
|
-
import { runSingleSectionLoader } from "./sectionLoaders";
|
|
53
|
-
|
|
54
|
-
describe("resolveDeferredSectionFull", () => {
|
|
55
|
-
it("resolves a deferred section and preserves index", async () => {
|
|
56
|
-
const ds: DeferredSection = {
|
|
57
|
-
component: "site/sections/ProductShelf.tsx",
|
|
58
|
-
key: "site/sections/ProductShelf.tsx",
|
|
59
|
-
index: 5,
|
|
60
|
-
propsHash: "test",
|
|
61
|
-
rawProps: { title: "Best Sellers" },
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const request = new Request("https://store.com/");
|
|
65
|
-
|
|
66
|
-
// resolveDeferredSection depends on ensureInitialized() and CMS internals.
|
|
67
|
-
// Since we can't easily mock the full resolution pipeline, we test that
|
|
68
|
-
// the function composes correctly by verifying it calls the right deps.
|
|
69
|
-
// A full integration test would require a running CMS context.
|
|
70
|
-
|
|
71
|
-
// For now, verify the function signature is correct and types align
|
|
72
|
-
expect(typeof resolveDeferredSectionFull).toBe("function");
|
|
73
|
-
expect(resolveDeferredSectionFull.length).toBe(4); // ds, pagePath, request, matcherCtx?
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("runSingleSectionLoader is called with enriched section", async () => {
|
|
77
|
-
// Verify the mock is correctly set up
|
|
78
|
-
const mockSection = {
|
|
79
|
-
component: "test",
|
|
80
|
-
props: { title: "hi" },
|
|
81
|
-
key: "test",
|
|
82
|
-
index: 3,
|
|
83
|
-
};
|
|
84
|
-
const request = new Request("https://store.com/");
|
|
85
|
-
|
|
86
|
-
const result = await (runSingleSectionLoader as any)(mockSection, request);
|
|
87
|
-
expect(result).toEqual(mockSection);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("normalizeUrlsInObject is used for output normalization", () => {
|
|
91
|
-
const input = { url: "https://store.com/image.jpg" };
|
|
92
|
-
const result = (normalizeUrlsInObject as any)(input);
|
|
93
|
-
expect(result).toEqual(input); // mock passes through
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
// ---------------------------------------------------------------------------
|
|
98
|
-
// resolveSectionsList — page-level variant wrapper support
|
|
99
|
-
// ---------------------------------------------------------------------------
|
|
100
|
-
|
|
101
|
-
describe("resolveSectionsList", () => {
|
|
102
|
-
const makeRctx = (matcherCtx = {}) => ({
|
|
103
|
-
routeParams: {},
|
|
104
|
-
matcherCtx,
|
|
105
|
-
memo: new Map(),
|
|
106
|
-
depth: 0,
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("returns array as-is when value is already an array", async () => {
|
|
110
|
-
const sections = [{ __resolveType: "section-A" }, { __resolveType: "section-B" }];
|
|
111
|
-
const result = await resolveSectionsList(sections, makeRctx());
|
|
112
|
-
expect(result).toEqual(sections);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it("returns empty array for null/undefined/non-object", async () => {
|
|
116
|
-
expect(await resolveSectionsList(null, makeRctx())).toEqual([]);
|
|
117
|
-
expect(await resolveSectionsList(undefined, makeRctx())).toEqual([]);
|
|
118
|
-
expect(await resolveSectionsList("string", makeRctx())).toEqual([]);
|
|
119
|
-
expect(await resolveSectionsList(42, makeRctx())).toEqual([]);
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it("resolves page-level variant wrapper without __resolveType", async () => {
|
|
123
|
-
// Simulates CMS admin wrapping all sections in a device variant
|
|
124
|
-
// Rule has no __resolveType → evaluateMatcher returns true (match-all)
|
|
125
|
-
const sectionsArray = [
|
|
126
|
-
{ __resolveType: "Header - 01" },
|
|
127
|
-
{ __resolveType: "site/sections/Account/PersonalData.tsx" },
|
|
128
|
-
{ __resolveType: "Footer - 01" },
|
|
129
|
-
];
|
|
130
|
-
|
|
131
|
-
const variantWrapper = {
|
|
132
|
-
variants: [
|
|
133
|
-
{
|
|
134
|
-
rule: { mobile: true, tablet: true, desktop: true },
|
|
135
|
-
value: sectionsArray,
|
|
136
|
-
},
|
|
137
|
-
],
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const result = await resolveSectionsList(variantWrapper, makeRctx());
|
|
141
|
-
expect(result).toEqual(sectionsArray);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("returns empty when no variant matches in page-level wrapper", async () => {
|
|
145
|
-
// All variants have __resolveType in rule → evaluateMatcher returns false
|
|
146
|
-
// (unregistered matcher defaults to false)
|
|
147
|
-
const variantWrapper = {
|
|
148
|
-
variants: [
|
|
149
|
-
{
|
|
150
|
-
rule: { __resolveType: "website/matchers/device.ts", mobile: true },
|
|
151
|
-
value: [{ __resolveType: "MobileOnly" }],
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
const result = await resolveSectionsList(variantWrapper, makeRctx());
|
|
157
|
-
expect(result).toEqual([]);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("picks first matching variant in page-level wrapper", async () => {
|
|
161
|
-
const desktopSections = [{ __resolveType: "DesktopLayout" }];
|
|
162
|
-
const mobileSections = [{ __resolveType: "MobileLayout" }];
|
|
163
|
-
|
|
164
|
-
const variantWrapper = {
|
|
165
|
-
variants: [
|
|
166
|
-
{
|
|
167
|
-
// No __resolveType → evaluateMatcher returns true (first match wins)
|
|
168
|
-
rule: { desktop: true },
|
|
169
|
-
value: desktopSections,
|
|
170
|
-
},
|
|
171
|
-
{
|
|
172
|
-
rule: { mobile: true },
|
|
173
|
-
value: mobileSections,
|
|
174
|
-
},
|
|
175
|
-
],
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
const result = await resolveSectionsList(variantWrapper, makeRctx());
|
|
179
|
-
expect(result).toEqual(desktopSections);
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
it("returns empty for object without __resolveType and without variants", async () => {
|
|
183
|
-
const result = await resolveSectionsList({ someKey: "value" }, makeRctx());
|
|
184
|
-
expect(result).toEqual([]);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it("respects max depth limit (20)", async () => {
|
|
188
|
-
// Build 21 levels of nested variant wrappers to exceed MAX_RESOLVE_DEPTH=20
|
|
189
|
-
let wrapper: any = [{ __resolveType: "deep" }];
|
|
190
|
-
for (let i = 0; i < 21; i++) {
|
|
191
|
-
wrapper = { variants: [{ rule: {}, value: wrapper }] };
|
|
192
|
-
}
|
|
193
|
-
const result = await resolveSectionsList(wrapper, makeRctx());
|
|
194
|
-
expect(result).toEqual([]);
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
// ---------------------------------------------------------------------------
|
|
199
|
-
// Commerce loader auto-injects URL query params as top-level props
|
|
200
|
-
// ---------------------------------------------------------------------------
|
|
201
|
-
//
|
|
202
|
-
// Regression guard for Google Shopping deep links (and any direct entry
|
|
203
|
-
// with `?skuId=…`, `?q=…`, etc.): the apps-start canonical commerce
|
|
204
|
-
// loaders read `props.skuId` to pre-select a variant. The framework
|
|
205
|
-
// injects URL search params into `resolvedProps` at the commerce-loader
|
|
206
|
-
// call site so loaders see them on direct navigation. CMS-configured
|
|
207
|
-
// props win over URL params (URL is a fallback, not an override).
|
|
208
|
-
|
|
209
|
-
describe("commerce loader auto-injects URL search params as props", () => {
|
|
210
|
-
const KEY = "site/loaders/__test/queryInjectLoader";
|
|
211
|
-
|
|
212
|
-
beforeEach(() => {
|
|
213
|
-
clearCommerceLoaders();
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
afterEach(() => {
|
|
217
|
-
clearCommerceLoaders();
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
it("populates props.skuId from ?skuId= when CMS does not set it", async () => {
|
|
221
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
222
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
223
|
-
calls.push({ ...props });
|
|
224
|
-
return null;
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
await resolveValue({ __resolveType: KEY, slug: "sabonete" }, undefined, {
|
|
228
|
-
url: "https://store.com/produto/sabonete/p?skuId=12345&size=M",
|
|
229
|
-
path: "/produto/sabonete/p",
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
expect(calls).toHaveLength(1);
|
|
233
|
-
expect(calls[0]).toMatchObject({
|
|
234
|
-
slug: "sabonete",
|
|
235
|
-
skuId: "12345",
|
|
236
|
-
size: "M",
|
|
237
|
-
__pagePath: "/produto/sabonete/p",
|
|
238
|
-
__pageUrl: "https://store.com/produto/sabonete/p?skuId=12345&size=M",
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it("strips tracking params from __pageUrl and never injects them as props", async () => {
|
|
243
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
244
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
245
|
-
calls.push({ ...props });
|
|
246
|
-
return null;
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
await resolveValue({ __resolveType: KEY, slug: "sabonete" }, undefined, {
|
|
250
|
-
url: "https://store.com/produto/sabonete/p?skuId=12345&utm_source=google&gclid=abc&srsltid=xyz",
|
|
251
|
-
path: "/produto/sabonete/p",
|
|
252
|
-
});
|
|
253
|
-
|
|
254
|
-
expect(calls[0]).toMatchObject({
|
|
255
|
-
slug: "sabonete",
|
|
256
|
-
skuId: "12345",
|
|
257
|
-
__pageUrl: "https://store.com/produto/sabonete/p?skuId=12345",
|
|
258
|
-
});
|
|
259
|
-
expect(calls[0]).not.toHaveProperty("utm_source");
|
|
260
|
-
expect(calls[0]).not.toHaveProperty("gclid");
|
|
261
|
-
expect(calls[0]).not.toHaveProperty("srsltid");
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
// The point of the strip: `createCachedLoader`'s default keyFn is
|
|
265
|
-
// JSON.stringify(props), so identical props mean a shared cache entry. A
|
|
266
|
-
// campaign tail used to mint a fresh one on every paid landing.
|
|
267
|
-
it("gives a paid-traffic landing the same props as the clean URL", async () => {
|
|
268
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
269
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
270
|
-
calls.push({ ...props });
|
|
271
|
-
return null;
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
const at = (url: string) =>
|
|
275
|
-
resolveValue({ __resolveType: KEY, slug: "sabonete" }, undefined, {
|
|
276
|
-
url,
|
|
277
|
-
path: "/produto/sabonete/p",
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
await at("https://store.com/produto/sabonete/p");
|
|
281
|
-
await at("https://store.com/produto/sabonete/p?utm_source=google&gclid=abc&fbclid=d");
|
|
282
|
-
|
|
283
|
-
expect(JSON.stringify(calls[1])).toBe(JSON.stringify(calls[0]));
|
|
284
|
-
});
|
|
285
|
-
|
|
286
|
-
it("does NOT override a CMS-configured prop with a URL param of the same name", async () => {
|
|
287
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
288
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
289
|
-
calls.push({ ...props });
|
|
290
|
-
return null;
|
|
291
|
-
});
|
|
292
|
-
|
|
293
|
-
await resolveValue({ __resolveType: KEY, skuId: "cms-locked-sku" }, undefined, {
|
|
294
|
-
url: "https://store.com/p?skuId=url-value",
|
|
295
|
-
path: "/p",
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
expect(calls[0]?.skuId).toBe("cms-locked-sku");
|
|
299
|
-
});
|
|
300
|
-
|
|
301
|
-
it("decodes URL-encoded values", async () => {
|
|
302
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
303
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
304
|
-
calls.push({ ...props });
|
|
305
|
-
return null;
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
await resolveValue({ __resolveType: KEY }, undefined, {
|
|
309
|
-
url: "https://store.com/?q=preto%20azul",
|
|
310
|
-
path: "/",
|
|
311
|
-
});
|
|
312
|
-
|
|
313
|
-
expect(calls[0]?.q).toBe("preto azul");
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
it("is a no-op when matcherCtx.url is missing", async () => {
|
|
317
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
318
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
319
|
-
calls.push({ ...props });
|
|
320
|
-
return null;
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
await resolveValue({ __resolveType: KEY, slug: "abc" }, undefined, {});
|
|
324
|
-
|
|
325
|
-
expect(calls[0]).toEqual({ slug: "abc" });
|
|
326
|
-
expect(calls[0]?.__pageUrl).toBeUndefined();
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
it("warns and skips injection when matcherCtx.url is malformed", async () => {
|
|
330
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
331
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
332
|
-
calls.push({ ...props });
|
|
333
|
-
return null;
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
337
|
-
try {
|
|
338
|
-
await expect(
|
|
339
|
-
resolveValue({ __resolveType: KEY, slug: "abc" }, undefined, {
|
|
340
|
-
url: "not a url",
|
|
341
|
-
path: "/",
|
|
342
|
-
}),
|
|
343
|
-
).resolves.not.toThrow();
|
|
344
|
-
|
|
345
|
-
// Loader still ran with __pageUrl set, but no query params were
|
|
346
|
-
// injected and the warning surfaced the upstream bug.
|
|
347
|
-
expect(calls[0]).toMatchObject({ slug: "abc", __pageUrl: "not a url" });
|
|
348
|
-
expect(Object.keys(calls[0] ?? {}).sort()).toEqual(
|
|
349
|
-
["__pagePath", "__pageUrl", "slug"].sort(),
|
|
350
|
-
);
|
|
351
|
-
const warnings = warnSpy.mock.calls.map((c) => String(c[0]));
|
|
352
|
-
expect(warnings.some((w) => w.includes("malformed matcherCtx.url"))).toBe(true);
|
|
353
|
-
expect(warnings.some((w) => w.includes(KEY))).toBe(true);
|
|
354
|
-
} finally {
|
|
355
|
-
warnSpy.mockRestore();
|
|
356
|
-
}
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
it("never auto-injects `page` — loaders that read __pageUrl apply their own index-base conversion (#391)", async () => {
|
|
360
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
361
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
362
|
-
calls.push({ ...props });
|
|
363
|
-
return null;
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
await resolveValue({ __resolveType: KEY }, undefined, {
|
|
367
|
-
url: "https://store.com/?page=3",
|
|
368
|
-
path: "/",
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
expect(calls[0]?.page).toBeUndefined();
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
it("coerces `count`/`pageOffset` to numbers instead of injecting raw strings", async () => {
|
|
375
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
376
|
-
registerCommerceLoader(KEY, async (props: Record<string, unknown>) => {
|
|
377
|
-
calls.push({ ...props });
|
|
378
|
-
return null;
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
await resolveValue({ __resolveType: KEY }, undefined, {
|
|
382
|
-
url: "https://store.com/?count=24&pageOffset=2",
|
|
383
|
-
path: "/",
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
expect(calls[0]?.count).toBe(24);
|
|
387
|
-
expect(calls[0]?.pageOffset).toBe(2);
|
|
388
|
-
});
|
|
389
|
-
});
|
|
390
|
-
|
|
391
|
-
describe("commerce loader resolves legacy .ts-suffixed resolveType", () => {
|
|
392
|
-
beforeEach(() => clearCommerceLoaders());
|
|
393
|
-
afterEach(() => clearCommerceLoaders());
|
|
394
|
-
|
|
395
|
-
it("matches a manifest key (no extension) against a decofile .ts resolveType", async () => {
|
|
396
|
-
const calls: Array<Record<string, unknown>> = [];
|
|
397
|
-
// Split-package manifests register WITHOUT the file extension.
|
|
398
|
-
registerCommerceLoader("shopify/loaders/ProductDetailsPage", async (props) => {
|
|
399
|
-
calls.push({ ...props });
|
|
400
|
-
return null;
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
// Legacy (Fresh/Deno) decofile references it WITH the .ts extension.
|
|
404
|
-
await resolveValue(
|
|
405
|
-
{ __resolveType: "shopify/loaders/ProductDetailsPage.ts", slug: "oversize-t-shirt-123" },
|
|
406
|
-
undefined,
|
|
407
|
-
{
|
|
408
|
-
url: "https://store.com/products/oversize-t-shirt-123",
|
|
409
|
-
path: "/products/oversize-t-shirt-123",
|
|
410
|
-
},
|
|
411
|
-
);
|
|
412
|
-
|
|
413
|
-
expect(calls).toHaveLength(1);
|
|
414
|
-
expect(calls[0]).toMatchObject({ slug: "oversize-t-shirt-123" });
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
it("prefers an exact .ts registration over the stripped fallback", async () => {
|
|
418
|
-
const hits: string[] = [];
|
|
419
|
-
registerCommerceLoader("shopify/loaders/X", async () => {
|
|
420
|
-
hits.push("plain");
|
|
421
|
-
return null;
|
|
422
|
-
});
|
|
423
|
-
registerCommerceLoader("shopify/loaders/X.ts", async () => {
|
|
424
|
-
hits.push("dotts");
|
|
425
|
-
return null;
|
|
426
|
-
});
|
|
427
|
-
await resolveValue({ __resolveType: "shopify/loaders/X.ts" }, undefined, {
|
|
428
|
-
url: "https://s.com/",
|
|
429
|
-
path: "/",
|
|
430
|
-
});
|
|
431
|
-
expect(hits).toEqual(["dotts"]);
|
|
432
|
-
});
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
// ---------------------------------------------------------------------------
|
|
436
|
-
// Async rendering: the admin (CMS Lazy ⚡ toggle) is the source of truth
|
|
437
|
-
// ---------------------------------------------------------------------------
|
|
438
|
-
//
|
|
439
|
-
// Regression guard for issue #266: the framework must NOT defer a section by
|
|
440
|
-
// position, and must NOT let code-level flags (`export const eager/neverDefer`)
|
|
441
|
-
// override the editor's ⚡ choice. A section is deferred iff the editor wrapped
|
|
442
|
-
// it in CMS Lazy/Deferred in the admin. The position threshold + code flags are
|
|
443
|
-
// an explicit per-site opt-in that is OFF by default (foldThreshold = Infinity)
|
|
444
|
-
// and never overrides the admin.
|
|
445
|
-
|
|
446
|
-
describe("async rendering config defaults", () => {
|
|
447
|
-
beforeEach(() => {
|
|
448
|
-
// Reset the globalThis-backed config so each assertion is order-independent.
|
|
449
|
-
(globalThis as any).__deco.asyncConfig = null;
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
it("DEFAULT_FOLD_THRESHOLD is Infinity (position-based deferral off)", () => {
|
|
453
|
-
expect(DEFAULT_FOLD_THRESHOLD).toBe(Infinity);
|
|
454
|
-
});
|
|
455
|
-
|
|
456
|
-
it("setAsyncRenderingConfig() defaults to foldThreshold=Infinity, respectCmsLazy=true", () => {
|
|
457
|
-
setAsyncRenderingConfig();
|
|
458
|
-
const cfg = getAsyncRenderingConfig();
|
|
459
|
-
expect(cfg).not.toBeNull();
|
|
460
|
-
expect(cfg!.foldThreshold).toBe(Infinity);
|
|
461
|
-
expect(cfg!.respectCmsLazy).toBe(true);
|
|
462
|
-
expect(cfg!.botAwareSeo).toBe(false); // opt-in — off by default
|
|
463
|
-
expect(cfg!.deferredTrigger).toBe("intersection"); // opt-in — a bump changes nothing
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
it("carries deferredTrigger through, and a later partial call does not reset it", () => {
|
|
467
|
-
setAsyncRenderingConfig({ deferredTrigger: "load" });
|
|
468
|
-
expect(getAsyncRenderingConfig()!.deferredTrigger).toBe("load");
|
|
469
|
-
|
|
470
|
-
// applySectionConventions() re-calls this with only `alwaysEager` after the
|
|
471
|
-
// site's setup.ts ran — the trigger must survive that merge.
|
|
472
|
-
setAsyncRenderingConfig({ alwaysEager: ["site/sections/Header.tsx"] });
|
|
473
|
-
expect(getAsyncRenderingConfig()!.deferredTrigger).toBe("load");
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
it("preserves an explicit finite foldThreshold (opt-in)", () => {
|
|
477
|
-
setAsyncRenderingConfig({ foldThreshold: 3 });
|
|
478
|
-
expect(getAsyncRenderingConfig()!.foldThreshold).toBe(3);
|
|
479
|
-
});
|
|
480
|
-
});
|
|
481
|
-
|
|
482
|
-
describe("shouldDeferSection — admin is the source of truth", () => {
|
|
483
|
-
// Deliberately does NOT set `deferredTrigger`: this literal stands in for the
|
|
484
|
-
// ones outside the package, and it has to keep compiling after the field was
|
|
485
|
-
// added. If that ever breaks, the option stopped being backwards compatible.
|
|
486
|
-
const mkCfg = (over: Partial<AsyncRenderingConfig> = {}): AsyncRenderingConfig => ({
|
|
487
|
-
respectCmsLazy: true,
|
|
488
|
-
foldThreshold: Infinity,
|
|
489
|
-
alwaysEager: new Set(),
|
|
490
|
-
botAwareSeo: false,
|
|
491
|
-
...over,
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
const lazyWrap = (inner: object) => ({
|
|
495
|
-
__resolveType: WELL_KNOWN_TYPES.LAZY,
|
|
496
|
-
section: inner,
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
it("defers a section the editor marked ⚡ (wrapped in CMS Lazy)", () => {
|
|
500
|
-
const section = lazyWrap({ __resolveType: "site/sections/Hero.tsx" });
|
|
501
|
-
expect(shouldDeferSection(section, 0, mkCfg(), false)).toBe(true);
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
it("renders a non-⚡ section eagerly regardless of position (default Infinity)", () => {
|
|
505
|
-
const section = { __resolveType: "site/sections/SeoText.tsx" };
|
|
506
|
-
// Position 5 used to auto-defer with the old foldThreshold=3 — now SSR.
|
|
507
|
-
expect(shouldDeferSection(section, 5, mkCfg(), false)).toBe(false);
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
it("admin ⚡ overrides `export const eager` (code flag ignored)", () => {
|
|
511
|
-
const key = "site/sections/EagerButLazy.tsx";
|
|
512
|
-
registerEagerSections([key]);
|
|
513
|
-
const section = lazyWrap({ __resolveType: key });
|
|
514
|
-
// Even with a finite threshold where the eager flag would otherwise apply,
|
|
515
|
-
// the editor's ⚡ wins → deferred.
|
|
516
|
-
expect(shouldDeferSection(section, 0, mkCfg({ foldThreshold: 3 }), false)).toBe(true);
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
it("admin ⚡ overrides `export const neverDefer` (code flag ignored)", () => {
|
|
520
|
-
const key = "site/sections/NeverDeferButLazy.tsx";
|
|
521
|
-
registerNeverDeferSections([key]);
|
|
522
|
-
const section = lazyWrap({ __resolveType: key });
|
|
523
|
-
expect(shouldDeferSection(section, 0, mkCfg(), false)).toBe(true);
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
it("bots always get SSR, even for ⚡ sections (SEO)", () => {
|
|
527
|
-
const section = lazyWrap({ __resolveType: "site/sections/Hero.tsx" });
|
|
528
|
-
expect(shouldDeferSection(section, 0, mkCfg(), true)).toBe(false);
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
it("opt-in finite foldThreshold defers UNMARKED sections by position", () => {
|
|
532
|
-
const section = { __resolveType: "site/sections/Shelf.tsx" };
|
|
533
|
-
expect(shouldDeferSection(section, 5, mkCfg({ foldThreshold: 3 }), false)).toBe(true);
|
|
534
|
-
expect(shouldDeferSection(section, 1, mkCfg({ foldThreshold: 3 }), false)).toBe(false);
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
it("`export const deferred = true` defers one section even with respectCmsLazy off", () => {
|
|
538
|
-
const key = "site/sections/HeavyPLP.tsx";
|
|
539
|
-
registerAlwaysDeferSections([key]);
|
|
540
|
-
const section = { __resolveType: key };
|
|
541
|
-
// Not ⚡-wrapped and respectCmsLazy disabled — the per-section flag is the
|
|
542
|
-
// only thing forcing deferral here.
|
|
543
|
-
expect(shouldDeferSection(section, 0, mkCfg({ respectCmsLazy: false }), false)).toBe(true);
|
|
544
|
-
});
|
|
545
|
-
|
|
546
|
-
it("`export const deferred = true` still stays eager for bots (SEO)", () => {
|
|
547
|
-
const key = "site/sections/HeavyPLPBot.tsx";
|
|
548
|
-
registerAlwaysDeferSections([key]);
|
|
549
|
-
const section = { __resolveType: key };
|
|
550
|
-
expect(shouldDeferSection(section, 0, mkCfg({ respectCmsLazy: false }), true)).toBe(false);
|
|
551
|
-
});
|
|
552
|
-
});
|
|
553
|
-
|
|
554
|
-
// ---------------------------------------------------------------------------
|
|
555
|
-
// isEagerRequest — programmatic (non-navigation) fetches render eagerly
|
|
556
|
-
// ---------------------------------------------------------------------------
|
|
557
|
-
//
|
|
558
|
-
// An AJAX `fetch()` (e.g. the PLP "Ver mais"/load-more pagination) reads the
|
|
559
|
-
// static SSR HTML and never runs the client-side deferred-section resolution,
|
|
560
|
-
// so a ⚡ deferred section would be invisible (skeleton only). Such requests —
|
|
561
|
-
// identified by `Sec-Fetch-Dest: empty` — must render eagerly. Top-level browser
|
|
562
|
-
// navigations (`document`) stay deferred; SPA navigations are excluded so
|
|
563
|
-
// page-SEO commerce loaders stay off for humans (#286).
|
|
564
|
-
describe("isEagerRequest — programmatic fetch detection", () => {
|
|
565
|
-
const HUMAN_UA =
|
|
566
|
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36";
|
|
567
|
-
const reqWith = (dest: string) =>
|
|
568
|
-
new Request("https://store.com/escolar?page=2", {
|
|
569
|
-
headers: { "user-agent": HUMAN_UA, "sec-fetch-dest": dest },
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
it("programmatic fetch (Sec-Fetch-Dest: empty) is eager", () => {
|
|
573
|
-
expect(isEagerRequest({ userAgent: HUMAN_UA, request: reqWith("empty") })).toBe(true);
|
|
574
|
-
});
|
|
575
|
-
|
|
576
|
-
it("top-level navigation (Sec-Fetch-Dest: document) is NOT eager", () => {
|
|
577
|
-
expect(isEagerRequest({ userAgent: HUMAN_UA, request: reqWith("document") })).toBe(false);
|
|
578
|
-
});
|
|
579
|
-
|
|
580
|
-
it("SPA navigation (empty + isClientNavigation) is NOT eager (preserves #286)", () => {
|
|
581
|
-
expect(
|
|
582
|
-
isEagerRequest({
|
|
583
|
-
userAgent: HUMAN_UA,
|
|
584
|
-
request: reqWith("empty"),
|
|
585
|
-
isClientNavigation: true,
|
|
586
|
-
}),
|
|
587
|
-
).toBe(false);
|
|
588
|
-
});
|
|
589
|
-
|
|
590
|
-
it("falls back to matcherCtx.headers when no Request is present", () => {
|
|
591
|
-
expect(isEagerRequest({ userAgent: HUMAN_UA, headers: { "sec-fetch-dest": "empty" } })).toBe(
|
|
592
|
-
true,
|
|
593
|
-
);
|
|
594
|
-
});
|
|
595
|
-
|
|
596
|
-
it("a request with no Sec-Fetch headers stays deferred (no UA bot, no override)", () => {
|
|
597
|
-
expect(isEagerRequest({ userAgent: HUMAN_UA, url: "https://store.com/escolar" })).toBe(false);
|
|
598
|
-
});
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
// ---------------------------------------------------------------------------
|
|
602
|
-
// Page SEO block — bot-aware commerce resolution
|
|
603
|
-
// ---------------------------------------------------------------------------
|
|
604
|
-
//
|
|
605
|
-
// The page-level SEO block (e.g. `commerce/sections/Seo/SeoPLPV2.tsx` with
|
|
606
|
-
// `jsonLD: { __resolveType: "PLP Loader" }`) is always eager. For humans the
|
|
607
|
-
// commerce-loader-backed props must be skipped: resolving them blocks SSR on a
|
|
608
|
-
// heavy upstream fetch and serializes the full product payload into HTML that a
|
|
609
|
-
// human request never renders. Bots keep the full resolution for indexing.
|
|
610
|
-
describe("resolvePageSeoBlock — bot-aware commerce SEO", () => {
|
|
611
|
-
const KEY = "site/loaders/__test/plpSeoLoader";
|
|
612
|
-
const HUMAN_UA =
|
|
613
|
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36";
|
|
614
|
-
const BOT_UA = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
|
|
615
|
-
|
|
616
|
-
const seoBlock = {
|
|
617
|
-
__resolveType: "commerce/sections/Seo/SeoPLPV2.tsx",
|
|
618
|
-
title: "Escolar",
|
|
619
|
-
jsonLD: { __resolveType: KEY },
|
|
620
|
-
};
|
|
621
|
-
|
|
622
|
-
const rctx = (userAgent?: string) =>
|
|
623
|
-
({
|
|
624
|
-
matcherCtx: { userAgent, url: "https://store.com/escolar", path: "/escolar" },
|
|
625
|
-
memo: new Map(),
|
|
626
|
-
depth: 0,
|
|
627
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
628
|
-
}) as any;
|
|
629
|
-
|
|
630
|
-
beforeEach(() => {
|
|
631
|
-
clearCommerceLoaders();
|
|
632
|
-
// Bot-aware SEO is opt-in — enable it for these cases.
|
|
633
|
-
setAsyncRenderingConfig({ botAwareSeo: true });
|
|
634
|
-
});
|
|
635
|
-
afterEach(() => {
|
|
636
|
-
clearCommerceLoaders();
|
|
637
|
-
setAsyncRenderingConfig({ botAwareSeo: false });
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
it("flag OFF (default): humans still get the full SEO — no regression", async () => {
|
|
641
|
-
setAsyncRenderingConfig({ botAwareSeo: false });
|
|
642
|
-
let calls = 0;
|
|
643
|
-
registerCommerceLoader(KEY, async () => {
|
|
644
|
-
calls++;
|
|
645
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
646
|
-
});
|
|
647
|
-
|
|
648
|
-
const res = await resolvePageSeoBlock(seoBlock, rctx(HUMAN_UA));
|
|
649
|
-
|
|
650
|
-
expect(calls).toBe(1); // loader runs for everyone when the flag is off
|
|
651
|
-
expect(res?.props?.jsonLD).toMatchObject({ seo: { title: "Rich SEO title" } });
|
|
652
|
-
});
|
|
653
|
-
|
|
654
|
-
it("humans: skips the commerce loader and drops the commerce-backed prop", async () => {
|
|
655
|
-
let calls = 0;
|
|
656
|
-
registerCommerceLoader(KEY, async () => {
|
|
657
|
-
calls++;
|
|
658
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
659
|
-
});
|
|
660
|
-
|
|
661
|
-
const res = await resolvePageSeoBlock(seoBlock, rctx(HUMAN_UA));
|
|
662
|
-
|
|
663
|
-
expect(calls).toBe(0); // heavy upstream fetch never runs for humans
|
|
664
|
-
expect(res?.props).toHaveProperty("title", "Escolar"); // literal props kept
|
|
665
|
-
expect(res?.props).not.toHaveProperty("jsonLD"); // no product payload in HTML
|
|
666
|
-
});
|
|
667
|
-
|
|
668
|
-
it("bots: resolves the commerce loader and keeps the JSON-LD payload", async () => {
|
|
669
|
-
let calls = 0;
|
|
670
|
-
registerCommerceLoader(KEY, async () => {
|
|
671
|
-
calls++;
|
|
672
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
const res = await resolvePageSeoBlock(seoBlock, rctx(BOT_UA));
|
|
676
|
-
|
|
677
|
-
expect(calls).toBe(1);
|
|
678
|
-
expect(res?.props?.jsonLD).toMatchObject({ seo: { title: "Rich SEO title" } });
|
|
679
|
-
});
|
|
680
|
-
|
|
681
|
-
it("?__deco_ssr=1 override: a human UA gets the full eager SEO (QA/audit)", async () => {
|
|
682
|
-
let calls = 0;
|
|
683
|
-
registerCommerceLoader(KEY, async () => {
|
|
684
|
-
calls++;
|
|
685
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
686
|
-
});
|
|
687
|
-
|
|
688
|
-
const ctx = {
|
|
689
|
-
matcherCtx: {
|
|
690
|
-
userAgent: HUMAN_UA,
|
|
691
|
-
url: "https://store.com/escolar?__deco_ssr=1",
|
|
692
|
-
path: "/escolar",
|
|
693
|
-
},
|
|
694
|
-
memo: new Map(),
|
|
695
|
-
depth: 0,
|
|
696
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
697
|
-
} as any;
|
|
698
|
-
const res = await resolvePageSeoBlock(seoBlock, ctx);
|
|
699
|
-
|
|
700
|
-
expect(calls).toBe(1); // override forces the commerce loader to run
|
|
701
|
-
expect(res?.props?.jsonLD).toMatchObject({ seo: { title: "Rich SEO title" } });
|
|
702
|
-
});
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
describe("resolvePageSeoBlock — per-section ignoreStructuredData drives the fetch-skip", () => {
|
|
706
|
-
const KEY = "site/loaders/__test/plpSeoLoader";
|
|
707
|
-
const HUMAN_UA =
|
|
708
|
-
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120 Safari/537.36";
|
|
709
|
-
const BOT_UA = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
|
|
710
|
-
|
|
711
|
-
// PLP toggle lives under `configJsonLD`; PDP toggle is top-level.
|
|
712
|
-
const plpBlock = {
|
|
713
|
-
__resolveType: "commerce/sections/Seo/SeoPLPV2.tsx",
|
|
714
|
-
title: "Escolar",
|
|
715
|
-
jsonLD: { __resolveType: KEY },
|
|
716
|
-
configJsonLD: { ignoreStructuredData: true },
|
|
717
|
-
};
|
|
718
|
-
const pdpBlock = {
|
|
719
|
-
__resolveType: "commerce/sections/Seo/SeoPDPV2.tsx",
|
|
720
|
-
title: "Produto",
|
|
721
|
-
jsonLD: { __resolveType: KEY },
|
|
722
|
-
ignoreStructuredData: true,
|
|
723
|
-
};
|
|
724
|
-
|
|
725
|
-
const rctx = (userAgent?: string) =>
|
|
726
|
-
({
|
|
727
|
-
matcherCtx: { userAgent, url: "https://store.com/escolar", path: "/escolar" },
|
|
728
|
-
memo: new Map(),
|
|
729
|
-
depth: 0,
|
|
730
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
731
|
-
}) as any;
|
|
732
|
-
|
|
733
|
-
beforeEach(() => {
|
|
734
|
-
clearCommerceLoaders();
|
|
735
|
-
// The per-section toggle is the PRIMARY lever — it must work with the
|
|
736
|
-
// site-wide botAwareSeo shortcut OFF.
|
|
737
|
-
setAsyncRenderingConfig({ botAwareSeo: false });
|
|
738
|
-
});
|
|
739
|
-
afterEach(() => {
|
|
740
|
-
clearCommerceLoaders();
|
|
741
|
-
setAsyncRenderingConfig({ botAwareSeo: false });
|
|
742
|
-
});
|
|
743
|
-
|
|
744
|
-
it("humans: toggle skips the commerce loader (PLP configJsonLD)", async () => {
|
|
745
|
-
let calls = 0;
|
|
746
|
-
registerCommerceLoader(KEY, async () => {
|
|
747
|
-
calls++;
|
|
748
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
749
|
-
});
|
|
750
|
-
|
|
751
|
-
const res = await resolvePageSeoBlock(plpBlock, rctx(HUMAN_UA));
|
|
752
|
-
|
|
753
|
-
expect(calls).toBe(0); // no product fetch for humans
|
|
754
|
-
expect(res?.props).toHaveProperty("title", "Escolar");
|
|
755
|
-
expect(res?.props).not.toHaveProperty("jsonLD");
|
|
756
|
-
});
|
|
757
|
-
|
|
758
|
-
it("bots: toggle still resolves the commerce loader (PLP configJsonLD)", async () => {
|
|
759
|
-
let calls = 0;
|
|
760
|
-
registerCommerceLoader(KEY, async () => {
|
|
761
|
-
calls++;
|
|
762
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
763
|
-
});
|
|
764
|
-
|
|
765
|
-
const res = await resolvePageSeoBlock(plpBlock, rctx(BOT_UA));
|
|
766
|
-
|
|
767
|
-
expect(calls).toBe(1);
|
|
768
|
-
expect(res?.props?.jsonLD).toMatchObject({ seo: { title: "Rich SEO title" } });
|
|
769
|
-
});
|
|
770
|
-
|
|
771
|
-
it("humans: top-level toggle skips the commerce loader (PDP)", async () => {
|
|
772
|
-
let calls = 0;
|
|
773
|
-
registerCommerceLoader(KEY, async () => {
|
|
774
|
-
calls++;
|
|
775
|
-
return { seo: { title: "Rich SEO title" }, product: { id: 1 } };
|
|
776
|
-
});
|
|
777
|
-
|
|
778
|
-
const res = await resolvePageSeoBlock(pdpBlock, rctx(HUMAN_UA));
|
|
779
|
-
|
|
780
|
-
expect(calls).toBe(0);
|
|
781
|
-
expect(res?.props).not.toHaveProperty("jsonLD");
|
|
782
|
-
});
|
|
783
|
-
|
|
784
|
-
it("toggle OFF: humans keep the full SEO even with botAwareSeo off (no regression)", async () => {
|
|
785
|
-
let calls = 0;
|
|
786
|
-
registerCommerceLoader(KEY, async () => {
|
|
787
|
-
calls++;
|
|
788
|
-
return { seo: { title: "Rich SEO title" }, products: [{ id: 1 }] };
|
|
789
|
-
});
|
|
790
|
-
|
|
791
|
-
const noToggle = {
|
|
792
|
-
__resolveType: "commerce/sections/Seo/SeoPLPV2.tsx",
|
|
793
|
-
title: "Escolar",
|
|
794
|
-
jsonLD: { __resolveType: KEY },
|
|
795
|
-
};
|
|
796
|
-
const res = await resolvePageSeoBlock(noToggle, rctx(HUMAN_UA));
|
|
797
|
-
|
|
798
|
-
expect(calls).toBe(1); // no toggle + no global flag → resolve for everyone
|
|
799
|
-
expect(res?.props?.jsonLD).toMatchObject({ seo: { title: "Rich SEO title" } });
|
|
800
|
-
});
|
|
801
|
-
});
|
|
802
|
-
|
|
803
|
-
describe("extractSeoFromProps — commerce jsonLD structured data", () => {
|
|
804
|
-
const plp = (overrides: Record<string, unknown> = {}) => ({
|
|
805
|
-
"@type": "ProductListingPage",
|
|
806
|
-
breadcrumb: {
|
|
807
|
-
"@type": "BreadcrumbList",
|
|
808
|
-
itemListElement: [
|
|
809
|
-
{ "@type": "ListItem", position: 1, item: "https://x.com/a" },
|
|
810
|
-
{ "@type": "ListItem", position: 2, item: "https://x.com/a/b" },
|
|
811
|
-
],
|
|
812
|
-
},
|
|
813
|
-
products: [{ "@type": "Product", name: "P1" }],
|
|
814
|
-
seo: { title: "PLP Title", description: "PLP Desc" },
|
|
815
|
-
...overrides,
|
|
816
|
-
});
|
|
817
|
-
|
|
818
|
-
const pdp = (overrides: Record<string, unknown> = {}) => ({
|
|
819
|
-
"@type": "ProductDetailsPage",
|
|
820
|
-
breadcrumbList: {
|
|
821
|
-
"@type": "BreadcrumbList",
|
|
822
|
-
itemListElement: [
|
|
823
|
-
{ "@type": "ListItem", position: 1, item: "https://x.com/a" },
|
|
824
|
-
{ "@type": "ListItem", position: 2, item: "https://x.com/a/p" },
|
|
825
|
-
],
|
|
826
|
-
},
|
|
827
|
-
product: { "@type": "Product", name: "P1", image: [{ url: "https://x.com/p.jpg" }] },
|
|
828
|
-
seo: { title: "PDP Title", description: "PDP Desc" },
|
|
829
|
-
...overrides,
|
|
830
|
-
});
|
|
831
|
-
|
|
832
|
-
it("derives title/description/canonical and emits the ItemList JSON-LD (PLP)", () => {
|
|
833
|
-
// The acme.com.br/eletroportateis regression: a page.seo pointing at
|
|
834
|
-
// commerce/sections/Seo/SeoPLPV2.tsx resolved its jsonLD but emitted no
|
|
835
|
-
// schema.org. There is no section component in start to run this transform.
|
|
836
|
-
const seo = extractSeoFromProps({
|
|
837
|
-
__resolveType: "commerce/sections/Seo/SeoPLPV2.tsx",
|
|
838
|
-
jsonLD: plp(),
|
|
839
|
-
});
|
|
840
|
-
expect(seo.title).toBe("PLP Title");
|
|
841
|
-
expect(seo.description).toBe("PLP Desc");
|
|
842
|
-
// Highest-position breadcrumb item wins.
|
|
843
|
-
expect(seo.canonical).toBe("https://x.com/a/b");
|
|
844
|
-
expect(seo.noIndexing).toBe(false);
|
|
845
|
-
expect(seo.jsonLDs).toHaveLength(2);
|
|
846
|
-
expect(seo.jsonLDs?.[0]).toMatchObject({
|
|
847
|
-
"@context": "https://schema.org",
|
|
848
|
-
"@type": "ItemList",
|
|
849
|
-
});
|
|
850
|
-
expect(seo.jsonLDs?.[1]).toMatchObject({
|
|
851
|
-
"@context": "https://schema.org",
|
|
852
|
-
"@type": "BreadcrumbList",
|
|
853
|
-
});
|
|
854
|
-
});
|
|
855
|
-
|
|
856
|
-
it("emits the product image and JSON-LD for a PDP", () => {
|
|
857
|
-
const seo = extractSeoFromProps({
|
|
858
|
-
__resolveType: "commerce/sections/Seo/SeoPDPV2.tsx",
|
|
859
|
-
jsonLD: {
|
|
860
|
-
"@type": "ProductDetailsPage",
|
|
861
|
-
breadcrumbList: { "@type": "BreadcrumbList", itemListElement: [] },
|
|
862
|
-
product: {
|
|
863
|
-
"@type": "Product",
|
|
864
|
-
name: "Widget",
|
|
865
|
-
image: [{ url: "https://x.com/w.jpg" }],
|
|
866
|
-
},
|
|
867
|
-
seo: { title: "Widget", description: "A widget" },
|
|
868
|
-
},
|
|
869
|
-
});
|
|
870
|
-
expect(seo.title).toBe("Widget");
|
|
871
|
-
expect(seo.image).toBe("https://x.com/w.jpg");
|
|
872
|
-
expect(seo.jsonLDs?.[0]).toMatchObject({ "@type": "ProductDetailsPage" });
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
it("lets manual override fields win over the jsonLD-derived ones", () => {
|
|
876
|
-
const seo = extractSeoFromProps({
|
|
877
|
-
jsonLD: plp(),
|
|
878
|
-
title: "Manual Title",
|
|
879
|
-
canonical: "https://x.com/manual",
|
|
880
|
-
});
|
|
881
|
-
expect(seo.title).toBe("Manual Title");
|
|
882
|
-
expect(seo.canonical).toBe("https://x.com/manual");
|
|
883
|
-
expect(seo.jsonLDs).toHaveLength(2);
|
|
884
|
-
});
|
|
885
|
-
|
|
886
|
-
it("omits structured data for humans when ignoreStructuredData is set", () => {
|
|
887
|
-
const seo = extractSeoFromProps({
|
|
888
|
-
jsonLD: plp(),
|
|
889
|
-
configJsonLD: { ignoreStructuredData: true },
|
|
890
|
-
});
|
|
891
|
-
expect(seo.jsonLDs).toBeUndefined();
|
|
892
|
-
// Metadata is still derived — only the ItemList is suppressed.
|
|
893
|
-
expect(seo.title).toBe("PLP Title");
|
|
894
|
-
});
|
|
895
|
-
|
|
896
|
-
it("keeps structured data for bots even when ignoreStructuredData is set", () => {
|
|
897
|
-
// Bot-aware: the toggle suppresses JSON-LD for humans only. Crawlers (and the
|
|
898
|
-
// `?__deco_ssr=1` audit override, both surfaced via isEager) still get it.
|
|
899
|
-
const seo = extractSeoFromProps(
|
|
900
|
-
{ jsonLD: plp(), configJsonLD: { ignoreStructuredData: true } },
|
|
901
|
-
{ isEager: true },
|
|
902
|
-
);
|
|
903
|
-
expect(seo.jsonLDs).toHaveLength(2);
|
|
904
|
-
expect(seo.title).toBe("PLP Title");
|
|
905
|
-
});
|
|
906
|
-
|
|
907
|
-
it("omits structured data for bots too when the listing is empty", () => {
|
|
908
|
-
// ignoreStructuredData is bot-aware, but an empty listing contributes no
|
|
909
|
-
// ItemList regardless of bot status.
|
|
910
|
-
const seo = extractSeoFromProps(
|
|
911
|
-
{ jsonLD: plp({ products: [] }), configJsonLD: { ignoreStructuredData: true } },
|
|
912
|
-
{ isEager: true },
|
|
913
|
-
);
|
|
914
|
-
expect(seo.jsonLDs).toBeUndefined();
|
|
915
|
-
});
|
|
916
|
-
|
|
917
|
-
it("keeps PDP structured data for bots with top-level ignoreStructuredData", () => {
|
|
918
|
-
const seo = extractSeoFromProps(
|
|
919
|
-
{ jsonLD: pdp(), ignoreStructuredData: true },
|
|
920
|
-
{ isEager: true },
|
|
921
|
-
);
|
|
922
|
-
expect(seo.jsonLDs).toHaveLength(1);
|
|
923
|
-
});
|
|
924
|
-
|
|
925
|
-
it("marks an empty listing noIndexing and emits no ItemList", () => {
|
|
926
|
-
const seo = extractSeoFromProps({ jsonLD: plp({ products: [] }) });
|
|
927
|
-
expect(seo.noIndexing).toBe(true);
|
|
928
|
-
expect(seo.jsonLDs).toBeUndefined();
|
|
929
|
-
});
|
|
930
|
-
|
|
931
|
-
it("does not touch a section that already emitted jsonLDs", () => {
|
|
932
|
-
const existing = [{ "@type": "WebSite" }];
|
|
933
|
-
const seo = extractSeoFromProps({ jsonLD: plp(), jsonLDs: existing });
|
|
934
|
-
expect(seo.jsonLDs).toBe(existing);
|
|
935
|
-
});
|
|
936
|
-
|
|
937
|
-
it("emits a compact ItemList (position/url/name), not the whole listing page", () => {
|
|
938
|
-
const heavy = {
|
|
939
|
-
"@type": "Product",
|
|
940
|
-
name: "SKU name",
|
|
941
|
-
url: "https://x.com/p1/p",
|
|
942
|
-
isVariantOf: { name: "Product 1", hasVariant: [{ sku: "1" }, { sku: "2" }] },
|
|
943
|
-
additionalProperty: [{ name: "cluster", value: "42" }],
|
|
944
|
-
image: [{ url: "https://x.com/1.jpg" }],
|
|
945
|
-
video: [{ "@type": "VideoObject" }],
|
|
946
|
-
};
|
|
947
|
-
const seo = extractSeoFromProps({
|
|
948
|
-
jsonLD: plp({
|
|
949
|
-
products: [heavy, { "@type": "Product", name: "P2", url: "https://x.com/p2/p" }],
|
|
950
|
-
}),
|
|
951
|
-
});
|
|
952
|
-
expect(seo.jsonLDs?.[0]).toEqual({
|
|
953
|
-
"@context": "https://schema.org",
|
|
954
|
-
"@type": "ItemList",
|
|
955
|
-
numberOfItems: 2,
|
|
956
|
-
itemListElement: [
|
|
957
|
-
{ "@type": "ListItem", position: 1, url: "https://x.com/p1/p", name: "Product 1" },
|
|
958
|
-
{ "@type": "ListItem", position: 2, url: "https://x.com/p2/p", name: "P2" },
|
|
959
|
-
],
|
|
960
|
-
});
|
|
961
|
-
const json = JSON.stringify(seo.jsonLDs);
|
|
962
|
-
expect(json).not.toContain("hasVariant");
|
|
963
|
-
expect(json).not.toContain("additionalProperty");
|
|
964
|
-
expect(json).not.toContain("VideoObject");
|
|
965
|
-
});
|
|
966
|
-
|
|
967
|
-
it("points ListItems at the canonical product URL and drops items without one", () => {
|
|
968
|
-
const seo = extractSeoFromProps({
|
|
969
|
-
jsonLD: plp({
|
|
970
|
-
products: [
|
|
971
|
-
{
|
|
972
|
-
name: "SKU A",
|
|
973
|
-
url: "https://x.com/a/p?skuId=1",
|
|
974
|
-
isVariantOf: { name: "A", url: "https://x.com/a/p" },
|
|
975
|
-
},
|
|
976
|
-
{ name: "No URL" },
|
|
977
|
-
{ name: "B", url: "https://x.com/b/p?skuId=2" },
|
|
978
|
-
],
|
|
979
|
-
}),
|
|
980
|
-
});
|
|
981
|
-
expect(seo.jsonLDs?.[0].itemListElement).toEqual([
|
|
982
|
-
{ "@type": "ListItem", position: 1, url: "https://x.com/a/p", name: "A" },
|
|
983
|
-
{ "@type": "ListItem", position: 2, url: "https://x.com/b/p?skuId=2", name: "B" },
|
|
984
|
-
]);
|
|
985
|
-
expect(seo.jsonLDs?.[0].numberOfItems).toBe(2);
|
|
986
|
-
});
|
|
987
|
-
|
|
988
|
-
it("makes path-only breadcrumb items absolute without touching the source", () => {
|
|
989
|
-
const breadcrumb = {
|
|
990
|
-
"@type": "BreadcrumbList",
|
|
991
|
-
itemListElement: [
|
|
992
|
-
{ "@type": "ListItem", position: 1, name: "Cat", item: "/cat" },
|
|
993
|
-
{ "@type": "ListItem", position: 2, name: "Sub", item: "https://y.com/cat/sub" },
|
|
994
|
-
],
|
|
995
|
-
};
|
|
996
|
-
const seo = extractSeoFromProps({
|
|
997
|
-
jsonLD: plp({ breadcrumb, products: [{ name: "A", url: "https://x.com/a/p" }] }),
|
|
998
|
-
});
|
|
999
|
-
expect(seo.jsonLDs?.[1].itemListElement.map((el: { item: string }) => el.item)).toEqual([
|
|
1000
|
-
"https://x.com/cat",
|
|
1001
|
-
"https://y.com/cat/sub",
|
|
1002
|
-
]);
|
|
1003
|
-
// In-app breadcrumb (section data) keeps its relative links.
|
|
1004
|
-
expect(breadcrumb.itemListElement[0].item).toBe("/cat");
|
|
1005
|
-
});
|
|
1006
|
-
|
|
1007
|
-
it("falls back to seo.canonical for the origin and leaves //host items alone", () => {
|
|
1008
|
-
const seo = extractSeoFromProps({
|
|
1009
|
-
jsonLD: plp({
|
|
1010
|
-
seo: { title: "T", canonical: "https://z.com/cat" },
|
|
1011
|
-
products: [{ name: "No URL" }, { name: "A", url: "/a/p" }],
|
|
1012
|
-
breadcrumb: {
|
|
1013
|
-
"@type": "BreadcrumbList",
|
|
1014
|
-
itemListElement: [
|
|
1015
|
-
{ "@type": "ListItem", position: 1, name: "Cat", item: "/cat" },
|
|
1016
|
-
{ "@type": "ListItem", position: 2, name: "Ext", item: "//evil.com/x" },
|
|
1017
|
-
],
|
|
1018
|
-
},
|
|
1019
|
-
}),
|
|
1020
|
-
});
|
|
1021
|
-
expect(seo.jsonLDs?.[1].itemListElement.map((el: { item: string }) => el.item)).toEqual([
|
|
1022
|
-
"https://z.com/cat",
|
|
1023
|
-
"//evil.com/x",
|
|
1024
|
-
]);
|
|
1025
|
-
});
|
|
1026
|
-
|
|
1027
|
-
it("omits the BreadcrumbList when the listing has no breadcrumb", () => {
|
|
1028
|
-
const seo = extractSeoFromProps({ jsonLD: plp({ breadcrumb: undefined }) });
|
|
1029
|
-
expect(seo.jsonLDs).toHaveLength(1);
|
|
1030
|
-
expect(seo.jsonLDs?.[0]).toMatchObject({ "@type": "ItemList" });
|
|
1031
|
-
});
|
|
1032
|
-
|
|
1033
|
-
it("omitVariants empties PDP variants without mutating the source", () => {
|
|
1034
|
-
const source = pdp({
|
|
1035
|
-
product: { "@type": "Product", name: "P1", isVariantOf: { hasVariant: [{ sku: "1" }] } },
|
|
1036
|
-
});
|
|
1037
|
-
const seo = extractSeoFromProps({ jsonLD: source, omitVariants: true });
|
|
1038
|
-
expect(seo.jsonLDs?.[0].product.isVariantOf.hasVariant).toEqual([]);
|
|
1039
|
-
// biome-ignore lint/suspicious/noExplicitAny: test fixture
|
|
1040
|
-
expect((source.product as any).isVariantOf.hasVariant).toHaveLength(1);
|
|
1041
|
-
});
|
|
1042
|
-
});
|
|
1043
|
-
|
|
1044
|
-
// ---------------------------------------------------------------------------
|
|
1045
|
-
// resolveDecoPage — client nav gets the SAME eager/deferred split as SSR
|
|
1046
|
-
// ---------------------------------------------------------------------------
|
|
1047
|
-
//
|
|
1048
|
-
// A TanStack route loader is BLOCKING: the router will not commit the
|
|
1049
|
-
// transition until the loader promise settles. Eager-resolving every ⚡
|
|
1050
|
-
// below-fold section on client nav therefore freezes the previous page for as
|
|
1051
|
-
// long as the slowest upstream takes (measured on a real PDP: 20 awaited
|
|
1052
|
-
// sections, 2717ms, 3.41MB — worse than a full reload). So deferral must apply
|
|
1053
|
-
// to client nav exactly as it does to SSR.
|
|
1054
|
-
//
|
|
1055
|
-
// The historical `!isClientNavigation` gate (decocms/blocks#277) was a
|
|
1056
|
-
// workaround for deferred loaders that appeared to lose per-request app
|
|
1057
|
-
// context. It traded a page-wide latency regression for that symptom. The
|
|
1058
|
-
// second hop (`loadDeferredSection`) is the SAME server fn the SSR path has
|
|
1059
|
-
// always used and rebuilds MatcherContext from the real request — see the
|
|
1060
|
-
// "#277 — per-request context survives the deferred second hop" cases below.
|
|
1061
|
-
// ---------------------------------------------------------------------------
|
|
1062
|
-
|
|
1063
|
-
describe("resolveDecoPage — deferral parity between SSR and client nav", () => {
|
|
1064
|
-
const lazySec = {
|
|
1065
|
-
__resolveType: WELL_KNOWN_TYPES.LAZY,
|
|
1066
|
-
section: { __resolveType: "site/sections/Hero.tsx" },
|
|
1067
|
-
};
|
|
1068
|
-
const eagerSec = { __resolveType: "site/sections/Banner.tsx" };
|
|
1069
|
-
|
|
1070
|
-
beforeEach(() => {
|
|
1071
|
-
// Enable async rendering so useAsync can be true.
|
|
1072
|
-
setAsyncRenderingConfig({ foldThreshold: Infinity, respectCmsLazy: true });
|
|
1073
|
-
// resolveSectionShallow unwraps the ⚡ and looks up the inner key via
|
|
1074
|
-
// getSection — return truthy so it produces a DeferredSection rather than
|
|
1075
|
-
// falling back to eager resolution.
|
|
1076
|
-
(getSection as ReturnType<typeof vi.fn>).mockReturnValue({ default: () => null });
|
|
1077
|
-
// A page with one plain section followed by one CMS ⚡-wrapped section.
|
|
1078
|
-
(findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
1079
|
-
page: { name: "test", sections: [eagerSec, lazySec] },
|
|
1080
|
-
params: {},
|
|
1081
|
-
blockKey: "test-page",
|
|
1082
|
-
});
|
|
1083
|
-
});
|
|
1084
|
-
|
|
1085
|
-
afterEach(() => {
|
|
1086
|
-
(getSection as ReturnType<typeof vi.fn>).mockReset();
|
|
1087
|
-
(findPageByPath as ReturnType<typeof vi.fn>).mockReset();
|
|
1088
|
-
});
|
|
1089
|
-
|
|
1090
|
-
it("SSR request defers the CMS ⚡ section and keeps the plain one eager", async () => {
|
|
1091
|
-
const result = await resolveDecoPage("/product/foo", {});
|
|
1092
|
-
expect(result?.deferredSections).toHaveLength(1);
|
|
1093
|
-
expect(result?.deferredSections[0].component).toBe("site/sections/Hero.tsx");
|
|
1094
|
-
expect(result?.resolvedSections.map((s) => s.component)).toEqual(["site/sections/Banner.tsx"]);
|
|
1095
|
-
});
|
|
1096
|
-
|
|
1097
|
-
it("client nav produces the IDENTICAL split — deferral is not disabled", async () => {
|
|
1098
|
-
const ssr = await resolveDecoPage("/product/foo", {});
|
|
1099
|
-
const nav = await resolveDecoPage("/product/foo", { isClientNavigation: true });
|
|
1100
|
-
|
|
1101
|
-
// The acceptance criterion: client nav returns deferredSections and awaits
|
|
1102
|
-
// only the eager set, exactly like SSR.
|
|
1103
|
-
expect(nav?.deferredSections).toHaveLength(1);
|
|
1104
|
-
expect(nav?.deferredSections.map((d) => d.component)).toEqual(
|
|
1105
|
-
ssr?.deferredSections.map((d) => d.component),
|
|
1106
|
-
);
|
|
1107
|
-
expect(nav?.resolvedSections.map((s) => s.component)).toEqual(
|
|
1108
|
-
ssr?.resolvedSections.map((s) => s.component),
|
|
1109
|
-
);
|
|
1110
|
-
});
|
|
1111
|
-
|
|
1112
|
-
it("the ⚡ section's index is preserved on client nav (ordering on the merge)", async () => {
|
|
1113
|
-
const nav = await resolveDecoPage("/product/foo", { isClientNavigation: true });
|
|
1114
|
-
expect(nav?.deferredSections[0].index).toBe(1);
|
|
1115
|
-
});
|
|
1116
|
-
|
|
1117
|
-
it("bots stay fully eager on a client-nav-flagged request (SEO guarantee)", async () => {
|
|
1118
|
-
const result = await resolveDecoPage("/product/foo", {
|
|
1119
|
-
isClientNavigation: true,
|
|
1120
|
-
userAgent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
|
|
1121
|
-
});
|
|
1122
|
-
expect(result?.deferredSections).toHaveLength(0);
|
|
1123
|
-
expect(result?.resolvedSections).toHaveLength(2);
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
it("?__deco_ssr=1 stays fully eager on a client-nav-flagged request", async () => {
|
|
1127
|
-
const result = await resolveDecoPage("/product/foo", {
|
|
1128
|
-
isClientNavigation: true,
|
|
1129
|
-
url: "https://store.com/product/foo?__deco_ssr=1",
|
|
1130
|
-
});
|
|
1131
|
-
expect(result?.deferredSections).toHaveLength(0);
|
|
1132
|
-
expect(result?.resolvedSections).toHaveLength(2);
|
|
1133
|
-
});
|
|
1134
|
-
|
|
1135
|
-
it("a genuine programmatic fetch (Sec-Fetch-Dest: empty, no client-nav flag) stays eager", async () => {
|
|
1136
|
-
const result = await resolveDecoPage("/product/foo", {
|
|
1137
|
-
request: new Request("https://store.com/product/foo", {
|
|
1138
|
-
headers: { "sec-fetch-dest": "empty" },
|
|
1139
|
-
}),
|
|
1140
|
-
});
|
|
1141
|
-
expect(result?.deferredSections).toHaveLength(0);
|
|
1142
|
-
});
|
|
1143
|
-
});
|
|
1144
|
-
|
|
1145
|
-
// ---------------------------------------------------------------------------
|
|
1146
|
-
// #277 — per-request context survives the deferred second hop
|
|
1147
|
-
// ---------------------------------------------------------------------------
|
|
1148
|
-
//
|
|
1149
|
-
// This is the invariant that made it safe to re-enable deferral on client nav.
|
|
1150
|
-
// #277 reported deferred sections rendering blank because their loaders "lost"
|
|
1151
|
-
// per-request app context. The guarantee is that `resolveDeferredSectionFull`
|
|
1152
|
-
// (and `loadDeferredSection`, which wraps it in @decocms/tanstack) threads the
|
|
1153
|
-
// caller's MatcherContext — cookies, url, path, userAgent, request — into BOTH
|
|
1154
|
-
// the cache-hit and the cache-miss (`reExtractRawProps`) branch. A different
|
|
1155
|
-
// isolate misses the in-process rawProps Map, so the miss path is the one that
|
|
1156
|
-
// actually runs in production on Cloudflare Workers; if it ever stops receiving
|
|
1157
|
-
// matcherCtx, cookie-dependent loaders silently resolve against an anonymous
|
|
1158
|
-
// request. Do not relax these assertions.
|
|
1159
|
-
|
|
1160
|
-
describe("#277 — deferred second hop keeps per-request context", () => {
|
|
1161
|
-
const PROBE = "test/matchers/probe.ts";
|
|
1162
|
-
const CTX: MatcherContext & { request: Request } = {
|
|
1163
|
-
userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0) Mobile Safari",
|
|
1164
|
-
url: "https://store.com/product/foo?utm=x",
|
|
1165
|
-
path: "/product/foo",
|
|
1166
|
-
cookies: { deco_segment: "abc", VtexIdclientAutCookie: "tok" },
|
|
1167
|
-
request: new Request("https://store.com/product/foo?utm=x"),
|
|
1168
|
-
};
|
|
1169
|
-
|
|
1170
|
-
/** Every MatcherContext the probe matcher was evaluated with. */
|
|
1171
|
-
let seen: MatcherContext[] = [];
|
|
1172
|
-
|
|
1173
|
-
// The ⚡ section sits behind a multivariate flag whose rule is the probe
|
|
1174
|
-
// matcher. Reaching the inner Shelf at all therefore PROVES the resolver
|
|
1175
|
-
// evaluated the rule, and the probe records exactly which context it saw —
|
|
1176
|
-
// an end-to-end assertion rather than a mock of the call site.
|
|
1177
|
-
const gatedLazy = {
|
|
1178
|
-
__resolveType: WELL_KNOWN_TYPES.MULTIVARIATE,
|
|
1179
|
-
variants: [
|
|
1180
|
-
{
|
|
1181
|
-
rule: { __resolveType: PROBE },
|
|
1182
|
-
value: {
|
|
1183
|
-
__resolveType: WELL_KNOWN_TYPES.LAZY,
|
|
1184
|
-
section: { __resolveType: "site/sections/Shelf.tsx", title: "Mais vendidos" },
|
|
1185
|
-
},
|
|
1186
|
-
},
|
|
1187
|
-
],
|
|
1188
|
-
};
|
|
1189
|
-
|
|
1190
|
-
beforeEach(() => {
|
|
1191
|
-
seen = [];
|
|
1192
|
-
registerMatcher(PROBE, (_rule, ctx) => {
|
|
1193
|
-
seen.push(ctx);
|
|
1194
|
-
return true;
|
|
1195
|
-
});
|
|
1196
|
-
setAsyncRenderingConfig({ foldThreshold: Infinity, respectCmsLazy: true });
|
|
1197
|
-
(getSection as ReturnType<typeof vi.fn>).mockReturnValue({ default: () => null });
|
|
1198
|
-
(findPageByPath as ReturnType<typeof vi.fn>).mockReturnValue({
|
|
1199
|
-
page: { name: "test", sections: [gatedLazy] },
|
|
1200
|
-
params: {},
|
|
1201
|
-
blockKey: "test-page",
|
|
1202
|
-
});
|
|
1203
|
-
(runSingleSectionLoader as ReturnType<typeof vi.fn>).mockImplementation(
|
|
1204
|
-
async (s: unknown) => s,
|
|
1205
|
-
);
|
|
1206
|
-
});
|
|
1207
|
-
|
|
1208
|
-
afterEach(() => {
|
|
1209
|
-
(getSection as ReturnType<typeof vi.fn>).mockReset();
|
|
1210
|
-
(findPageByPath as ReturnType<typeof vi.fn>).mockReset();
|
|
1211
|
-
(runSingleSectionLoader as ReturnType<typeof vi.fn>).mockReset();
|
|
1212
|
-
});
|
|
1213
|
-
|
|
1214
|
-
it("reExtractRawProps (cross-isolate cache miss) resolves with the caller's cookies/UA/url", async () => {
|
|
1215
|
-
// A cold isolate never populated the in-process rawProps Map, so this is
|
|
1216
|
-
// the branch that actually runs in production on Cloudflare Workers.
|
|
1217
|
-
const rawProps = await reExtractRawProps("/product/foo", "site/sections/Shelf.tsx", 0, CTX);
|
|
1218
|
-
|
|
1219
|
-
expect(rawProps).toMatchObject({ title: "Mais vendidos" });
|
|
1220
|
-
expect(seen).not.toHaveLength(0);
|
|
1221
|
-
for (const ctx of seen) {
|
|
1222
|
-
expect(ctx.cookies).toEqual(CTX.cookies);
|
|
1223
|
-
expect(ctx.userAgent).toBe(CTX.userAgent);
|
|
1224
|
-
expect(ctx.url).toBe(CTX.url);
|
|
1225
|
-
expect(ctx.request).toBe(CTX.request);
|
|
1226
|
-
}
|
|
1227
|
-
});
|
|
1228
|
-
|
|
1229
|
-
it("resolveDeferredSectionFull on a cold cache still resolves + enriches the section", async () => {
|
|
1230
|
-
const ds = {
|
|
1231
|
-
component: "site/sections/Shelf.tsx",
|
|
1232
|
-
index: 0,
|
|
1233
|
-
props: {},
|
|
1234
|
-
} as unknown as DeferredSection;
|
|
1235
|
-
|
|
1236
|
-
const section = await resolveDeferredSectionFull(ds, "/product/foo", CTX.request, CTX);
|
|
1237
|
-
|
|
1238
|
-
expect(section).not.toBeNull();
|
|
1239
|
-
expect(section?.component).toBe("site/sections/Shelf.tsx");
|
|
1240
|
-
expect(section?.index).toBe(0);
|
|
1241
|
-
// The deferred hop must run the section's own loader — this is what #277
|
|
1242
|
-
// reported as missing, and it is what makes the second hop equivalent to
|
|
1243
|
-
// eager resolution. Scope note: this asserts the request THIS function was
|
|
1244
|
-
// handed reaches the loader. `loadDeferredSection` in @decocms/tanstack
|
|
1245
|
-
// constructs its own `new Request(pageUrl || serverUrl, { headers })` before
|
|
1246
|
-
// calling in, so the fidelity of that reconstruction is a separate concern
|
|
1247
|
-
// and is not covered here.
|
|
1248
|
-
expect(runSingleSectionLoader).toHaveBeenCalled();
|
|
1249
|
-
const [, passedRequest] = (runSingleSectionLoader as ReturnType<typeof vi.fn>).mock
|
|
1250
|
-
.calls[0] as [unknown, Request];
|
|
1251
|
-
expect(passedRequest).toBe(CTX.request);
|
|
1252
|
-
});
|
|
1253
|
-
|
|
1254
|
-
it("a context-free second hop is observably different — guards against dropping matcherCtx", async () => {
|
|
1255
|
-
// If reExtractRawProps ever stops threading matcherCtx, cookie/UA-gated
|
|
1256
|
-
// variants silently resolve against an anonymous request. Assert the probe
|
|
1257
|
-
// can actually tell the two apart, so the test above is not vacuous.
|
|
1258
|
-
await reExtractRawProps("/product/foo", "site/sections/Shelf.tsx", 0, undefined);
|
|
1259
|
-
expect(seen).not.toHaveLength(0);
|
|
1260
|
-
expect(seen[0].cookies).toBeUndefined();
|
|
1261
|
-
expect(seen[0].userAgent).toBeUndefined();
|
|
1262
|
-
});
|
|
1263
|
-
});
|
|
1264
|
-
|
|
1265
|
-
// ---------------------------------------------------------------------------
|
|
1266
|
-
// Hidden array items — a `never`-gated multivariate wrapper must vanish from
|
|
1267
|
-
// the array, not survive as a `null` hole the section renders as empty.
|
|
1268
|
-
//
|
|
1269
|
-
// The admin (Studio) hides an array item by wrapping it in
|
|
1270
|
-
// `{ __resolveType: "website/flags/multivariate.ts", variants: [{ value, rule:
|
|
1271
|
-
// { __resolveType: "website/matchers/never.ts" } }] }`. `never` never matches,
|
|
1272
|
-
// so the wrapper resolves to "not present" and the item is dropped.
|
|
1273
|
-
// ---------------------------------------------------------------------------
|
|
1274
|
-
|
|
1275
|
-
describe("hidden array items (never matcher)", () => {
|
|
1276
|
-
const NEVER = "website/matchers/never.ts";
|
|
1277
|
-
const ALWAYS = "website/matchers/always.ts";
|
|
1278
|
-
const MULTIVARIATE = "website/flags/multivariate.ts";
|
|
1279
|
-
|
|
1280
|
-
const hidden = (value: unknown) => ({
|
|
1281
|
-
__resolveType: MULTIVARIATE,
|
|
1282
|
-
variants: [{ value, rule: { __resolveType: NEVER } }],
|
|
1283
|
-
});
|
|
1284
|
-
|
|
1285
|
-
it("drops a hidden item from an array instead of leaving a null hole", async () => {
|
|
1286
|
-
const benefits = [
|
|
1287
|
-
{ label: "Parcelamento" },
|
|
1288
|
-
{ label: "Frete" },
|
|
1289
|
-
hidden({ label: "RetiradaLoja" }),
|
|
1290
|
-
hidden({ label: "EntregaExpressa" }),
|
|
1291
|
-
];
|
|
1292
|
-
|
|
1293
|
-
const result = (await resolveValue({ benefits }, undefined, {})) as {
|
|
1294
|
-
benefits: unknown[];
|
|
1295
|
-
};
|
|
1296
|
-
|
|
1297
|
-
expect(result.benefits).toEqual([{ label: "Parcelamento" }, { label: "Frete" }]);
|
|
1298
|
-
// No null holes.
|
|
1299
|
-
expect(result.benefits).not.toContain(null);
|
|
1300
|
-
});
|
|
1301
|
-
|
|
1302
|
-
it("keeps a visible (always-gated) item in the array", async () => {
|
|
1303
|
-
const benefits = [
|
|
1304
|
-
{
|
|
1305
|
-
__resolveType: MULTIVARIATE,
|
|
1306
|
-
variants: [{ value: { label: "Shown" }, rule: { __resolveType: ALWAYS } }],
|
|
1307
|
-
},
|
|
1308
|
-
hidden({ label: "Hidden" }),
|
|
1309
|
-
];
|
|
1310
|
-
|
|
1311
|
-
const result = (await resolveValue({ benefits }, undefined, {})) as {
|
|
1312
|
-
benefits: unknown[];
|
|
1313
|
-
};
|
|
1314
|
-
|
|
1315
|
-
expect(result.benefits).toEqual([{ label: "Shown" }]);
|
|
1316
|
-
});
|
|
1317
|
-
|
|
1318
|
-
it("preserves a legitimate null value in an array (matched variant resolving to null)", async () => {
|
|
1319
|
-
// A matched variant whose value is literally null stays — only the
|
|
1320
|
-
// no-match sentinel is filtered, not every null.
|
|
1321
|
-
const items = [
|
|
1322
|
-
{
|
|
1323
|
-
__resolveType: MULTIVARIATE,
|
|
1324
|
-
variants: [{ value: null, rule: { __resolveType: ALWAYS } }],
|
|
1325
|
-
},
|
|
1326
|
-
{ label: "kept" },
|
|
1327
|
-
];
|
|
1328
|
-
|
|
1329
|
-
const result = (await resolveValue({ items }, undefined, {})) as { items: unknown[] };
|
|
1330
|
-
|
|
1331
|
-
expect(result.items).toEqual([null, { label: "kept" }]);
|
|
1332
|
-
});
|
|
1333
|
-
});
|
|
1334
|
-
|
|
1335
|
-
// ---------------------------------------------------------------------------
|
|
1336
|
-
// resolved-layout cache — device is part of the key
|
|
1337
|
-
// ---------------------------------------------------------------------------
|
|
1338
|
-
//
|
|
1339
|
-
// The key used to be the component path alone, so the first visitor's variant
|
|
1340
|
-
// was served to everyone for the whole TTL. A real store had to leave Header
|
|
1341
|
-
// and Footer OUT of registerLayoutSections because of it, and then re-resolved
|
|
1342
|
-
// both on every page and every navigation. These cases pin the axis; without
|
|
1343
|
-
// them a refactor could silently collapse the key again and the symptom (a
|
|
1344
|
-
// mobile header on desktop) only shows up in production, minutes later.
|
|
1345
|
-
|
|
1346
|
-
describe("layoutCacheKey — the resolved-layout cache is segmented by device", () => {
|
|
1347
|
-
const MOBILE_UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0) Mobile Safari";
|
|
1348
|
-
const TABLET_UA = "Mozilla/5.0 (iPad; CPU OS 17_0) Safari/605";
|
|
1349
|
-
const DESKTOP_UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) Chrome/120 Safari/537.36";
|
|
1350
|
-
const KEY = "site/sections/Header.tsx";
|
|
1351
|
-
|
|
1352
|
-
// The key used to be the component path alone. A site whose Header takes a
|
|
1353
|
-
// per-request device prop therefore served the first visitor's variant to
|
|
1354
|
-
// everyone for the whole 5-minute TTL — a mobile header on desktop. The only
|
|
1355
|
-
// escape was leaving Header/Footer out of `registerLayoutSections`, which is
|
|
1356
|
-
// what a real store did, and it then re-resolved both on every page and every
|
|
1357
|
-
// navigation.
|
|
1358
|
-
|
|
1359
|
-
it("mobile, tablet and desktop are three distinct keys", () => {
|
|
1360
|
-
const keys = [MOBILE_UA, TABLET_UA, DESKTOP_UA].map((ua) =>
|
|
1361
|
-
layoutCacheKey(KEY, { userAgent: ua }),
|
|
1362
|
-
);
|
|
1363
|
-
expect(new Set(keys).size).toBe(3);
|
|
1364
|
-
});
|
|
1365
|
-
|
|
1366
|
-
it("the same device yields the same key — the cache still works", () => {
|
|
1367
|
-
expect(layoutCacheKey(KEY, { userAgent: DESKTOP_UA })).toBe(
|
|
1368
|
-
layoutCacheKey(KEY, { userAgent: DESKTOP_UA }),
|
|
1369
|
-
);
|
|
1370
|
-
});
|
|
1371
|
-
|
|
1372
|
-
it("different components never share a key on the same device", () => {
|
|
1373
|
-
expect(layoutCacheKey("site/sections/Header.tsx", { userAgent: MOBILE_UA })).not.toBe(
|
|
1374
|
-
layoutCacheKey("site/sections/Footer.tsx", { userAgent: MOBILE_UA }),
|
|
1375
|
-
);
|
|
1376
|
-
});
|
|
1377
|
-
|
|
1378
|
-
it("a missing userAgent still produces a key (no crash, no undefined axis)", () => {
|
|
1379
|
-
const noCtx = layoutCacheKey(KEY);
|
|
1380
|
-
expect(noCtx).toContain(KEY);
|
|
1381
|
-
expect(noCtx).not.toContain("undefined");
|
|
1382
|
-
// An absent UA must land on the SAME bucket as a request whose UA is empty,
|
|
1383
|
-
// so a health check and a real desktop hit do not fragment the cache twice.
|
|
1384
|
-
expect(noCtx).toBe(layoutCacheKey(KEY, { userAgent: "" }));
|
|
1385
|
-
});
|
|
1386
|
-
});
|
|
1387
|
-
|
|
1388
|
-
// ---------------------------------------------------------------------------
|
|
1389
|
-
// asResolved / deferred props
|
|
1390
|
-
// ---------------------------------------------------------------------------
|
|
1391
|
-
//
|
|
1392
|
-
// The CMS resolver is recursive and resolves EVERY `__resolveType` it finds in a
|
|
1393
|
-
// section's props — including branches the section will not render. Measured on
|
|
1394
|
-
// a real store, a PDP's `notFoundSections` (the 404 fallback) carried a shelf
|
|
1395
|
-
// whose `products` was a full-text search: 1703ms of the eager critical path of
|
|
1396
|
-
// every product page, result thrown away. `asResolved(v, true)` is the opt-out.
|
|
1397
|
-
|
|
1398
|
-
describe("asResolved — deferred props", () => {
|
|
1399
|
-
const LOADER = "test/loaders/expensive.ts";
|
|
1400
|
-
let calls = 0;
|
|
1401
|
-
|
|
1402
|
-
beforeEach(() => {
|
|
1403
|
-
calls = 0;
|
|
1404
|
-
clearCommerceLoaders();
|
|
1405
|
-
registerCommerceLoader(LOADER, async () => {
|
|
1406
|
-
calls += 1;
|
|
1407
|
-
return { products: ["p1", "p2"] };
|
|
1408
|
-
});
|
|
1409
|
-
});
|
|
1410
|
-
|
|
1411
|
-
afterEach(() => clearCommerceLoaders());
|
|
1412
|
-
|
|
1413
|
-
const expensive = { __resolveType: LOADER };
|
|
1414
|
-
|
|
1415
|
-
it("without asResolved the loader runs during the page pass", async () => {
|
|
1416
|
-
const out = (await resolveValue({ fallback: expensive })) as any;
|
|
1417
|
-
expect(calls).toBe(1);
|
|
1418
|
-
expect(out.fallback).toEqual({ products: ["p1", "p2"] });
|
|
1419
|
-
});
|
|
1420
|
-
|
|
1421
|
-
it("asResolved(v) hands the value back untouched — the loader never runs", async () => {
|
|
1422
|
-
const out = (await resolveValue({ fallback: asResolved(expensive) })) as any;
|
|
1423
|
-
expect(calls).toBe(0);
|
|
1424
|
-
// The raw node comes through, NOT its resolution.
|
|
1425
|
-
expect(out.fallback).toEqual(expensive);
|
|
1426
|
-
});
|
|
1427
|
-
|
|
1428
|
-
it("asResolved(v, true) yields a thunk and does NOT resolve until called", async () => {
|
|
1429
|
-
const out = (await resolveValue({ fallback: asResolved(expensive, true) })) as any;
|
|
1430
|
-
|
|
1431
|
-
expect(calls).toBe(0);
|
|
1432
|
-
expect(isDeferred(out.fallback)).toBe(true);
|
|
1433
|
-
|
|
1434
|
-
expect(await resolveDeferred(out.fallback)).toEqual({ products: ["p1", "p2"] });
|
|
1435
|
-
expect(calls).toBe(1);
|
|
1436
|
-
});
|
|
1437
|
-
|
|
1438
|
-
it("the branch that never calls the thunk pays nothing", async () => {
|
|
1439
|
-
const out = (await resolveValue({
|
|
1440
|
-
shown: expensive,
|
|
1441
|
-
fallback: asResolved(expensive, true),
|
|
1442
|
-
})) as any;
|
|
1443
|
-
// Only the rendered branch resolved. This is the whole point: 1 upstream
|
|
1444
|
-
// call instead of 2 for a page that renders one of the two.
|
|
1445
|
-
expect(calls).toBe(1);
|
|
1446
|
-
expect(out.shown).toEqual({ products: ["p1", "p2"] });
|
|
1447
|
-
});
|
|
1448
|
-
|
|
1449
|
-
it("a thunk is dropped by JSON.stringify — the unused branch leaves the payload too", async () => {
|
|
1450
|
-
const out = (await resolveValue({ fallback: asResolved(expensive, true) })) as any;
|
|
1451
|
-
expect(JSON.parse(JSON.stringify(out))).toEqual({});
|
|
1452
|
-
});
|
|
1453
|
-
|
|
1454
|
-
it("resolveDeferred is tolerant of a plain value (admin preview path)", async () => {
|
|
1455
|
-
// The admin preview does not run `onBeforeResolveProps`, so the prop arrives
|
|
1456
|
-
// already resolved. A section must read the same either way.
|
|
1457
|
-
expect(await resolveDeferred([{ a: 1 }])).toEqual([{ a: 1 }]);
|
|
1458
|
-
expect(await resolveDeferred(undefined)).toBeUndefined();
|
|
1459
|
-
});
|
|
1460
|
-
|
|
1461
|
-
it("the thunk resolves in THIS request's context, not a context-free one", async () => {
|
|
1462
|
-
// The invariant that makes deferring safe: a cookie/UA-gated prop must not
|
|
1463
|
-
// silently resolve against an anonymous request when the section calls it.
|
|
1464
|
-
const PROBE = "test/matchers/deferredProbe.ts";
|
|
1465
|
-
const seen: MatcherContext[] = [];
|
|
1466
|
-
registerMatcher(PROBE, (_rule, ctx) => {
|
|
1467
|
-
seen.push(ctx);
|
|
1468
|
-
return true;
|
|
1469
|
-
});
|
|
1470
|
-
const gated = {
|
|
1471
|
-
__resolveType: WELL_KNOWN_TYPES.MULTIVARIATE,
|
|
1472
|
-
variants: [{ rule: { __resolveType: PROBE }, value: expensive }],
|
|
1473
|
-
};
|
|
1474
|
-
const ctx: MatcherContext = {
|
|
1475
|
-
userAgent: "Mozilla/5.0 (iPhone) Mobile Safari",
|
|
1476
|
-
url: "https://store.com/x/p",
|
|
1477
|
-
cookies: { VtexIdclientAutCookie: "tok" },
|
|
1478
|
-
};
|
|
1479
|
-
|
|
1480
|
-
const out = (await resolveValue({ fallback: asResolved(gated, true) }, undefined, ctx)) as any;
|
|
1481
|
-
expect(seen).toHaveLength(0); // nothing evaluated yet
|
|
1482
|
-
|
|
1483
|
-
await resolveDeferred(out.fallback);
|
|
1484
|
-
|
|
1485
|
-
expect(seen).not.toHaveLength(0);
|
|
1486
|
-
for (const c of seen) {
|
|
1487
|
-
expect(c.cookies).toEqual(ctx.cookies);
|
|
1488
|
-
expect(c.userAgent).toBe(ctx.userAgent);
|
|
1489
|
-
expect(c.url).toBe(ctx.url);
|
|
1490
|
-
}
|
|
1491
|
-
});
|
|
1492
|
-
});
|