@decocms/tanstack 8.1.0-next.0 → 8.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -0
- package/dist/draft.d.ts +33 -0
- package/dist/draft.d.ts.map +1 -0
- package/dist/draft.js +41 -0
- package/dist/draft.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/react/DecoDocument.d.ts +61 -0
- package/dist/react/DecoDocument.d.ts.map +1 -0
- package/dist/react/DecoDocument.js +96 -0
- package/dist/react/DecoDocument.js.map +1 -0
- package/dist/react/Image.d.ts +96 -0
- package/dist/react/Image.d.ts.map +1 -0
- package/dist/react/Image.js +79 -0
- package/dist/react/Image.js.map +1 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/pageValues.d.ts +10 -0
- package/dist/react/pageValues.d.ts.map +1 -0
- package/dist/react/pageValues.js +29 -0
- package/dist/react/pageValues.js.map +1 -0
- package/dist/react/scripts.d.ts +14 -0
- package/dist/react/scripts.d.ts.map +1 -0
- package/dist/react/scripts.js +96 -0
- package/dist/react/scripts.js.map +1 -0
- package/dist/router/index.d.ts +38 -0
- package/dist/router/index.d.ts.map +1 -0
- package/dist/router/index.js +60 -0
- package/dist/router/index.js.map +1 -0
- package/dist/scope/browser.d.ts +11 -0
- package/dist/scope/browser.d.ts.map +1 -0
- package/dist/scope/browser.js +8 -0
- package/dist/scope/browser.js.map +1 -0
- package/dist/scope/server.d.ts +13 -0
- package/dist/scope/server.d.ts.map +1 -0
- package/dist/scope/server.js +26 -0
- package/dist/scope/server.js.map +1 -0
- package/dist/server/index.d.ts +27 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +31 -0
- package/dist/server/index.js.map +1 -0
- package/dist/shared.d.ts +26 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +26 -0
- package/dist/shared.js.map +1 -0
- package/dist/start/index.d.ts +9 -0
- package/dist/start/index.d.ts.map +1 -0
- package/dist/start/index.js +33 -0
- package/dist/start/index.js.map +1 -0
- package/dist/studio.d.ts +28 -0
- package/dist/studio.d.ts.map +1 -0
- package/dist/studio.js +53 -0
- package/dist/studio.js.map +1 -0
- package/dist/vite/index.d.ts +15 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +95 -0
- package/dist/vite/index.js.map +1 -0
- package/dist/worker/cacheProfiles.d.ts +43 -0
- package/dist/worker/cacheProfiles.d.ts.map +1 -0
- package/dist/worker/cacheProfiles.js +162 -0
- package/dist/worker/cacheProfiles.js.map +1 -0
- package/dist/worker/cookies.d.ts +20 -0
- package/dist/worker/cookies.d.ts.map +1 -0
- package/dist/worker/cookies.js +69 -0
- package/dist/worker/cookies.js.map +1 -0
- package/dist/worker/drafts.d.ts +59 -0
- package/dist/worker/drafts.d.ts.map +1 -0
- package/dist/worker/drafts.js +140 -0
- package/dist/worker/drafts.js.map +1 -0
- package/dist/worker/index.d.ts +111 -0
- package/dist/worker/index.d.ts.map +1 -0
- package/dist/worker/index.js +618 -0
- package/dist/worker/index.js.map +1 -0
- package/dist/worker/security.d.ts +14 -0
- package/dist/worker/security.d.ts.map +1 -0
- package/dist/worker/security.js +48 -0
- package/dist/worker/security.js.map +1 -0
- package/dist/worker/segment.d.ts +21 -0
- package/dist/worker/segment.d.ts.map +1 -0
- package/dist/worker/segment.js +43 -0
- package/dist/worker/segment.js.map +1 -0
- package/package.json +85 -37
- package/src/draft.ts +43 -0
- package/src/index.ts +8 -61
- package/src/react/DecoDocument.tsx +234 -0
- package/src/react/Image.tsx +213 -0
- package/src/react/index.ts +24 -0
- package/src/react/pageValues.ts +35 -0
- package/src/react/scripts.ts +98 -0
- package/src/router/index.ts +80 -0
- package/src/scope/browser.ts +18 -0
- package/src/scope/server.ts +37 -0
- package/src/server/index.ts +43 -0
- package/src/shared.ts +29 -0
- package/src/start/index.ts +31 -0
- package/src/studio.ts +52 -0
- package/src/vite/index.ts +105 -0
- package/src/worker/cacheProfiles.ts +180 -0
- package/src/worker/cookies.ts +65 -0
- package/src/worker/drafts.ts +157 -0
- package/src/worker/index.ts +784 -0
- package/src/worker/security.ts +63 -0
- package/src/worker/segment.ts +49 -0
- package/src/__conformance__/observability.test.ts +0 -221
- package/src/cms/kvBlockSource.test.ts +0 -82
- package/src/cms/kvBlockSource.ts +0 -68
- package/src/daemon/auth.ts +0 -204
- package/src/daemon/fs.ts +0 -238
- package/src/daemon/index.ts +0 -8
- package/src/daemon/middleware.ts +0 -156
- package/src/daemon/tunnel.ts +0 -129
- package/src/daemon/volumes.ts +0 -366
- package/src/daemon/watch.ts +0 -272
- package/src/hooks/CdnSegmentMarker.tsx +0 -38
- package/src/hooks/DecoPageRenderer.tsx +0 -750
- package/src/hooks/DecoRootLayout.tsx +0 -192
- package/src/hooks/DraftPreviewIndicator.test.tsx +0 -60
- package/src/hooks/DraftPreviewIndicator.tsx +0 -64
- package/src/hooks/NavigationProgress.test.tsx +0 -58
- package/src/hooks/NavigationProgress.tsx +0 -63
- package/src/hooks/PreviewProviders.test.tsx +0 -42
- package/src/hooks/PreviewProviders.tsx +0 -45
- package/src/hooks/StableOutlet.tsx +0 -30
- package/src/hooks/deferredObserverTiming.test.tsx +0 -91
- package/src/hooks/index.ts +0 -10
- package/src/routes/adminRoutes.test.ts +0 -99
- package/src/routes/adminRoutes.ts +0 -153
- package/src/routes/cmsRoute.test.ts +0 -267
- package/src/routes/cmsRoute.ts +0 -1053
- package/src/routes/components.tsx +0 -57
- package/src/routes/index.ts +0 -24
- package/src/routes/pageUrl.test.ts +0 -70
- package/src/routes/pageUrl.ts +0 -55
- package/src/routes/withSiteGlobals.test.ts +0 -243
- package/src/routes/withSiteGlobals.ts +0 -243
- package/src/sdk/backgroundTasks.test.ts +0 -68
- package/src/sdk/backgroundTasks.ts +0 -38
- package/src/sdk/cdnSegment.test.ts +0 -67
- package/src/sdk/cdnSegment.ts +0 -71
- package/src/sdk/cookiePassthrough.ts +0 -58
- package/src/sdk/createInvoke.ts +0 -77
- package/src/sdk/deferredSectionLoader.test.ts +0 -94
- package/src/sdk/deferredSectionLoader.ts +0 -46
- package/src/sdk/draft.test.ts +0 -341
- package/src/sdk/draft.ts +0 -277
- package/src/sdk/draftShared.ts +0 -16
- package/src/sdk/kvHydration.test.ts +0 -371
- package/src/sdk/kvHydration.ts +0 -263
- package/src/sdk/kvRedirects.test.ts +0 -132
- package/src/sdk/kvRedirects.ts +0 -125
- package/src/sdk/outboundHeaders.test.ts +0 -113
- package/src/sdk/outboundHeaders.ts +0 -81
- package/src/sdk/router.ts +0 -101
- package/src/sdk/serverFnFetch.ts +0 -59
- package/src/sdk/speculationRules.test.ts +0 -82
- package/src/sdk/speculationRules.ts +0 -136
- package/src/sdk/startEntry.ts +0 -31
- package/src/sdk/useHydrated.ts +0 -19
- package/src/sdk/workerEntry.cacheStorage.test.ts +0 -147
- package/src/sdk/workerEntry.test.ts +0 -1033
- package/src/sdk/workerEntry.trackingCache.test.ts +0 -328
- package/src/sdk/workerEntry.ts +0 -3154
- package/src/setupFastDeploy.ts +0 -13
- package/src/v8/__conformance__/delivery.test.ts +0 -100
- package/src/v8/binding.ts +0 -70
- package/src/v8/kvLoader.bundle.test.ts +0 -42
- package/src/v8/kvLoader.test.ts +0 -74
- package/src/v8/kvLoader.ts +0 -43
- package/src/vite/plugin-scaffold-gen.test.js +0 -106
- package/src/vite/plugin.js +0 -1022
- package/src/vite/plugin.test.js +0 -238
- package/src/vite/plugin.test.ts +0 -176
- package/tsconfig.json +0 -7
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Contract test for the public `deferredSectionLoader` wrapper
|
|
3
|
-
* (`@decocms/tanstack/sdk/deferredSectionLoader`).
|
|
4
|
-
*
|
|
5
|
-
* Before 7.7 this wrapper was unreachable from any public subpath, so
|
|
6
|
-
* migrated production storefronts each carried a
|
|
7
|
-
* byte-identical local shim wrapping the public `loadDeferredSection`
|
|
8
|
-
* export. The cases below are derived from how those sites call it: it is
|
|
9
|
-
* passed verbatim as `<DecoPageRenderer loadDeferredSectionFn={...} />`,
|
|
10
|
-
* so it must (a) accept the renderer's flat argument object, (b) wrap it
|
|
11
|
-
* into the server function's `{ data }` envelope, and (c) pass the result
|
|
12
|
-
* (section or null) straight through.
|
|
13
|
-
*/
|
|
14
|
-
import type { ComponentProps } from "react";
|
|
15
|
-
import { beforeEach, describe, expect, expectTypeOf, it, vi } from "vitest";
|
|
16
|
-
|
|
17
|
-
vi.mock("../routes/cmsRoute", () => ({
|
|
18
|
-
loadDeferredSection: vi.fn(),
|
|
19
|
-
}));
|
|
20
|
-
|
|
21
|
-
import type { DecoPageRenderer } from "../hooks/DecoPageRenderer";
|
|
22
|
-
import { loadDeferredSection } from "../routes/cmsRoute";
|
|
23
|
-
import { deferredSectionLoader } from "./deferredSectionLoader";
|
|
24
|
-
|
|
25
|
-
const mockedLoad = loadDeferredSection as unknown as ReturnType<typeof vi.fn>;
|
|
26
|
-
|
|
27
|
-
describe("deferredSectionLoader", () => {
|
|
28
|
-
beforeEach(() => {
|
|
29
|
-
mockedLoad.mockReset();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it("wraps the flat argument object into the server function's { data } envelope", async () => {
|
|
33
|
-
const section = { component: "site/sections/Newsletter.tsx", props: {} };
|
|
34
|
-
mockedLoad.mockResolvedValue(section);
|
|
35
|
-
|
|
36
|
-
const result = await deferredSectionLoader({
|
|
37
|
-
component: "site/sections/Newsletter.tsx",
|
|
38
|
-
rawProps: { title: "Hi" },
|
|
39
|
-
pagePath: "/",
|
|
40
|
-
pageUrl: "https://example.com/?utm=x",
|
|
41
|
-
index: 4,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
expect(mockedLoad).toHaveBeenCalledTimes(1);
|
|
45
|
-
expect(mockedLoad).toHaveBeenCalledWith({
|
|
46
|
-
data: {
|
|
47
|
-
component: "site/sections/Newsletter.tsx",
|
|
48
|
-
rawProps: { title: "Hi" },
|
|
49
|
-
pagePath: "/",
|
|
50
|
-
pageUrl: "https://example.com/?utm=x",
|
|
51
|
-
index: 4,
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
expect(result).toBe(section);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it("accepts the minimal call shape (component + pagePath only)", async () => {
|
|
58
|
-
mockedLoad.mockResolvedValue(null);
|
|
59
|
-
|
|
60
|
-
await deferredSectionLoader({
|
|
61
|
-
component: "site/sections/Footer.tsx",
|
|
62
|
-
pagePath: "/collections/sale",
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
expect(mockedLoad).toHaveBeenCalledWith({
|
|
66
|
-
data: {
|
|
67
|
-
component: "site/sections/Footer.tsx",
|
|
68
|
-
rawProps: undefined,
|
|
69
|
-
pagePath: "/collections/sale",
|
|
70
|
-
pageUrl: undefined,
|
|
71
|
-
index: undefined,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
it("passes a null resolution (cache miss / unresolvable section) straight through", async () => {
|
|
77
|
-
mockedLoad.mockResolvedValue(null);
|
|
78
|
-
|
|
79
|
-
const result = await deferredSectionLoader({
|
|
80
|
-
component: "site/sections/Missing.tsx",
|
|
81
|
-
pagePath: "/",
|
|
82
|
-
index: 0,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
expect(result).toBeNull();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("matches DecoPageRenderer's loadDeferredSectionFn prop signature", () => {
|
|
89
|
-
// The whole point of the export: sites pass it verbatim as
|
|
90
|
-
// `<DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />`.
|
|
91
|
-
type LoadFn = NonNullable<ComponentProps<typeof DecoPageRenderer>["loadDeferredSectionFn"]>;
|
|
92
|
-
expectTypeOf(deferredSectionLoader).toExtend<LoadFn>();
|
|
93
|
-
});
|
|
94
|
-
});
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pre-wrapped deferred section loader for IntersectionObserver-based
|
|
3
|
-
* rendering.
|
|
4
|
-
*
|
|
5
|
-
* Convenience wrapper around `loadDeferredSection` (the POST server
|
|
6
|
-
* function defined in `../routes/cmsRoute.ts`) that matches the
|
|
7
|
-
* `loadDeferredSectionFn` prop signature of `DecoPageRenderer`.
|
|
8
|
-
*
|
|
9
|
-
* Pass this directly to
|
|
10
|
-
* `<DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />`
|
|
11
|
-
* to enable IntersectionObserver-based lazy loading of deferred sections.
|
|
12
|
-
*
|
|
13
|
-
* Publicly exported at `@decocms/tanstack/sdk/deferredSectionLoader`.
|
|
14
|
-
* Before 7.7 this wrapper existed only inside the package (exported from
|
|
15
|
-
* the internal `./routes` barrel but reachable from no public subpath), so
|
|
16
|
-
* migrated sites each carried a byte-identical local shim re-implementing
|
|
17
|
-
* it against the public `loadDeferredSection` export. Those shims can be
|
|
18
|
-
* deleted in favor of this subpath.
|
|
19
|
-
*
|
|
20
|
-
* @example Site's `src/routes/$.tsx`:
|
|
21
|
-
* ```tsx
|
|
22
|
-
* import { deferredSectionLoader } from "@decocms/tanstack/sdk/deferredSectionLoader";
|
|
23
|
-
*
|
|
24
|
-
* <DecoPageRenderer loadDeferredSectionFn={deferredSectionLoader} />
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
import type { ResolvedSection } from "@decocms/blocks/cms";
|
|
28
|
-
import { loadDeferredSection } from "../routes/cmsRoute";
|
|
29
|
-
|
|
30
|
-
export const deferredSectionLoader = async ({
|
|
31
|
-
component,
|
|
32
|
-
rawProps,
|
|
33
|
-
pagePath,
|
|
34
|
-
pageUrl,
|
|
35
|
-
index,
|
|
36
|
-
}: {
|
|
37
|
-
component: string;
|
|
38
|
-
rawProps?: Record<string, unknown>;
|
|
39
|
-
pagePath: string;
|
|
40
|
-
pageUrl?: string;
|
|
41
|
-
index?: number;
|
|
42
|
-
}): Promise<ResolvedSection | null> => {
|
|
43
|
-
return loadDeferredSection({
|
|
44
|
-
data: { component, rawProps, pagePath, pageUrl, index },
|
|
45
|
-
});
|
|
46
|
-
};
|
package/src/sdk/draft.test.ts
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
clearDraftCache,
|
|
3
|
-
getRequestDraftOverride,
|
|
4
|
-
isDraftPreviewEnabled,
|
|
5
|
-
setDecoSiteHost,
|
|
6
|
-
setDraftPreviewHosts,
|
|
7
|
-
} from "@decocms/blocks/cms";
|
|
8
|
-
import { RequestContext } from "@decocms/blocks/sdk/requestContext";
|
|
9
|
-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
10
|
-
|
|
11
|
-
import {
|
|
12
|
-
applyDraftCookieAndHeaders,
|
|
13
|
-
bindRequestDraft,
|
|
14
|
-
type DraftDecision,
|
|
15
|
-
installDecoSiteHostFromEnv,
|
|
16
|
-
installPreviewHostsFromBlocks,
|
|
17
|
-
registerDraftOverride,
|
|
18
|
-
requestCarriesDraft,
|
|
19
|
-
} from "./draft";
|
|
20
|
-
import { DRAFT_POINTER_BAG_KEY } from "./draftShared";
|
|
21
|
-
|
|
22
|
-
const HOST = "preview.example";
|
|
23
|
-
const DRAFT_BLOCKS = { "pages-home": { title: "draft" } };
|
|
24
|
-
|
|
25
|
-
function req(
|
|
26
|
-
opts: { url?: string; host?: string; cookie?: string } = {},
|
|
27
|
-
): Request {
|
|
28
|
-
const headers = new Headers();
|
|
29
|
-
headers.set("host", opts.host ?? HOST);
|
|
30
|
-
if (opts.cookie) headers.set("cookie", opts.cookie);
|
|
31
|
-
return new Request(opts.url ?? `https://${opts.host ?? HOST}/some/page`, {
|
|
32
|
-
headers,
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** A fetch stub serving the sandbox decofile; throws if `fail`. */
|
|
37
|
-
function sandboxFetch(fail = false): typeof fetch {
|
|
38
|
-
return (async () => {
|
|
39
|
-
if (fail) throw new Error("unreachable");
|
|
40
|
-
return new Response(JSON.stringify(DRAFT_BLOCKS), {
|
|
41
|
-
status: 200,
|
|
42
|
-
headers: { "content-type": "application/json" },
|
|
43
|
-
});
|
|
44
|
-
}) as unknown as typeof fetch;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
beforeEach(() => {
|
|
48
|
-
clearDraftCache();
|
|
49
|
-
// Allowlist is globalThis-backed (the site block's previewHosts), so no env
|
|
50
|
-
// needed — this makes the feature "enabled" for the tests below.
|
|
51
|
-
setDraftPreviewHosts([HOST, "localhost:3100"]);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
afterEach(() => {
|
|
55
|
-
setDraftPreviewHosts([]);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
describe("requestCarriesDraft", () => {
|
|
59
|
-
it("is inert when no allowlist is configured", () => {
|
|
60
|
-
setDraftPreviewHosts([]);
|
|
61
|
-
const url = new URL(
|
|
62
|
-
`https://${HOST}/p?__draft=abc.preview-studio.decocms.com@v1`,
|
|
63
|
-
);
|
|
64
|
-
expect(requestCarriesDraft(req({ url: url.toString() }), url)).toBe(false);
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it("detects the ?__draft= entry param", () => {
|
|
68
|
-
const url = new URL(
|
|
69
|
-
`https://${HOST}/p?__draft=abc.preview-studio.decocms.com@v1`,
|
|
70
|
-
);
|
|
71
|
-
expect(requestCarriesDraft(req({ url: url.toString() }), url)).toBe(true);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("detects the navigation cookie (SPA nav after entry)", () => {
|
|
75
|
-
const url = new URL(`https://${HOST}/p`);
|
|
76
|
-
expect(
|
|
77
|
-
requestCarriesDraft(
|
|
78
|
-
req({ url: url.toString(), cookie: "__deco_draft=abc.x@v1" }),
|
|
79
|
-
url,
|
|
80
|
-
),
|
|
81
|
-
).toBe(true);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("is false on ?__draft=off (leaving preview)", () => {
|
|
85
|
-
const url = new URL(`https://${HOST}/p?__draft=off`);
|
|
86
|
-
expect(
|
|
87
|
-
requestCarriesDraft(
|
|
88
|
-
req({ url: url.toString(), cookie: "__deco_draft=abc.x@v1" }),
|
|
89
|
-
url,
|
|
90
|
-
),
|
|
91
|
-
).toBe(false);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("is structurally inert on a non-preview host, param or not", () => {
|
|
95
|
-
const url = new URL("https://prod.example/p?__draft=abc.x@v1");
|
|
96
|
-
expect(
|
|
97
|
-
requestCarriesDraft(
|
|
98
|
-
req({ url: url.toString(), host: "prod.example" }),
|
|
99
|
-
url,
|
|
100
|
-
),
|
|
101
|
-
).toBe(false);
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
describe("bindRequestDraft", () => {
|
|
106
|
-
it("binds the resolved draft to the request bag on entry", async () => {
|
|
107
|
-
registerDraftOverride();
|
|
108
|
-
const pointer = "studio.decocms.com/api/o/decofile/m/main?token=t@v1";
|
|
109
|
-
const request = req({ url: `https://${HOST}/p?__draft=${pointer}` });
|
|
110
|
-
|
|
111
|
-
const decision = await RequestContext.run(request, async () => {
|
|
112
|
-
const d = await bindRequestDraft(request, sandboxFetch());
|
|
113
|
-
// loadBlocks() reads this back via the DI getter registered above.
|
|
114
|
-
expect(getRequestDraftOverride()).toEqual(DRAFT_BLOCKS);
|
|
115
|
-
return d;
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
expect(decision).toEqual({
|
|
119
|
-
previewing: true,
|
|
120
|
-
setCookie: pointer,
|
|
121
|
-
clearCookie: false,
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it("carries the draft across navigation via the cookie (no re-set)", async () => {
|
|
126
|
-
registerDraftOverride();
|
|
127
|
-
const pointer = "abc.preview-studio.decocms.com@v1";
|
|
128
|
-
const request = req({
|
|
129
|
-
url: `https://${HOST}/other`,
|
|
130
|
-
cookie: `__deco_draft=${pointer}`,
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const decision = await RequestContext.run(request, () =>
|
|
134
|
-
bindRequestDraft(request, sandboxFetch()),
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
// previewing, but setCookie is null — the cookie is already present.
|
|
138
|
-
expect(decision).toEqual({
|
|
139
|
-
previewing: true,
|
|
140
|
-
setCookie: null,
|
|
141
|
-
clearCookie: false,
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
it("decodes a URL-encoded cookie so the badge pointer round-trips (no double-encode)", async () => {
|
|
146
|
-
// The cookie is written with encodeURIComponent (`:`→%3A, `@`→%40); the
|
|
147
|
-
// pointer stashed for the badge must be the DECODED value, or the badge's
|
|
148
|
-
// share link would re-encode an already-encoded string.
|
|
149
|
-
registerDraftOverride();
|
|
150
|
-
const decoded = "abc.localhost:60534/api/o/decofile/m/main?token=t@v7";
|
|
151
|
-
const request = req({
|
|
152
|
-
url: `https://${HOST}/other`,
|
|
153
|
-
cookie: `__deco_draft=${encodeURIComponent(decoded)}`,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
const stored = await RequestContext.run(request, async () => {
|
|
157
|
-
await bindRequestDraft(request, sandboxFetch());
|
|
158
|
-
return RequestContext.getBag<string>(DRAFT_POINTER_BAG_KEY);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
expect(stored).toBe(decoded);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it("stays previewing (uncacheable) even when the draft fails to resolve", async () => {
|
|
165
|
-
const request = req({
|
|
166
|
-
url: `https://${HOST}/p?__draft=abc.preview-studio.decocms.com@v1`,
|
|
167
|
-
});
|
|
168
|
-
const decision = await RequestContext.run(request, () =>
|
|
169
|
-
bindRequestDraft(request, sandboxFetch(true)),
|
|
170
|
-
);
|
|
171
|
-
expect(decision.previewing).toBe(true);
|
|
172
|
-
expect(decision.setCookie).toBe("abc.preview-studio.decocms.com@v1");
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it("clears the cookie on ?__draft=off", async () => {
|
|
176
|
-
const request = req({
|
|
177
|
-
url: `https://${HOST}/p?__draft=off`,
|
|
178
|
-
cookie: "__deco_draft=x@v1",
|
|
179
|
-
});
|
|
180
|
-
const decision = await RequestContext.run(request, () =>
|
|
181
|
-
bindRequestDraft(request),
|
|
182
|
-
);
|
|
183
|
-
expect(decision).toEqual({
|
|
184
|
-
previewing: false,
|
|
185
|
-
setCookie: null,
|
|
186
|
-
clearCookie: true,
|
|
187
|
-
});
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
it("is inert on a non-preview host", async () => {
|
|
191
|
-
const request = req({
|
|
192
|
-
url: "https://prod.example/p?__draft=abc.x@v1",
|
|
193
|
-
host: "prod.example",
|
|
194
|
-
});
|
|
195
|
-
const decision = await RequestContext.run(request, () =>
|
|
196
|
-
bindRequestDraft(request),
|
|
197
|
-
);
|
|
198
|
-
expect(decision).toEqual({
|
|
199
|
-
previewing: false,
|
|
200
|
-
setCookie: null,
|
|
201
|
-
clearCookie: false,
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
describe("applyDraftCookieAndHeaders", () => {
|
|
207
|
-
const entry: DraftDecision = {
|
|
208
|
-
previewing: true,
|
|
209
|
-
setCookie: "abc@v1",
|
|
210
|
-
clearCookie: false,
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
it("sets a cross-site-iframe-safe cookie and the anti-leak headers on entry", () => {
|
|
214
|
-
const res = new Response("ok");
|
|
215
|
-
applyDraftCookieAndHeaders(res, entry);
|
|
216
|
-
|
|
217
|
-
const cookie = res.headers.get("set-cookie") ?? "";
|
|
218
|
-
expect(cookie).toContain("__deco_draft=abc%40v1"); // pointer is URL-encoded
|
|
219
|
-
expect(cookie).toContain("SameSite=None");
|
|
220
|
-
expect(cookie).toContain("Secure");
|
|
221
|
-
expect(cookie).toContain("Partitioned");
|
|
222
|
-
expect(cookie).toContain("HttpOnly");
|
|
223
|
-
|
|
224
|
-
expect(res.headers.get("cache-control")).toBe("no-store, private");
|
|
225
|
-
expect(res.headers.get("vary")).toBe("Cookie");
|
|
226
|
-
expect(res.headers.get("x-robots-tag")).toBe("noindex, nofollow");
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it("expires the cookie when leaving preview", () => {
|
|
230
|
-
const res = new Response("ok");
|
|
231
|
-
applyDraftCookieAndHeaders(res, {
|
|
232
|
-
previewing: false,
|
|
233
|
-
setCookie: null,
|
|
234
|
-
clearCookie: true,
|
|
235
|
-
});
|
|
236
|
-
expect(res.headers.get("set-cookie") ?? "").toContain("Max-Age=0");
|
|
237
|
-
// Not previewing → no cache/index headers.
|
|
238
|
-
expect(res.headers.get("cache-control")).toBeNull();
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
it("merges Cookie into an existing Vary instead of clobbering it", () => {
|
|
242
|
-
const res = new Response("ok", { headers: { Vary: "Accept-Encoding" } });
|
|
243
|
-
applyDraftCookieAndHeaders(res, {
|
|
244
|
-
previewing: true,
|
|
245
|
-
setCookie: null,
|
|
246
|
-
clearCookie: false,
|
|
247
|
-
});
|
|
248
|
-
expect(res.headers.get("vary")).toBe("Accept-Encoding, Cookie");
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
it("leaves a Vary that already lists Cookie untouched (no token dropped)", () => {
|
|
252
|
-
const res = new Response("ok", {
|
|
253
|
-
headers: { Vary: "Accept-Encoding, Cookie" },
|
|
254
|
-
});
|
|
255
|
-
applyDraftCookieAndHeaders(res, {
|
|
256
|
-
previewing: true,
|
|
257
|
-
setCookie: null,
|
|
258
|
-
clearCookie: false,
|
|
259
|
-
});
|
|
260
|
-
expect(res.headers.get("vary")).toBe("Accept-Encoding, Cookie");
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it("does nothing for an ordinary (non-draft) response", () => {
|
|
264
|
-
const res = new Response("ok");
|
|
265
|
-
applyDraftCookieAndHeaders(res, {
|
|
266
|
-
previewing: false,
|
|
267
|
-
setCookie: null,
|
|
268
|
-
clearCookie: false,
|
|
269
|
-
});
|
|
270
|
-
expect(res.headers.get("set-cookie")).toBeNull();
|
|
271
|
-
expect(res.headers.get("cache-control")).toBeNull();
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
describe("installPreviewHostsFromBlocks", () => {
|
|
276
|
-
it("installs previewHosts from the site block (either casing)", async () => {
|
|
277
|
-
setDraftPreviewHosts([]);
|
|
278
|
-
installPreviewHostsFromBlocks({
|
|
279
|
-
Site: { previewHosts: ["acme.vtex.app"] },
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
// Verified through the wire: an allowed host now previews.
|
|
283
|
-
const request = req({
|
|
284
|
-
url: "https://acme.vtex.app/p?__draft=abc.preview-studio.decocms.com@v1",
|
|
285
|
-
host: "acme.vtex.app",
|
|
286
|
-
});
|
|
287
|
-
const url = new URL(request.url);
|
|
288
|
-
expect(requestCarriesDraft(request, url)).toBe(true);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
it("ignores blocks without previewHosts", () => {
|
|
292
|
-
setDraftPreviewHosts([]);
|
|
293
|
-
installPreviewHostsFromBlocks({ site: {} });
|
|
294
|
-
const url = new URL("https://acme.vtex.app/p?__draft=x@v1");
|
|
295
|
-
expect(
|
|
296
|
-
requestCarriesDraft(
|
|
297
|
-
req({ url: url.toString(), host: "acme.vtex.app" }),
|
|
298
|
-
url,
|
|
299
|
-
),
|
|
300
|
-
).toBe(false);
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
|
|
304
|
-
describe("installDecoSiteHostFromEnv", () => {
|
|
305
|
-
afterEach(() => {
|
|
306
|
-
setDecoSiteHost(null);
|
|
307
|
-
});
|
|
308
|
-
|
|
309
|
-
it("arms the deco-hosted domains from the DECO_SITE_NAME binding", () => {
|
|
310
|
-
setDraftPreviewHosts([]);
|
|
311
|
-
installDecoSiteHostFromEnv({ DECO_SITE_NAME: "acme-tanstack" });
|
|
312
|
-
|
|
313
|
-
// Verified through the wire: both deco-operated hosts now preview, with no
|
|
314
|
-
// site-block/env config at all.
|
|
315
|
-
for (const host of [
|
|
316
|
-
"acme-tanstack.deco.site",
|
|
317
|
-
"acme-tanstack.deco-cx.workers.dev",
|
|
318
|
-
]) {
|
|
319
|
-
const url = new URL(
|
|
320
|
-
`https://${host}/p?__draft=abc.preview-studio.decocms.com@v1`,
|
|
321
|
-
);
|
|
322
|
-
expect(requestCarriesDraft(req({ url: url.toString(), host }), url)).toBe(true);
|
|
323
|
-
}
|
|
324
|
-
// A custom production domain is never inferred.
|
|
325
|
-
const url = new URL("https://www.acme.com.br/p?__draft=x@v1");
|
|
326
|
-
expect(
|
|
327
|
-
requestCarriesDraft(
|
|
328
|
-
req({ url: url.toString(), host: "www.acme.com.br" }),
|
|
329
|
-
url,
|
|
330
|
-
),
|
|
331
|
-
).toBe(false);
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
it("an unset or non-string binding registers nothing", () => {
|
|
335
|
-
setDraftPreviewHosts([]);
|
|
336
|
-
installDecoSiteHostFromEnv({});
|
|
337
|
-
expect(isDraftPreviewEnabled({})).toBe(false);
|
|
338
|
-
installDecoSiteHostFromEnv({ DECO_SITE_NAME: 42 });
|
|
339
|
-
expect(isDraftPreviewEnabled({})).toBe(false);
|
|
340
|
-
});
|
|
341
|
-
});
|