@decocms/tanstack 8.5.2 → 8.6.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/README.md +35 -8
- package/dist/assignments.d.ts +31 -0
- package/dist/assignments.d.ts.map +1 -0
- package/dist/assignments.js +61 -0
- package/dist/assignments.js.map +1 -0
- package/dist/draft.d.ts +9 -6
- package/dist/draft.d.ts.map +1 -1
- package/dist/draft.js +10 -7
- package/dist/draft.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/scope/server.d.ts +1 -1
- package/dist/scope/server.d.ts.map +1 -1
- package/dist/server/index.d.ts +35 -8
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +41 -9
- package/dist/server/index.js.map +1 -1
- package/dist/shared.d.ts +5 -5
- package/dist/shared.js +5 -5
- package/dist/start/index.d.ts +3 -3
- package/dist/start/index.js +3 -3
- package/dist/visitor.d.ts +14 -0
- package/dist/visitor.d.ts.map +1 -0
- package/dist/visitor.js +14 -0
- package/dist/visitor.js.map +1 -0
- package/dist/worker/assignments.d.ts +44 -0
- package/dist/worker/assignments.d.ts.map +1 -0
- package/dist/worker/assignments.js +114 -0
- package/dist/worker/assignments.js.map +1 -0
- package/dist/worker/cache.d.ts +33 -0
- package/dist/worker/cache.d.ts.map +1 -0
- package/dist/worker/cache.js +41 -0
- package/dist/worker/cache.js.map +1 -0
- package/dist/worker/cookies.d.ts +1 -5
- package/dist/worker/cookies.d.ts.map +1 -1
- package/dist/worker/cookies.js +1 -26
- package/dist/worker/cookies.js.map +1 -1
- package/dist/worker/drafts.d.ts +27 -48
- package/dist/worker/drafts.d.ts.map +1 -1
- package/dist/worker/drafts.js +33 -88
- package/dist/worker/drafts.js.map +1 -1
- package/dist/worker/index.d.ts +77 -51
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +141 -360
- package/dist/worker/index.js.map +1 -1
- package/dist/worker/segment.d.ts +7 -6
- package/dist/worker/segment.d.ts.map +1 -1
- package/dist/worker/segment.js +6 -5
- package/dist/worker/segment.js.map +1 -1
- package/dist/worker/serverFn.d.ts +12 -0
- package/dist/worker/serverFn.d.ts.map +1 -0
- package/dist/worker/serverFn.js +44 -0
- package/dist/worker/serverFn.js.map +1 -0
- package/package.json +3 -3
- package/src/assignments.ts +77 -0
- package/src/draft.ts +10 -7
- package/src/index.ts +3 -1
- package/src/scope/server.ts +1 -1
- package/src/server/index.ts +51 -12
- package/src/shared.ts +5 -5
- package/src/start/index.ts +3 -3
- package/src/visitor.ts +14 -0
- package/src/worker/assignments.ts +146 -0
- package/src/worker/cache.ts +57 -0
- package/src/worker/cookies.ts +1 -27
- package/src/worker/drafts.ts +38 -98
- package/src/worker/index.ts +213 -425
- package/src/worker/segment.ts +7 -7
- package/src/worker/serverFn.ts +36 -0
- package/dist/worker/cacheProfiles.d.ts +0 -43
- package/dist/worker/cacheProfiles.d.ts.map +0 -1
- package/dist/worker/cacheProfiles.js +0 -162
- package/dist/worker/cacheProfiles.js.map +0 -1
- package/src/worker/cacheProfiles.ts +0 -180
package/src/worker/segment.ts
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* What
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* What a site's own cache splits on: the device class of a request, and a
|
|
3
|
+
* segment (the device plus the site's own parts: a region, a sales channel) as
|
|
4
|
+
* one stable string, for a cache key and for the `__cseg` token the page hands
|
|
5
|
+
* to the browser, so a CDN can keep client-side navigation data under the
|
|
6
|
+
* same split. The worker caches nothing itself (the README's worker-entry
|
|
7
|
+
* recipe does).
|
|
6
8
|
*/
|
|
7
9
|
|
|
8
10
|
export type Device = "mobile" | "tablet" | "desktop";
|
|
9
11
|
|
|
10
|
-
/** A cache split: part name to value
|
|
12
|
+
/** A cache split: part name to value (the device, and the site's own parts). */
|
|
11
13
|
export type Segment = Readonly<Record<string, string>>;
|
|
12
14
|
|
|
13
15
|
// v7's expressions (@decocms/blocks/sdk/detectDevice): tablets first, so
|
|
@@ -21,8 +23,6 @@ export function detectDevice(userAgent: string): Device {
|
|
|
21
23
|
return "desktop";
|
|
22
24
|
}
|
|
23
25
|
|
|
24
|
-
export const DEVICES: readonly Device[] = ["mobile", "tablet", "desktop"];
|
|
25
|
-
|
|
26
26
|
/** A segment as one stable string: parts sorted by name, values URL-encoded. */
|
|
27
27
|
export function segmentKey(segment: Segment): string {
|
|
28
28
|
return Object.keys(segment)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TanStack Start's server-function URLs, as the worker and a site's own cache
|
|
3
|
+
* read them: which paths are server functions, and the page a GET page load
|
|
4
|
+
* asks for (the first local path in its `payload`).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export function isServerFnPath(pathname: string): boolean {
|
|
8
|
+
return pathname.startsWith("/_serverFn/") || pathname.startsWith("/_server/");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The page a GET server-function request loads, from its `payload`: the first
|
|
13
|
+
* string in it that is a local path. `null` for any other server function.
|
|
14
|
+
*/
|
|
15
|
+
export function serverFnPagePath(url: URL): string | null {
|
|
16
|
+
if (!isServerFnPath(url.pathname)) return null;
|
|
17
|
+
const payload = url.searchParams.get("payload");
|
|
18
|
+
if (!payload) return null;
|
|
19
|
+
let found: string | null = null;
|
|
20
|
+
const visit = (v: unknown): void => {
|
|
21
|
+
if (found !== null) return;
|
|
22
|
+
if (typeof v === "string") {
|
|
23
|
+
if (v.startsWith("/") && !v.startsWith("//")) found = v;
|
|
24
|
+
} else if (Array.isArray(v)) {
|
|
25
|
+
for (const x of v) visit(x);
|
|
26
|
+
} else if (v && typeof v === "object") {
|
|
27
|
+
for (const x of Object.values(v)) visit(x);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
try {
|
|
31
|
+
visit(JSON.parse(payload));
|
|
32
|
+
} catch {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
return found;
|
|
36
|
+
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Page cache profiles: how long a response may be served from the edge cache
|
|
3
|
-
* and from the browser, picked per URL. The values and URL rules are v7's
|
|
4
|
-
* (`@decocms/blocks/sdk/cacheHeaders`), which the TanStack storefronts copied
|
|
5
|
-
* unchanged; a site that needs other values passes `cacheProfiles` to
|
|
6
|
-
* `createDecoWorker`.
|
|
7
|
-
*/
|
|
8
|
-
export type CacheProfileName = "static" | "product" | "listing" | "search" | "cart" | "private" | "none";
|
|
9
|
-
/** Seconds: `fresh` = served as is; `swr` = served stale while revalidating; `sie` = served stale when the origin fails. */
|
|
10
|
-
export interface CacheWindow {
|
|
11
|
-
fresh: number;
|
|
12
|
-
swr: number;
|
|
13
|
-
sie: number;
|
|
14
|
-
}
|
|
15
|
-
export interface CacheProfile {
|
|
16
|
-
edge: CacheWindow;
|
|
17
|
-
browser: CacheWindow;
|
|
18
|
-
isPublic: boolean;
|
|
19
|
-
}
|
|
20
|
-
/** A site's changes to the default profiles: only the values that differ. */
|
|
21
|
-
export type CacheProfileOverrides = Partial<Record<CacheProfileName, {
|
|
22
|
-
edge?: Partial<CacheWindow>;
|
|
23
|
-
browser?: Partial<CacheWindow>;
|
|
24
|
-
}>>;
|
|
25
|
-
export declare const DEFAULT_CACHE_PROFILES: Readonly<Record<CacheProfileName, CacheProfile>>;
|
|
26
|
-
/** The defaults with a site's overrides applied. The private profiles stay private. */
|
|
27
|
-
export declare function resolveProfiles(overrides: CacheProfileOverrides | undefined): Record<CacheProfileName, CacheProfile>;
|
|
28
|
-
export declare const PRIVATE_NO_CACHE = "private, no-cache, no-store, must-revalidate";
|
|
29
|
-
/** The browser's `Cache-Control` for a profile. */
|
|
30
|
-
export declare function cacheControl(profile: CacheProfile): string;
|
|
31
|
-
/** The profile of a page URL. The first rule that matches wins. */
|
|
32
|
-
export declare function detectCacheProfile(url: URL): CacheProfileName;
|
|
33
|
-
export declare function isServerFnPath(pathname: string): boolean;
|
|
34
|
-
/**
|
|
35
|
-
* The page a GET server-function request loads, from its `payload`: the first
|
|
36
|
-
* string in it that is a local path. `null` for any other server function.
|
|
37
|
-
*/
|
|
38
|
-
export declare function serverFnPagePath(url: URL): string | null;
|
|
39
|
-
/** A server function's payload for its cache key: page paths without the variant params. */
|
|
40
|
-
export declare function canonicalizeServerFnPayload(payload: string): string;
|
|
41
|
-
/** The profile of a request: a GET server function takes its page's, any other one is private. */
|
|
42
|
-
export declare function profileOf(url: URL): CacheProfileName;
|
|
43
|
-
//# sourceMappingURL=cacheProfiles.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cacheProfiles.d.ts","sourceRoot":"","sources":["../../src/worker/cacheProfiles.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,gBAAgB,GACxB,QAAQ,GACR,SAAS,GACT,SAAS,GACT,QAAQ,GACR,MAAM,GACN,SAAS,GACT,MAAM,CAAC;AAEX,4HAA4H;AAC5H,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,WAAW,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,6EAA6E;AAC7E,MAAM,MAAM,qBAAqB,GAAG,OAAO,CACzC,MAAM,CAAC,gBAAgB,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC,CAAA;CAAE,CAAC,CAC1F,CAAC;AAKF,eAAO,MAAM,sBAAsB,EAAE,QAAQ,CAAC,MAAM,CAAC,gBAAgB,EAAE,YAAY,CAAC,CAwBnF,CAAC;AAEF,uFAAuF;AACvF,wBAAgB,eAAe,CAC7B,SAAS,EAAE,qBAAqB,GAAG,SAAS,GAC3C,MAAM,CAAC,gBAAgB,EAAE,YAAY,CAAC,CAexC;AAED,eAAO,MAAM,gBAAgB,iDAAiD,CAAC;AAE/E,mDAAmD;AACnD,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,MAAM,CAU1D;AAID,mEAAmE;AACnE,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,gBAAgB,CAS7D;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAExD;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,GAAG,IAAI,CAqBxD;AAKD,4FAA4F;AAC5F,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAsBnE;AAED,kGAAkG;AAClG,wBAAgB,SAAS,CAAC,GAAG,EAAE,GAAG,GAAG,gBAAgB,CAMpD"}
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Page cache profiles: how long a response may be served from the edge cache
|
|
3
|
-
* and from the browser, picked per URL. The values and URL rules are v7's
|
|
4
|
-
* (`@decocms/blocks/sdk/cacheHeaders`), which the TanStack storefronts copied
|
|
5
|
-
* unchanged; a site that needs other values passes `cacheProfiles` to
|
|
6
|
-
* `createDecoWorker`.
|
|
7
|
-
*/
|
|
8
|
-
const NEVER = { fresh: 0, swr: 0, sie: 0 };
|
|
9
|
-
const NO_CACHE = { edge: NEVER, browser: NEVER, isPublic: false };
|
|
10
|
-
export const DEFAULT_CACHE_PROFILES = {
|
|
11
|
-
static: {
|
|
12
|
-
edge: { fresh: 900, swr: 7200, sie: 21600 },
|
|
13
|
-
browser: { fresh: 120, swr: 1800, sie: 7200 },
|
|
14
|
-
isPublic: true,
|
|
15
|
-
},
|
|
16
|
-
product: {
|
|
17
|
-
edge: { fresh: 300, swr: 1800, sie: 7200 },
|
|
18
|
-
browser: { fresh: 60, swr: 600, sie: 3600 },
|
|
19
|
-
isPublic: true,
|
|
20
|
-
},
|
|
21
|
-
listing: {
|
|
22
|
-
edge: { fresh: 120, swr: 900, sie: 3600 },
|
|
23
|
-
browser: { fresh: 30, swr: 300, sie: 1800 },
|
|
24
|
-
isPublic: true,
|
|
25
|
-
},
|
|
26
|
-
search: {
|
|
27
|
-
edge: { fresh: 60, swr: 300, sie: 1800 },
|
|
28
|
-
browser: { fresh: 0, swr: 120, sie: 600 },
|
|
29
|
-
isPublic: true,
|
|
30
|
-
},
|
|
31
|
-
cart: NO_CACHE,
|
|
32
|
-
private: NO_CACHE,
|
|
33
|
-
none: NO_CACHE,
|
|
34
|
-
};
|
|
35
|
-
/** The defaults with a site's overrides applied. The private profiles stay private. */
|
|
36
|
-
export function resolveProfiles(overrides) {
|
|
37
|
-
const out = { ...DEFAULT_CACHE_PROFILES };
|
|
38
|
-
for (const [name, change] of Object.entries(overrides ?? {})) {
|
|
39
|
-
const base = DEFAULT_CACHE_PROFILES[name];
|
|
40
|
-
if (!base || !change || !base.isPublic)
|
|
41
|
-
continue;
|
|
42
|
-
out[name] = {
|
|
43
|
-
isPublic: true,
|
|
44
|
-
edge: { ...base.edge, ...change.edge },
|
|
45
|
-
browser: { ...base.browser, ...change.browser },
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
return out;
|
|
49
|
-
}
|
|
50
|
-
export const PRIVATE_NO_CACHE = "private, no-cache, no-store, must-revalidate";
|
|
51
|
-
/** The browser's `Cache-Control` for a profile. */
|
|
52
|
-
export function cacheControl(profile) {
|
|
53
|
-
if (!profile.isPublic || (profile.edge.fresh === 0 && profile.browser.fresh === 0)) {
|
|
54
|
-
return PRIVATE_NO_CACHE;
|
|
55
|
-
}
|
|
56
|
-
const { browser, edge } = profile;
|
|
57
|
-
const parts = ["public", browser.fresh > 0 ? `max-age=${browser.fresh}` : "max-age=0"];
|
|
58
|
-
if (edge.fresh > 0)
|
|
59
|
-
parts.push(`s-maxage=${edge.fresh}`);
|
|
60
|
-
if (browser.swr > 0)
|
|
61
|
-
parts.push(`stale-while-revalidate=${browser.swr}`);
|
|
62
|
-
if (browser.sie > 0)
|
|
63
|
-
parts.push(`stale-if-error=${browser.sie}`);
|
|
64
|
-
return parts.join(", ");
|
|
65
|
-
}
|
|
66
|
-
const PRIVATE_PREFIX_RE = /^\/(cart|checkout|account|login|my-account)(\/|$)/;
|
|
67
|
-
/** The profile of a page URL. The first rule that matches wins. */
|
|
68
|
-
export function detectCacheProfile(url) {
|
|
69
|
-
const p = url.pathname;
|
|
70
|
-
if (PRIVATE_PREFIX_RE.test(p))
|
|
71
|
-
return "private";
|
|
72
|
-
if (p.startsWith("/api/") || p.startsWith("/deco/") || p.startsWith("/_build"))
|
|
73
|
-
return "none";
|
|
74
|
-
if (p === "/sitemap.xml")
|
|
75
|
-
return "static";
|
|
76
|
-
if (p === "/s" || p.startsWith("/s/") || url.searchParams.has("q"))
|
|
77
|
-
return "search";
|
|
78
|
-
if (p.endsWith("/p"))
|
|
79
|
-
return "product";
|
|
80
|
-
if (p === "/" || p === "")
|
|
81
|
-
return "static";
|
|
82
|
-
return "listing";
|
|
83
|
-
}
|
|
84
|
-
export function isServerFnPath(pathname) {
|
|
85
|
-
return pathname.startsWith("/_serverFn/") || pathname.startsWith("/_server/");
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* The page a GET server-function request loads, from its `payload`: the first
|
|
89
|
-
* string in it that is a local path. `null` for any other server function.
|
|
90
|
-
*/
|
|
91
|
-
export function serverFnPagePath(url) {
|
|
92
|
-
if (!isServerFnPath(url.pathname))
|
|
93
|
-
return null;
|
|
94
|
-
const payload = url.searchParams.get("payload");
|
|
95
|
-
if (!payload)
|
|
96
|
-
return null;
|
|
97
|
-
let found = null;
|
|
98
|
-
const visit = (v) => {
|
|
99
|
-
if (found !== null)
|
|
100
|
-
return;
|
|
101
|
-
if (typeof v === "string") {
|
|
102
|
-
if (v.startsWith("/") && !v.startsWith("//"))
|
|
103
|
-
found = v;
|
|
104
|
-
}
|
|
105
|
-
else if (Array.isArray(v)) {
|
|
106
|
-
for (const x of v)
|
|
107
|
-
visit(x);
|
|
108
|
-
}
|
|
109
|
-
else if (v && typeof v === "object") {
|
|
110
|
-
for (const x of Object.values(v))
|
|
111
|
-
visit(x);
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
try {
|
|
115
|
-
visit(JSON.parse(payload));
|
|
116
|
-
}
|
|
117
|
-
catch {
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
return found;
|
|
121
|
-
}
|
|
122
|
-
/** Variant params a page loader ignores, left out of a server function's cache key. */
|
|
123
|
-
const SERVER_FN_IGNORED_PARAMS = ["skuId", "idsku"];
|
|
124
|
-
/** A server function's payload for its cache key: page paths without the variant params. */
|
|
125
|
-
export function canonicalizeServerFnPayload(payload) {
|
|
126
|
-
const stripQuery = (s) => {
|
|
127
|
-
if (!s.startsWith("/") || s.startsWith("//"))
|
|
128
|
-
return s;
|
|
129
|
-
const q = s.indexOf("?");
|
|
130
|
-
if (q < 0)
|
|
131
|
-
return s;
|
|
132
|
-
const sp = new URLSearchParams(s.slice(q + 1));
|
|
133
|
-
for (const p of SERVER_FN_IGNORED_PARAMS)
|
|
134
|
-
sp.delete(p);
|
|
135
|
-
const rest = sp.toString();
|
|
136
|
-
return rest ? `${s.slice(0, q)}?${rest}` : s.slice(0, q);
|
|
137
|
-
};
|
|
138
|
-
const visit = (v) => {
|
|
139
|
-
if (typeof v === "string")
|
|
140
|
-
return stripQuery(v);
|
|
141
|
-
if (Array.isArray(v))
|
|
142
|
-
return v.map(visit);
|
|
143
|
-
if (v && typeof v === "object")
|
|
144
|
-
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, visit(x)]));
|
|
145
|
-
return v;
|
|
146
|
-
};
|
|
147
|
-
try {
|
|
148
|
-
return JSON.stringify(visit(JSON.parse(payload)));
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
return payload;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
/** The profile of a request: a GET server function takes its page's, any other one is private. */
|
|
155
|
-
export function profileOf(url) {
|
|
156
|
-
if (isServerFnPath(url.pathname)) {
|
|
157
|
-
const page = serverFnPagePath(url);
|
|
158
|
-
return page ? detectCacheProfile(new URL(page, url.origin)) : "private";
|
|
159
|
-
}
|
|
160
|
-
return detectCacheProfile(url);
|
|
161
|
-
}
|
|
162
|
-
//# sourceMappingURL=cacheProfiles.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cacheProfiles.js","sourceRoot":"","sources":["../../src/worker/cacheProfiles.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AA6BH,MAAM,KAAK,GAAgB,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC;AACxD,MAAM,QAAQ,GAAiB,EAAE,IAAI,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC;AAEhF,MAAM,CAAC,MAAM,sBAAsB,GAAqD;IACtF,MAAM,EAAE;QACN,IAAI,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE;QAC3C,OAAO,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE;QAC7C,QAAQ,EAAE,IAAI;KACf;IACD,OAAO,EAAE;QACP,IAAI,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE;QAC1C,OAAO,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE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AAC,GAAG,CAAC,CAAC;AACjC,CAAC"}
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Page cache profiles: how long a response may be served from the edge cache
|
|
3
|
-
* and from the browser, picked per URL. The values and URL rules are v7's
|
|
4
|
-
* (`@decocms/blocks/sdk/cacheHeaders`), which the TanStack storefronts copied
|
|
5
|
-
* unchanged; a site that needs other values passes `cacheProfiles` to
|
|
6
|
-
* `createDecoWorker`.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export type CacheProfileName =
|
|
10
|
-
| "static"
|
|
11
|
-
| "product"
|
|
12
|
-
| "listing"
|
|
13
|
-
| "search"
|
|
14
|
-
| "cart"
|
|
15
|
-
| "private"
|
|
16
|
-
| "none";
|
|
17
|
-
|
|
18
|
-
/** Seconds: `fresh` = served as is; `swr` = served stale while revalidating; `sie` = served stale when the origin fails. */
|
|
19
|
-
export interface CacheWindow {
|
|
20
|
-
fresh: number;
|
|
21
|
-
swr: number;
|
|
22
|
-
sie: number;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface CacheProfile {
|
|
26
|
-
edge: CacheWindow;
|
|
27
|
-
browser: CacheWindow;
|
|
28
|
-
isPublic: boolean;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/** A site's changes to the default profiles: only the values that differ. */
|
|
32
|
-
export type CacheProfileOverrides = Partial<
|
|
33
|
-
Record<CacheProfileName, { edge?: Partial<CacheWindow>; browser?: Partial<CacheWindow> }>
|
|
34
|
-
>;
|
|
35
|
-
|
|
36
|
-
const NEVER: CacheWindow = { fresh: 0, swr: 0, sie: 0 };
|
|
37
|
-
const NO_CACHE: CacheProfile = { edge: NEVER, browser: NEVER, isPublic: false };
|
|
38
|
-
|
|
39
|
-
export const DEFAULT_CACHE_PROFILES: Readonly<Record<CacheProfileName, CacheProfile>> = {
|
|
40
|
-
static: {
|
|
41
|
-
edge: { fresh: 900, swr: 7200, sie: 21600 },
|
|
42
|
-
browser: { fresh: 120, swr: 1800, sie: 7200 },
|
|
43
|
-
isPublic: true,
|
|
44
|
-
},
|
|
45
|
-
product: {
|
|
46
|
-
edge: { fresh: 300, swr: 1800, sie: 7200 },
|
|
47
|
-
browser: { fresh: 60, swr: 600, sie: 3600 },
|
|
48
|
-
isPublic: true,
|
|
49
|
-
},
|
|
50
|
-
listing: {
|
|
51
|
-
edge: { fresh: 120, swr: 900, sie: 3600 },
|
|
52
|
-
browser: { fresh: 30, swr: 300, sie: 1800 },
|
|
53
|
-
isPublic: true,
|
|
54
|
-
},
|
|
55
|
-
search: {
|
|
56
|
-
edge: { fresh: 60, swr: 300, sie: 1800 },
|
|
57
|
-
browser: { fresh: 0, swr: 120, sie: 600 },
|
|
58
|
-
isPublic: true,
|
|
59
|
-
},
|
|
60
|
-
cart: NO_CACHE,
|
|
61
|
-
private: NO_CACHE,
|
|
62
|
-
none: NO_CACHE,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/** The defaults with a site's overrides applied. The private profiles stay private. */
|
|
66
|
-
export function resolveProfiles(
|
|
67
|
-
overrides: CacheProfileOverrides | undefined,
|
|
68
|
-
): Record<CacheProfileName, CacheProfile> {
|
|
69
|
-
const out = { ...DEFAULT_CACHE_PROFILES };
|
|
70
|
-
for (const [name, change] of Object.entries(overrides ?? {}) as [
|
|
71
|
-
CacheProfileName,
|
|
72
|
-
CacheProfileOverrides[CacheProfileName],
|
|
73
|
-
][]) {
|
|
74
|
-
const base = DEFAULT_CACHE_PROFILES[name];
|
|
75
|
-
if (!base || !change || !base.isPublic) continue;
|
|
76
|
-
out[name] = {
|
|
77
|
-
isPublic: true,
|
|
78
|
-
edge: { ...base.edge, ...change.edge },
|
|
79
|
-
browser: { ...base.browser, ...change.browser },
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
return out;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const PRIVATE_NO_CACHE = "private, no-cache, no-store, must-revalidate";
|
|
86
|
-
|
|
87
|
-
/** The browser's `Cache-Control` for a profile. */
|
|
88
|
-
export function cacheControl(profile: CacheProfile): string {
|
|
89
|
-
if (!profile.isPublic || (profile.edge.fresh === 0 && profile.browser.fresh === 0)) {
|
|
90
|
-
return PRIVATE_NO_CACHE;
|
|
91
|
-
}
|
|
92
|
-
const { browser, edge } = profile;
|
|
93
|
-
const parts = ["public", browser.fresh > 0 ? `max-age=${browser.fresh}` : "max-age=0"];
|
|
94
|
-
if (edge.fresh > 0) parts.push(`s-maxage=${edge.fresh}`);
|
|
95
|
-
if (browser.swr > 0) parts.push(`stale-while-revalidate=${browser.swr}`);
|
|
96
|
-
if (browser.sie > 0) parts.push(`stale-if-error=${browser.sie}`);
|
|
97
|
-
return parts.join(", ");
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const PRIVATE_PREFIX_RE = /^\/(cart|checkout|account|login|my-account)(\/|$)/;
|
|
101
|
-
|
|
102
|
-
/** The profile of a page URL. The first rule that matches wins. */
|
|
103
|
-
export function detectCacheProfile(url: URL): CacheProfileName {
|
|
104
|
-
const p = url.pathname;
|
|
105
|
-
if (PRIVATE_PREFIX_RE.test(p)) return "private";
|
|
106
|
-
if (p.startsWith("/api/") || p.startsWith("/deco/") || p.startsWith("/_build")) return "none";
|
|
107
|
-
if (p === "/sitemap.xml") return "static";
|
|
108
|
-
if (p === "/s" || p.startsWith("/s/") || url.searchParams.has("q")) return "search";
|
|
109
|
-
if (p.endsWith("/p")) return "product";
|
|
110
|
-
if (p === "/" || p === "") return "static";
|
|
111
|
-
return "listing";
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function isServerFnPath(pathname: string): boolean {
|
|
115
|
-
return pathname.startsWith("/_serverFn/") || pathname.startsWith("/_server/");
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* The page a GET server-function request loads, from its `payload`: the first
|
|
120
|
-
* string in it that is a local path. `null` for any other server function.
|
|
121
|
-
*/
|
|
122
|
-
export function serverFnPagePath(url: URL): string | null {
|
|
123
|
-
if (!isServerFnPath(url.pathname)) return null;
|
|
124
|
-
const payload = url.searchParams.get("payload");
|
|
125
|
-
if (!payload) return null;
|
|
126
|
-
let found: string | null = null;
|
|
127
|
-
const visit = (v: unknown): void => {
|
|
128
|
-
if (found !== null) return;
|
|
129
|
-
if (typeof v === "string") {
|
|
130
|
-
if (v.startsWith("/") && !v.startsWith("//")) found = v;
|
|
131
|
-
} else if (Array.isArray(v)) {
|
|
132
|
-
for (const x of v) visit(x);
|
|
133
|
-
} else if (v && typeof v === "object") {
|
|
134
|
-
for (const x of Object.values(v)) visit(x);
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
try {
|
|
138
|
-
visit(JSON.parse(payload));
|
|
139
|
-
} catch {
|
|
140
|
-
return null;
|
|
141
|
-
}
|
|
142
|
-
return found;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/** Variant params a page loader ignores, left out of a server function's cache key. */
|
|
146
|
-
const SERVER_FN_IGNORED_PARAMS = ["skuId", "idsku"];
|
|
147
|
-
|
|
148
|
-
/** A server function's payload for its cache key: page paths without the variant params. */
|
|
149
|
-
export function canonicalizeServerFnPayload(payload: string): string {
|
|
150
|
-
const stripQuery = (s: string): string => {
|
|
151
|
-
if (!s.startsWith("/") || s.startsWith("//")) return s;
|
|
152
|
-
const q = s.indexOf("?");
|
|
153
|
-
if (q < 0) return s;
|
|
154
|
-
const sp = new URLSearchParams(s.slice(q + 1));
|
|
155
|
-
for (const p of SERVER_FN_IGNORED_PARAMS) sp.delete(p);
|
|
156
|
-
const rest = sp.toString();
|
|
157
|
-
return rest ? `${s.slice(0, q)}?${rest}` : s.slice(0, q);
|
|
158
|
-
};
|
|
159
|
-
const visit = (v: unknown): unknown => {
|
|
160
|
-
if (typeof v === "string") return stripQuery(v);
|
|
161
|
-
if (Array.isArray(v)) return v.map(visit);
|
|
162
|
-
if (v && typeof v === "object")
|
|
163
|
-
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, visit(x)]));
|
|
164
|
-
return v;
|
|
165
|
-
};
|
|
166
|
-
try {
|
|
167
|
-
return JSON.stringify(visit(JSON.parse(payload)));
|
|
168
|
-
} catch {
|
|
169
|
-
return payload;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/** The profile of a request: a GET server function takes its page's, any other one is private. */
|
|
174
|
-
export function profileOf(url: URL): CacheProfileName {
|
|
175
|
-
if (isServerFnPath(url.pathname)) {
|
|
176
|
-
const page = serverFnPagePath(url);
|
|
177
|
-
return page ? detectCacheProfile(new URL(page, url.origin)) : "private";
|
|
178
|
-
}
|
|
179
|
-
return detectCacheProfile(url);
|
|
180
|
-
}
|