@decocms/tanstack 7.64.2 → 7.65.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/package.json +4 -4
- package/src/hooks/CdnSegmentMarker.tsx +3 -0
- package/src/hooks/DecoPageRenderer.tsx +1 -1
- package/src/hooks/DecoRootLayout.tsx +131 -131
- package/src/hooks/DraftPreviewIndicator.tsx +2 -0
- package/src/routes/pageUrl.test.ts +6 -6
- package/src/routes/withSiteGlobals.ts +2 -2
- package/src/sdk/deferredSectionLoader.test.ts +1 -1
- package/src/sdk/draft.test.ts +10 -10
- package/src/sdk/draft.ts +1 -1
- package/src/sdk/kvHydration.test.ts +77 -18
- package/src/sdk/kvHydration.ts +36 -26
- package/src/sdk/router.ts +70 -61
- package/src/sdk/workerEntry.test.ts +211 -119
- package/src/sdk/workerEntry.ts +135 -17
- package/src/vite/plugin.js +24 -5
- package/src/vite/plugin.test.js +1 -1
- package/src/vite/plugin.test.ts +41 -25
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/tanstack",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.65.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco framework binding for TanStack Start + Cloudflare Workers",
|
|
6
6
|
"repository": {
|
|
@@ -27,9 +27,9 @@
|
|
|
27
27
|
"lint:unused": "knip"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@decocms/blocks": "7.
|
|
31
|
-
"@decocms/blocks-admin": "7.
|
|
32
|
-
"@decocms/blocks-cli": "7.
|
|
30
|
+
"@decocms/blocks": "7.65.0",
|
|
31
|
+
"@decocms/blocks-admin": "7.65.0",
|
|
32
|
+
"@decocms/blocks-cli": "7.65.0",
|
|
33
33
|
"@deco-cx/warp-node": "^0.3.16",
|
|
34
34
|
"fast-json-patch": "^3.1.0",
|
|
35
35
|
"ws": "^8.18.0"
|
|
@@ -17,6 +17,8 @@
|
|
|
17
17
|
* harmless by construction — the Worker verifies by recomputing and falls back
|
|
18
18
|
* to `no-store` when it doesn't match.
|
|
19
19
|
*/
|
|
20
|
+
|
|
21
|
+
import { getRequestNonce } from "@decocms/blocks/sdk/nonce";
|
|
20
22
|
import { RequestContext } from "@decocms/blocks/sdk/requestContext";
|
|
21
23
|
import { CSEG_BAG_KEY, CSEG_GLOBAL } from "../sdk/cdnSegment";
|
|
22
24
|
|
|
@@ -25,6 +27,7 @@ export function CdnSegmentMarker() {
|
|
|
25
27
|
if (!token) return null;
|
|
26
28
|
return (
|
|
27
29
|
<script
|
|
30
|
+
nonce={getRequestNonce()}
|
|
28
31
|
// A hash this server computed, not user input. JSON.stringify keeps it
|
|
29
32
|
// inert regardless.
|
|
30
33
|
dangerouslySetInnerHTML={{
|
|
@@ -436,7 +436,7 @@ function DeferredSectionWrapper({
|
|
|
436
436
|
// the skeleton subtree and mount of the resolved subtree even when the
|
|
437
437
|
// section's own LoadingFallback renders the real component with reduced
|
|
438
438
|
// props (a documented convention — see Footer.tsx/ETCImageContent.tsx in
|
|
439
|
-
//
|
|
439
|
+
// a production storefront migration's T64). That unmount/remount is what
|
|
440
440
|
// produces a hard rect-collapse-to-{0,0,0,0} layout-shift entry instead
|
|
441
441
|
// of an ordinary prop-driven resize. Keeping the wrapper identical across
|
|
442
442
|
// both branches lets React diff by type at the child position instead.
|
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import { CdnSegmentMarker } from "./CdnSegmentMarker";
|
|
2
|
-
import { useEffect, type ReactNode } from "react";
|
|
3
|
-
import { HeadContent, Scripts, ScriptOnce, useRouterState } from "@tanstack/react-router";
|
|
4
1
|
import { LiveControls, Stats } from "@decocms/blocks/hooks";
|
|
5
|
-
import {
|
|
6
|
-
ANALYTICS_SCRIPT,
|
|
7
|
-
SPECULATION_DEV_WARN_SCRIPT,
|
|
8
|
-
} from "@decocms/blocks/sdk/analytics";
|
|
2
|
+
import { ANALYTICS_SCRIPT, SPECULATION_DEV_WARN_SCRIPT } from "@decocms/blocks/sdk/analytics";
|
|
9
3
|
import { isDevMode } from "@decocms/blocks/sdk/env";
|
|
4
|
+
import { getRequestNonce } from "@decocms/blocks/sdk/nonce";
|
|
5
|
+
import { HeadContent, ScriptOnce, Scripts, useRouterState } from "@tanstack/react-router";
|
|
6
|
+
import { type ReactNode, useEffect } from "react";
|
|
10
7
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
8
|
+
buildSpeculationRules,
|
|
9
|
+
getSpeculationRules,
|
|
10
|
+
type SpeculationRulesConfig,
|
|
14
11
|
} from "../sdk/speculationRules";
|
|
12
|
+
import { CdnSegmentMarker } from "./CdnSegmentMarker";
|
|
15
13
|
import { DraftPreviewIndicator } from "./DraftPreviewIndicator";
|
|
16
14
|
import { NavigationProgress } from "./NavigationProgress";
|
|
17
15
|
import { StableOutlet } from "./StableOutlet";
|
|
18
16
|
|
|
19
17
|
declare global {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
interface Window {
|
|
19
|
+
__deco_ready?: boolean;
|
|
20
|
+
}
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
function buildDecoEventsBootstrap(account?: string): string {
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const accountJson = JSON.stringify(account ?? "");
|
|
25
|
+
return `
|
|
28
26
|
window.__RUNTIME__ = window.__RUNTIME__ || { account: ${accountJson} };
|
|
29
27
|
window.DECO = window.DECO || {};
|
|
30
28
|
window.DECO.events = window.DECO.events || {
|
|
@@ -48,34 +46,34 @@ window.dataLayer = window.dataLayer || [];
|
|
|
48
46
|
}
|
|
49
47
|
|
|
50
48
|
export interface DecoRootLayoutProps {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
49
|
+
/** Language attribute for the <html> tag. Default: "en" */
|
|
50
|
+
lang?: string;
|
|
51
|
+
/** DaisyUI data-theme attribute. Default: "light" */
|
|
52
|
+
dataTheme?: string;
|
|
53
|
+
/** Site name for LiveControls (admin iframe communication). Required. */
|
|
54
|
+
siteName: string;
|
|
55
|
+
/** Commerce platform account name for analytics bootstrap (e.g. VTEX account). */
|
|
56
|
+
account?: string;
|
|
57
|
+
/** CSS class for <body>. Default: "bg-base-200 text-base-content" */
|
|
58
|
+
bodyClassName?: string;
|
|
59
|
+
/** Delay in ms before firing deco:ready event. Default: 500 */
|
|
60
|
+
decoReadyDelay?: number;
|
|
61
|
+
/**
|
|
62
|
+
* Opt-in Speculation Rules API config override. Normally activated site-wide
|
|
63
|
+
* via `createDecoWorkerEntry({ speculationRules })` (like `asJson` /
|
|
64
|
+
* `renderJson`) and read from that shared config; pass this prop only to
|
|
65
|
+
* override it for a specific root. When set, emits a
|
|
66
|
+
* `<script type="speculationrules">` in <head> so the browser prerenders/
|
|
67
|
+
* prefetches the next document on intent. Ships DISABLED — only activate on
|
|
68
|
+
* sites whose analytics/pixel loaders are prerender-guarded.
|
|
69
|
+
* @see buildSpeculationRules
|
|
70
|
+
*/
|
|
71
|
+
speculationRules?: SpeculationRulesConfig;
|
|
72
|
+
/**
|
|
73
|
+
* Extra content rendered inside <body> after the main outlet
|
|
74
|
+
* (e.g. Toast, custom analytics components).
|
|
75
|
+
*/
|
|
76
|
+
children?: ReactNode;
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
/**
|
|
@@ -96,97 +94,99 @@ export interface DecoRootLayoutProps {
|
|
|
96
94
|
* pieces (NavigationProgress, StableOutlet, etc.) instead.
|
|
97
95
|
*/
|
|
98
96
|
export function DecoRootLayout({
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
97
|
+
lang = "en",
|
|
98
|
+
dataTheme = "light",
|
|
99
|
+
siteName,
|
|
100
|
+
account,
|
|
101
|
+
bodyClassName = "bg-base-200 text-base-content",
|
|
102
|
+
decoReadyDelay = 500,
|
|
103
|
+
speculationRules,
|
|
104
|
+
children,
|
|
107
105
|
}: DecoRootLayoutProps) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
const id = setTimeout(() => {
|
|
108
|
+
window.__deco_ready = true;
|
|
109
|
+
document.dispatchEvent(new Event("deco:ready"));
|
|
110
|
+
}, decoReadyDelay);
|
|
111
|
+
return () => clearTimeout(id);
|
|
112
|
+
}, [decoReadyDelay]);
|
|
115
113
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
114
|
+
// Pull the resolved CMS page's route pattern (`path` = pathTemplate) and block
|
|
115
|
+
// id (`blockKey`) out of the current route's loader data so LiveControls sends
|
|
116
|
+
// the admin the page the user is actually on — not the hardcoded "/*" fallback.
|
|
117
|
+
// The CMS page loader (cmsRoute) spreads both fields onto its loader data;
|
|
118
|
+
// we scan matches deepest-first to find the one that carries them.
|
|
119
|
+
const page = useRouterState({
|
|
120
|
+
select: (state) => {
|
|
121
|
+
for (let i = state.matches.length - 1; i >= 0; i--) {
|
|
122
|
+
const data = state.matches[i]?.loaderData as
|
|
123
|
+
| { path?: unknown; blockKey?: unknown }
|
|
124
|
+
| undefined;
|
|
125
|
+
if (data && typeof data.path === "string") {
|
|
126
|
+
return {
|
|
127
|
+
id: typeof data.blockKey === "string" ? data.blockKey : undefined,
|
|
128
|
+
pathTemplate: data.path,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
},
|
|
134
|
+
});
|
|
137
135
|
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
// Worker-entry option wins; prop is a per-root override. Undefined → disabled.
|
|
137
|
+
const speculation = speculationRules ?? getSpeculationRules();
|
|
140
138
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
139
|
+
return (
|
|
140
|
+
<html lang={lang} data-theme={dataTheme} suppressHydrationWarning>
|
|
141
|
+
<head>
|
|
142
|
+
<HeadContent />
|
|
143
|
+
<CdnSegmentMarker />
|
|
144
|
+
{speculation && (
|
|
145
|
+
<script
|
|
146
|
+
type="speculationrules"
|
|
147
|
+
nonce={getRequestNonce()}
|
|
148
|
+
// JSON is inert; dangerouslySetInnerHTML avoids React escaping
|
|
149
|
+
// `>` in selectors (e.g. "nav > a"). Same pattern as JSON-LD.
|
|
150
|
+
dangerouslySetInnerHTML={{
|
|
151
|
+
__html: buildSpeculationRules(speculation),
|
|
152
|
+
}}
|
|
153
|
+
/>
|
|
154
|
+
)}
|
|
155
|
+
{/* Dev-only: console.error when analytics fire during a prerender
|
|
157
156
|
so the dev fixes unguarded loaders before shipping. */}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
157
|
+
{speculation && isDevMode() && (
|
|
158
|
+
<script
|
|
159
|
+
nonce={getRequestNonce()}
|
|
160
|
+
dangerouslySetInnerHTML={{ __html: SPECULATION_DEV_WARN_SCRIPT }}
|
|
161
|
+
/>
|
|
162
|
+
)}
|
|
163
|
+
</head>
|
|
164
|
+
<body className={bodyClassName} suppressHydrationWarning>
|
|
165
|
+
<ScriptOnce children={buildDecoEventsBootstrap(account)} />
|
|
166
|
+
{/*
|
|
167
|
+
* AFTER the bootstrap above, and in the body rather than the head. The collector's
|
|
168
|
+
* deco module subscribes with `window?.DECO?.events?.subscribe?.(…)` — once,
|
|
169
|
+
* optional-chained, with no retry. In the head it is an `async` script the parser
|
|
170
|
+
* can reach and run before the inline bootstrap below it has defined
|
|
171
|
+
* `DECO.events`, and the subscription would simply not happen: pageviews would
|
|
172
|
+
* still flow while every commerce event went nowhere, with no error. Here the
|
|
173
|
+
* parser has already executed the bootstrap.
|
|
174
|
+
*
|
|
175
|
+
* Rendering unconditionally is safe — the component returns null unless
|
|
176
|
+
* DECO_ANALYTICS_ENABLED is exactly "true", which is what makes this an env-var
|
|
177
|
+
* change for a site instead of a code change.
|
|
178
|
+
*/}
|
|
179
|
+
<Stats />
|
|
180
|
+
<NavigationProgress />
|
|
181
|
+
<main>
|
|
182
|
+
<StableOutlet />
|
|
183
|
+
</main>
|
|
184
|
+
{children}
|
|
185
|
+
<DraftPreviewIndicator />
|
|
186
|
+
<LiveControls site={siteName} page={page} />
|
|
187
|
+
<ScriptOnce children={ANALYTICS_SCRIPT} />
|
|
188
|
+
<Scripts />
|
|
189
|
+
</body>
|
|
190
|
+
</html>
|
|
191
|
+
);
|
|
192
192
|
}
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
* closed), so the visible chip never participates in the hydration diff.
|
|
29
29
|
*/
|
|
30
30
|
import { DraftPreviewBadge } from "@decocms/blocks/preview";
|
|
31
|
+
import { getRequestNonce } from "@decocms/blocks/sdk/nonce";
|
|
31
32
|
import { RequestContext } from "@decocms/blocks/sdk/requestContext";
|
|
32
33
|
import { DRAFT_POINTER_BAG_KEY, DRAFT_POINTER_GLOBAL } from "../sdk/draftShared";
|
|
33
34
|
|
|
@@ -52,6 +53,7 @@ export function DraftPreviewIndicator() {
|
|
|
52
53
|
{/* Publishes a JSON-encoded pointer this server resolved (not user
|
|
53
54
|
input) so the client's hydration render matches. See the module doc. */}
|
|
54
55
|
<script
|
|
56
|
+
nonce={getRequestNonce()}
|
|
55
57
|
dangerouslySetInnerHTML={{
|
|
56
58
|
__html: `window.${DRAFT_POINTER_GLOBAL}=${JSON.stringify(pointer)}`,
|
|
57
59
|
}}
|
|
@@ -4,8 +4,8 @@ import { derivePageUrl, isClientNavigation } from "./pageUrl";
|
|
|
4
4
|
describe("derivePageUrl (#280 — client-nav request URL)", () => {
|
|
5
5
|
it("SSR real page without query: rebuilds from fullPath", () => {
|
|
6
6
|
expect(
|
|
7
|
-
derivePageUrl("/
|
|
8
|
-
).toBe("https://o.com/
|
|
7
|
+
derivePageUrl("/acme/produto", new URL("https://o.com/acme/produto")),
|
|
8
|
+
).toBe("https://o.com/acme/produto");
|
|
9
9
|
});
|
|
10
10
|
|
|
11
11
|
it("SSR real page preserves duplicate query params from the server URL", () => {
|
|
@@ -21,10 +21,10 @@ describe("derivePageUrl (#280 — client-nav request URL)", () => {
|
|
|
21
21
|
it("client nav without query: rebuilds from fullPath, NOT the /_serverFn URL", () => {
|
|
22
22
|
// The regression guard: getRequestUrl() is the serverFn endpoint here.
|
|
23
23
|
const serverUrl = new URL(
|
|
24
|
-
"https://o.com/_serverFn/loadCmsPage?payload=%
|
|
24
|
+
"https://o.com/_serverFn/loadCmsPage?payload=%2Facme%2Fproduto",
|
|
25
25
|
);
|
|
26
|
-
expect(derivePageUrl("/
|
|
27
|
-
"https://o.com/
|
|
26
|
+
expect(derivePageUrl("/acme/produto", serverUrl)).toBe(
|
|
27
|
+
"https://o.com/acme/produto",
|
|
28
28
|
);
|
|
29
29
|
});
|
|
30
30
|
|
|
@@ -50,7 +50,7 @@ describe("isClientNavigation (SPA vs SSR detection)", () => {
|
|
|
50
50
|
|
|
51
51
|
it("SSR real page without query (path matches): false", () => {
|
|
52
52
|
expect(
|
|
53
|
-
isClientNavigation("/
|
|
53
|
+
isClientNavigation("/acme/produto", new URL("https://o.com/acme/produto")),
|
|
54
54
|
).toBe(false);
|
|
55
55
|
});
|
|
56
56
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* (`@decocms/tanstack/sdk/deferredSectionLoader`).
|
|
4
4
|
*
|
|
5
5
|
* Before 7.7 this wrapper was unreachable from any public subpath, so
|
|
6
|
-
* migrated
|
|
6
|
+
* migrated production storefronts each carried a
|
|
7
7
|
* byte-identical local shim wrapping the public `loadDeferredSection`
|
|
8
8
|
* export. The cases below are derived from how those sites call it: it is
|
|
9
9
|
* passed verbatim as `<DecoPageRenderer loadDeferredSectionFn={...} />`,
|
package/src/sdk/draft.test.ts
CHANGED
|
@@ -276,13 +276,13 @@ describe("installPreviewHostsFromBlocks", () => {
|
|
|
276
276
|
it("installs previewHosts from the site block (either casing)", async () => {
|
|
277
277
|
setDraftPreviewHosts([]);
|
|
278
278
|
installPreviewHostsFromBlocks({
|
|
279
|
-
Site: { previewHosts: ["
|
|
279
|
+
Site: { previewHosts: ["acme.vtex.app"] },
|
|
280
280
|
});
|
|
281
281
|
|
|
282
282
|
// Verified through the wire: an allowed host now previews.
|
|
283
283
|
const request = req({
|
|
284
|
-
url: "https://
|
|
285
|
-
host: "
|
|
284
|
+
url: "https://acme.vtex.app/p?__draft=abc.preview-studio.decocms.com@v1",
|
|
285
|
+
host: "acme.vtex.app",
|
|
286
286
|
});
|
|
287
287
|
const url = new URL(request.url);
|
|
288
288
|
expect(requestCarriesDraft(request, url)).toBe(true);
|
|
@@ -291,10 +291,10 @@ describe("installPreviewHostsFromBlocks", () => {
|
|
|
291
291
|
it("ignores blocks without previewHosts", () => {
|
|
292
292
|
setDraftPreviewHosts([]);
|
|
293
293
|
installPreviewHostsFromBlocks({ site: {} });
|
|
294
|
-
const url = new URL("https://
|
|
294
|
+
const url = new URL("https://acme.vtex.app/p?__draft=x@v1");
|
|
295
295
|
expect(
|
|
296
296
|
requestCarriesDraft(
|
|
297
|
-
req({ url: url.toString(), host: "
|
|
297
|
+
req({ url: url.toString(), host: "acme.vtex.app" }),
|
|
298
298
|
url,
|
|
299
299
|
),
|
|
300
300
|
).toBe(false);
|
|
@@ -308,13 +308,13 @@ describe("installDecoSiteHostFromEnv", () => {
|
|
|
308
308
|
|
|
309
309
|
it("arms the deco-hosted domains from the DECO_SITE_NAME binding", () => {
|
|
310
310
|
setDraftPreviewHosts([]);
|
|
311
|
-
installDecoSiteHostFromEnv({ DECO_SITE_NAME: "
|
|
311
|
+
installDecoSiteHostFromEnv({ DECO_SITE_NAME: "acme-tanstack" });
|
|
312
312
|
|
|
313
313
|
// Verified through the wire: both deco-operated hosts now preview, with no
|
|
314
314
|
// site-block/env config at all.
|
|
315
315
|
for (const host of [
|
|
316
|
-
"
|
|
317
|
-
"
|
|
316
|
+
"acme-tanstack.deco.site",
|
|
317
|
+
"acme-tanstack.deco-cx.workers.dev",
|
|
318
318
|
]) {
|
|
319
319
|
const url = new URL(
|
|
320
320
|
`https://${host}/p?__draft=abc.preview-studio.decocms.com@v1`,
|
|
@@ -322,10 +322,10 @@ describe("installDecoSiteHostFromEnv", () => {
|
|
|
322
322
|
expect(requestCarriesDraft(req({ url: url.toString(), host }), url)).toBe(true);
|
|
323
323
|
}
|
|
324
324
|
// A custom production domain is never inferred.
|
|
325
|
-
const url = new URL("https://www.
|
|
325
|
+
const url = new URL("https://www.acme.com.br/p?__draft=x@v1");
|
|
326
326
|
expect(
|
|
327
327
|
requestCarriesDraft(
|
|
328
|
-
req({ url: url.toString(), host: "www.
|
|
328
|
+
req({ url: url.toString(), host: "www.acme.com.br" }),
|
|
329
329
|
url,
|
|
330
330
|
),
|
|
331
331
|
).toBe(false);
|
package/src/sdk/draft.ts
CHANGED
|
@@ -94,7 +94,7 @@ export function installPreviewHostsFromBlocks(blocks: Record<string, unknown> |
|
|
|
94
94
|
/**
|
|
95
95
|
* Register the deco-operated preview hosts inferred from the site name —
|
|
96
96
|
* `<site>.deco.site` and `<site>.deco-cx.workers.dev` (the workers.dev deploy
|
|
97
|
-
* URL, e.g. `
|
|
97
|
+
* URL, e.g. `acme-tanstack.deco-cx.workers.dev`) — so a signed
|
|
98
98
|
* `?__draft=` grant previews on deco-hosted infra with zero per-site config.
|
|
99
99
|
*
|
|
100
100
|
* Fed from the Workers env binding `DECO_SITE_NAME`: deploy-time configuration
|
|
@@ -215,28 +215,56 @@ describe("maybePollRevision", () => {
|
|
|
215
215
|
});
|
|
216
216
|
|
|
217
217
|
// ---------------------------------------------------------------------------
|
|
218
|
-
//
|
|
218
|
+
// Bundles that ship no decofile (`decoVitePlugin({ fastDeploy: true })`)
|
|
219
219
|
//
|
|
220
|
-
//
|
|
221
|
-
// "warn and serve bundled" recovery would serve an EMPTY site —
|
|
222
|
-
// pages, which get edge-cached and outlive the KV failure
|
|
223
|
-
//
|
|
220
|
+
// With the decofile stubbed out of the server bundle, KV is the ONLY source of
|
|
221
|
+
// content. The "warn and serve bundled" recovery would serve an EMPTY site —
|
|
222
|
+
// 200s with no pages, which get edge-cached and outlive the KV failure that
|
|
223
|
+
// caused them. These assert it fails loudly instead.
|
|
224
|
+
//
|
|
225
|
+
// The signal is the build-time `__DECO_BLOCKS_STUBBED__` define, NOT an empty
|
|
226
|
+
// in-memory map: draft overrides can make a stubbed bundle look populated, and
|
|
227
|
+
// a legitimately-empty decofile would look stubbed. Both are covered below.
|
|
224
228
|
// ---------------------------------------------------------------------------
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
229
|
+
declare global {
|
|
230
|
+
// eslint-disable-next-line no-var
|
|
231
|
+
var __DECO_BLOCKS_STUBBED__: boolean | undefined;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Simulates a build made with `fastDeploy: true`. */
|
|
235
|
+
function withStubbedBundle() {
|
|
236
|
+
globalThis.__DECO_BLOCKS_STUBBED__ = true;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
describe("ensureBlocksHydrated when the bundle ships no decofile", () => {
|
|
240
|
+
beforeEach(() => {
|
|
241
|
+
globalThis.__DECO_BLOCKS_STUBBED__ = undefined;
|
|
242
|
+
});
|
|
228
243
|
|
|
229
244
|
it("throws when the snapshot is missing instead of serving an empty site", async () => {
|
|
230
|
-
|
|
245
|
+
withStubbedBundle();
|
|
246
|
+
setBlocks({});
|
|
231
247
|
const { kv } = makeKV(); // no decofile:<id> seeded
|
|
232
248
|
const env = { DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID };
|
|
233
249
|
|
|
234
|
-
await expect(ensureBlocksHydrated(env)).rejects.toThrow(/not found and
|
|
235
|
-
|
|
250
|
+
await expect(ensureBlocksHydrated(env)).rejects.toThrow(/not found and this bundle ships no/);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it("throws when the snapshot exists but is empty", async () => {
|
|
254
|
+
// A seed that wrote `{}` would otherwise take the success branch, latch,
|
|
255
|
+
// and serve the empty edge-cached site this guard exists to prevent.
|
|
256
|
+
withStubbedBundle();
|
|
257
|
+
setBlocks({});
|
|
258
|
+
const { kv } = makeKV({ [SNAP]: "{}", [REV]: computeRevision({}) });
|
|
259
|
+
|
|
260
|
+
await expect(
|
|
261
|
+
ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID }),
|
|
262
|
+
).rejects.toThrow(/is empty and this bundle ships no/);
|
|
236
263
|
});
|
|
237
264
|
|
|
238
265
|
it("throws when KV itself fails", async () => {
|
|
239
|
-
|
|
266
|
+
withStubbedBundle();
|
|
267
|
+
setBlocks({});
|
|
240
268
|
const kv = {
|
|
241
269
|
get: () => Promise.reject(new Error("KV unreachable")),
|
|
242
270
|
put: () => Promise.resolve(),
|
|
@@ -249,16 +277,31 @@ describe("ensureBlocksHydrated without a bundled fallback", () => {
|
|
|
249
277
|
});
|
|
250
278
|
|
|
251
279
|
it("throws when no deployment id resolves", async () => {
|
|
252
|
-
|
|
280
|
+
withStubbedBundle();
|
|
281
|
+
setBlocks({});
|
|
282
|
+
const { kv } = makeKV();
|
|
283
|
+
|
|
284
|
+
await expect(ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1" })).rejects.toThrow(
|
|
285
|
+
/no deployment id/,
|
|
286
|
+
);
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("is not rescued by a populated in-memory map (draft-preview overrides)", async () => {
|
|
290
|
+
// `bindRequestDraft` composes overrides into loadBlocks() BEFORE hydration.
|
|
291
|
+
// Inferring "has a fallback" from the map would latch here and then serve
|
|
292
|
+
// the empty base decofile for the isolate's life.
|
|
293
|
+
withStubbedBundle();
|
|
294
|
+
setBlocks({ "pages-Draft": { name: "override" } });
|
|
253
295
|
const { kv } = makeKV();
|
|
254
296
|
|
|
255
297
|
await expect(
|
|
256
|
-
ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1" }),
|
|
257
|
-
).rejects.toThrow(/
|
|
298
|
+
ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID }),
|
|
299
|
+
).rejects.toThrow(/not found and this bundle ships no/);
|
|
258
300
|
});
|
|
259
301
|
|
|
260
302
|
it("does not latch hydration on the fatal path, so the next request retries", async () => {
|
|
261
|
-
|
|
303
|
+
withStubbedBundle();
|
|
304
|
+
setBlocks({});
|
|
262
305
|
const blocks = { Site: { name: "from-kv" } };
|
|
263
306
|
const store = new Map<string, string>();
|
|
264
307
|
const kv: KVNamespace = {
|
|
@@ -284,9 +327,14 @@ describe("ensureBlocksHydrated without a bundled fallback", () => {
|
|
|
284
327
|
await expect(ensureBlocksHydrated(env)).resolves.toBeUndefined();
|
|
285
328
|
expect(loadBlocks()).toEqual(blocks);
|
|
286
329
|
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
describe("ensureBlocksHydrated when the bundle DOES ship a decofile (default)", () => {
|
|
333
|
+
beforeEach(() => {
|
|
334
|
+
globalThis.__DECO_BLOCKS_STUBBED__ = undefined;
|
|
335
|
+
});
|
|
287
336
|
|
|
288
|
-
it("
|
|
289
|
-
// Default configuration — must behave exactly as before this change.
|
|
337
|
+
it("warns and serves bundled when KV fails", async () => {
|
|
290
338
|
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
291
339
|
const kv = {
|
|
292
340
|
get: () => Promise.reject(new Error("KV unreachable")),
|
|
@@ -300,4 +348,15 @@ describe("ensureBlocksHydrated without a bundled fallback", () => {
|
|
|
300
348
|
expect(loadBlocks()).toEqual(BUNDLED);
|
|
301
349
|
expect(warn).toHaveBeenCalled();
|
|
302
350
|
});
|
|
351
|
+
|
|
352
|
+
it("does not 5xx a site whose bundled decofile is legitimately empty", async () => {
|
|
353
|
+
// New site / missing blocks.gen.json. Before the build-time flag this
|
|
354
|
+
// looked identical to a stubbed bundle and started failing requests.
|
|
355
|
+
setBlocks({});
|
|
356
|
+
const { kv } = makeKV(); // no snapshot seeded either
|
|
357
|
+
|
|
358
|
+
await expect(
|
|
359
|
+
ensureBlocksHydrated({ DECO_KV: kv, DECO_FAST_DEPLOY: "1", DECO_DEPLOYMENT_ID: ID }),
|
|
360
|
+
).resolves.toBeUndefined();
|
|
361
|
+
});
|
|
303
362
|
});
|