@decocms/tanstack 7.83.1 → 7.84.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/sdk/kvRedirects.test.ts +9 -0
- package/src/sdk/kvRedirects.ts +16 -0
- package/src/sdk/workerEntry.test.ts +140 -1
- package/src/sdk/workerEntry.ts +77 -38
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/tanstack",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.84.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.84.0",
|
|
31
|
+
"@decocms/blocks-admin": "7.84.0",
|
|
32
|
+
"@decocms/blocks-cli": "7.84.0",
|
|
33
33
|
"@deco-cx/warp-node": "^0.3.16",
|
|
34
34
|
"fast-json-patch": "^3.1.0",
|
|
35
35
|
"ws": "^8.18.0"
|
|
@@ -35,6 +35,15 @@ describe("lookupExactRedirect", () => {
|
|
|
35
35
|
});
|
|
36
36
|
});
|
|
37
37
|
|
|
38
|
+
it("only navigations read KV: POST/PUT skip the lookup entirely", async () => {
|
|
39
|
+
const kv = makeKV({ [`redirect:${ID}:/old`]: '{"to":"/new","status":301}' });
|
|
40
|
+
await expect(lookupExactRedirect(enabled(kv), "/old", "POST")).resolves.toBeNull();
|
|
41
|
+
await expect(lookupExactRedirect(enabled(kv), "/old", "PUT")).resolves.toBeNull();
|
|
42
|
+
expect(kv.get).not.toHaveBeenCalled();
|
|
43
|
+
await expect(lookupExactRedirect(enabled(kv), "/old", "HEAD")).resolves.toMatchObject({ to: "/new" });
|
|
44
|
+
expect(kv.get).toHaveBeenCalledTimes(1);
|
|
45
|
+
});
|
|
46
|
+
|
|
38
47
|
it("returns null for a missing key", async () => {
|
|
39
48
|
await expect(lookupExactRedirect(enabled(makeKV()), "/nope")).resolves.toBeNull();
|
|
40
49
|
});
|
package/src/sdk/kvRedirects.ts
CHANGED
|
@@ -91,11 +91,27 @@ export function clearRedirectCache(): void {
|
|
|
91
91
|
* cannot run (fast-deploy off, no deployment id, KV error). A KV failure must
|
|
92
92
|
* not 5xx a page that would otherwise render: the worst outcome is the request
|
|
93
93
|
* proceeds and the URL serves its normal response.
|
|
94
|
+
*
|
|
95
|
+
* Only navigations (GET/HEAD) read KV. A content redirect is a navigation
|
|
96
|
+
* concept, and the rest of the traffic — the commerce proxy's cart/checkout
|
|
97
|
+
* POSTs above all — has a fresh path per request (`/orderForm/<id>/…`), so the
|
|
98
|
+
* isolate memo never hits and every one paid a serial KV miss (p75 ~160ms on
|
|
99
|
+
* a production storefront) before reaching its handler. In-memory rules
|
|
100
|
+
* (exact and glob) still apply to every method; only the KV read is gated.
|
|
101
|
+
*
|
|
102
|
+
* Trade-off: a temporary rule is served as 307, which browsers follow with the
|
|
103
|
+
* original method, so a KV-only 307 on a form/webhook path used to forward the
|
|
104
|
+
* POST and now lets it reach that path's handler. On a non-fast-deploy site
|
|
105
|
+
* the same rule is in memory and still redirects POSTs — the behaviour differs
|
|
106
|
+
* by deploy mode for that one case. Content redirects are navigation-only in
|
|
107
|
+
* practice; a site that redirects POST endpoints should do it in code.
|
|
94
108
|
*/
|
|
95
109
|
export async function lookupExactRedirect(
|
|
96
110
|
env: Record<string, unknown>,
|
|
97
111
|
pathname: string,
|
|
112
|
+
method = "GET",
|
|
98
113
|
): Promise<Redirect | null> {
|
|
114
|
+
if (method !== "GET" && method !== "HEAD") return null;
|
|
99
115
|
const kv = getFastDeployKV(env);
|
|
100
116
|
if (!kv) return null;
|
|
101
117
|
|
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
computeRevision,
|
|
3
|
+
redirectKey,
|
|
4
|
+
redirectPrefix,
|
|
5
|
+
revisionKey,
|
|
6
|
+
setBlocks,
|
|
7
|
+
snapshotKey,
|
|
8
|
+
} from "@decocms/blocks/cms";
|
|
2
9
|
import {
|
|
3
10
|
__resetAutoconfigStateForTests,
|
|
4
11
|
autoconfigApps,
|
|
@@ -6,6 +13,7 @@ import {
|
|
|
6
13
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
7
14
|
import { segmentToken } from "./cdnSegment";
|
|
8
15
|
import { __resetKvHydrationStateForTests } from "./kvHydration";
|
|
16
|
+
import { clearRedirectCache } from "./kvRedirects";
|
|
9
17
|
import {
|
|
10
18
|
aliasLiveInvoke,
|
|
11
19
|
buildEnforcedCsp,
|
|
@@ -433,6 +441,137 @@ describe("CMS redirects", () => {
|
|
|
433
441
|
});
|
|
434
442
|
});
|
|
435
443
|
|
|
444
|
+
describe("CMS redirects on a fast-deploy site (exact rules in KV)", () => {
|
|
445
|
+
const ID = "sha-redirects";
|
|
446
|
+
// Adding to the cart through the commerce proxy: the path is new for every
|
|
447
|
+
// cart, so a KV read for it is always a cold one.
|
|
448
|
+
const CART_ITEMS = "/api/checkout/pub/orderForm/5f1a0c2e9d/items";
|
|
449
|
+
|
|
450
|
+
beforeEach(() => {
|
|
451
|
+
__resetKvHydrationStateForTests();
|
|
452
|
+
clearRedirectCache();
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
afterEach(() => {
|
|
456
|
+
__resetKvHydrationStateForTests();
|
|
457
|
+
clearRedirectCache();
|
|
458
|
+
setBlocks({});
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* A fast-deploy env: KV holds the decofile (`blocks`) and one exact rule,
|
|
463
|
+
* `/old` → `/new`, under its own key, as the sync script writes it. Records
|
|
464
|
+
* every `redirect:` read with the options it was made with.
|
|
465
|
+
*/
|
|
466
|
+
function fastDeploy(blocks: Record<string, unknown> = {}) {
|
|
467
|
+
const store = new Map<string, string>([
|
|
468
|
+
[snapshotKey(ID), JSON.stringify(blocks)],
|
|
469
|
+
[revisionKey(ID), computeRevision(blocks)],
|
|
470
|
+
[redirectKey(ID, "/old"), JSON.stringify({ from: "/old", to: "/new", status: 301 })],
|
|
471
|
+
]);
|
|
472
|
+
const redirectReads: Array<{ key: string }> = [];
|
|
473
|
+
const env = {
|
|
474
|
+
DECO_KV: {
|
|
475
|
+
get: (key: string) => {
|
|
476
|
+
if (key.startsWith(redirectPrefix(ID))) redirectReads.push({ key });
|
|
477
|
+
return Promise.resolve(store.get(key) ?? null);
|
|
478
|
+
},
|
|
479
|
+
put: () => Promise.resolve(),
|
|
480
|
+
delete: () => Promise.resolve(),
|
|
481
|
+
},
|
|
482
|
+
DECO_FAST_DEPLOY: "1",
|
|
483
|
+
DECO_DEPLOYMENT_ID: ID,
|
|
484
|
+
};
|
|
485
|
+
return { env, redirectReads };
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/** The site's commerce proxy: answers `/api/*` itself. */
|
|
489
|
+
const proxyHandler = (_request: Request, url: URL) =>
|
|
490
|
+
url.pathname.startsWith("/api/") ? new Response("proxied", { status: 200 }) : null;
|
|
491
|
+
|
|
492
|
+
it("a POST to a cart path reaches the proxy without a KV read", async () => {
|
|
493
|
+
const { env, redirectReads } = fastDeploy();
|
|
494
|
+
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false, proxyHandler });
|
|
495
|
+
|
|
496
|
+
const res = await worker.fetch(
|
|
497
|
+
new Request(`https://example.com${CART_ITEMS}`, { method: "POST", body: "{}" }),
|
|
498
|
+
env,
|
|
499
|
+
MOCK_CTX,
|
|
500
|
+
);
|
|
501
|
+
|
|
502
|
+
expect(await res.text()).toBe("proxied");
|
|
503
|
+
expect(redirectReads).toEqual([]);
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it("a GET still redirects from the exact rule in KV", async () => {
|
|
507
|
+
const { env, redirectReads } = fastDeploy();
|
|
508
|
+
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false, proxyHandler });
|
|
509
|
+
|
|
510
|
+
const res = await worker.fetch(new Request("https://example.com/old"), env, MOCK_CTX);
|
|
511
|
+
|
|
512
|
+
expect(res.status).toBe(301);
|
|
513
|
+
expect(res.headers.get("Location")).toBe("/new");
|
|
514
|
+
expect(redirectReads).toEqual([{ key: redirectKey(ID, "/old") }]);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it("redirects.skip: a skipped request is not checked at all, in KV or in memory", async () => {
|
|
518
|
+
// A glob that would catch the proxied path: it stays in the decofile, so it
|
|
519
|
+
// is matched in memory, not in KV. `skip` must keep it from answering too.
|
|
520
|
+
const { env, redirectReads } = fastDeploy({
|
|
521
|
+
"redirect-1": {
|
|
522
|
+
__resolveType: "website/loaders/redirects.ts",
|
|
523
|
+
redirects: [{ from: "/api/*", to: "/gone", type: "permanent" }],
|
|
524
|
+
},
|
|
525
|
+
});
|
|
526
|
+
const asked: Array<[string, string]> = [];
|
|
527
|
+
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, {
|
|
528
|
+
observability: false,
|
|
529
|
+
proxyHandler,
|
|
530
|
+
redirects: {
|
|
531
|
+
skip: (url, request) => {
|
|
532
|
+
asked.push([url.pathname, request.method]);
|
|
533
|
+
return url.pathname.startsWith("/api/");
|
|
534
|
+
},
|
|
535
|
+
},
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
const api = await worker.fetch(
|
|
539
|
+
new Request("https://example.com/api/catalog_system/pub/products/search"),
|
|
540
|
+
env,
|
|
541
|
+
MOCK_CTX,
|
|
542
|
+
);
|
|
543
|
+
expect(await api.text()).toBe("proxied");
|
|
544
|
+
expect(redirectReads).toEqual([]);
|
|
545
|
+
|
|
546
|
+
// Every other request is checked as before.
|
|
547
|
+
const old = await worker.fetch(new Request("https://example.com/old"), env, MOCK_CTX);
|
|
548
|
+
expect(old.status).toBe(301);
|
|
549
|
+
expect(old.headers.get("Location")).toBe("/new");
|
|
550
|
+
expect(redirectReads.map((read) => read.key)).toEqual([redirectKey(ID, "/old")]);
|
|
551
|
+
|
|
552
|
+
expect(asked).toEqual([
|
|
553
|
+
["/api/catalog_system/pub/products/search", "GET"],
|
|
554
|
+
["/old", "GET"],
|
|
555
|
+
]);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it("without redirects.skip, the in-memory glob still answers on a proxied path", async () => {
|
|
559
|
+
// The default is unchanged: only a site that opts in loses the check.
|
|
560
|
+
const { env } = fastDeploy({
|
|
561
|
+
"redirect-1": {
|
|
562
|
+
__resolveType: "website/loaders/redirects.ts",
|
|
563
|
+
redirects: [{ from: "/api/*", to: "/gone", type: "permanent" }],
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
const worker = createDecoWorkerEntry(MOCK_SERVER_ENTRY, { observability: false, proxyHandler });
|
|
567
|
+
|
|
568
|
+
const res = await worker.fetch(new Request("https://example.com/api/x"), env, MOCK_CTX);
|
|
569
|
+
|
|
570
|
+
expect(res.status).toBe(301);
|
|
571
|
+
expect(res.headers.get("Location")).toBe("/gone");
|
|
572
|
+
});
|
|
573
|
+
});
|
|
574
|
+
|
|
436
575
|
describe("degraded origin (anti-cache-poisoning)", () => {
|
|
437
576
|
afterEach(() => {
|
|
438
577
|
setBlocks({});
|
package/src/sdk/workerEntry.ts
CHANGED
|
@@ -657,21 +657,52 @@ export interface DecoWorkerEntryOptions {
|
|
|
657
657
|
outboundUserAgent?: string | false;
|
|
658
658
|
|
|
659
659
|
/**
|
|
660
|
-
*
|
|
661
|
-
* match and answer
|
|
662
|
-
*
|
|
663
|
-
* - `"legacy"` (default): case- and trailing-slash-insensitive match,
|
|
664
|
-
* temporary → 302, the request's query dropped from `Location`. What every
|
|
665
|
-
* `@decocms/*` site has answered so far.
|
|
666
|
-
* - `"fresh"`: what the Fresh site a migration came from answered — source
|
|
667
|
-
* matched byte for byte (`pathname + search` first), temporary → 307, the
|
|
668
|
-
* request's query appended to `Location` unless `discardQueryParameters`.
|
|
669
|
-
*
|
|
670
|
-
* Both drop self-redirects and never double-encode `Location`.
|
|
660
|
+
* CMS redirects (`website/loaders/redirect(s).ts`, `redirectsFromCsv`): how
|
|
661
|
+
* they match and answer, and which requests check them.
|
|
671
662
|
*
|
|
672
663
|
* @default { semantics: "legacy" }
|
|
673
664
|
*/
|
|
674
|
-
redirects?: {
|
|
665
|
+
redirects?: {
|
|
666
|
+
/**
|
|
667
|
+
* How the rules match and answer — see `@decocms/blocks/sdk/redirects`.
|
|
668
|
+
*
|
|
669
|
+
* - `"legacy"` (default): case- and trailing-slash-insensitive match,
|
|
670
|
+
* temporary → 302, the request's query dropped from `Location`. What every
|
|
671
|
+
* `@decocms/*` site has answered so far.
|
|
672
|
+
* - `"fresh"`: what the Fresh site a migration came from answered — source
|
|
673
|
+
* matched byte for byte (`pathname + search` first), temporary → 307, the
|
|
674
|
+
* request's query appended to `Location` unless `discardQueryParameters`.
|
|
675
|
+
*
|
|
676
|
+
* Both drop self-redirects and never double-encode `Location`.
|
|
677
|
+
*/
|
|
678
|
+
semantics?: RedirectSemantics;
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* Requests that are never a CMS redirect source — typically the paths
|
|
682
|
+
* `proxyHandler` forwards to the commerce platform. Return `true` and the
|
|
683
|
+
* worker does not check CMS redirects for that request at all: no KV read
|
|
684
|
+
* on a fast-deploy site (a cart path is new on every request, so each one
|
|
685
|
+
* paid a cold read before reaching the proxy), and no in-memory rule
|
|
686
|
+
* either, so the answer for those paths does not depend on the deploy mode.
|
|
687
|
+
*
|
|
688
|
+
* Runs on nearly every request, ahead of `?asJson`, the proxy and the page:
|
|
689
|
+
* keep it a cheap, synchronous test of the URL. Unset, every request is
|
|
690
|
+
* checked. Match whole path segments: a bare `/login` prefix also matches
|
|
691
|
+
* a page such as `/loginpedido.aspx`, whose CMS redirect would then never
|
|
692
|
+
* answer.
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* ```ts
|
|
696
|
+
* // What proxyHandler forwards to the commerce platform.
|
|
697
|
+
* const PROXIED = /^\/(?:api|files|arquivos|_v)\/|^\/(?:checkout|account|login)(?:\/|$)/;
|
|
698
|
+
*
|
|
699
|
+
* createDecoWorkerEntry(serverEntry, {
|
|
700
|
+
* redirects: { skip: (url) => PROXIED.test(url.pathname) },
|
|
701
|
+
* });
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
skip?: (url: URL, request: Request) => boolean;
|
|
705
|
+
};
|
|
675
706
|
}
|
|
676
707
|
|
|
677
708
|
// ---------------------------------------------------------------------------
|
|
@@ -1306,6 +1337,7 @@ export function createDecoWorkerEntry(
|
|
|
1306
1337
|
// Before the first request builds the redirect map — the map keys its rules
|
|
1307
1338
|
// for the semantics in force when it is built.
|
|
1308
1339
|
setRedirectSemantics(redirectsOpt?.semantics ?? "legacy");
|
|
1340
|
+
const skipCmsRedirects = redirectsOpt?.skip;
|
|
1309
1341
|
|
|
1310
1342
|
// Speculation Rules — store the site-wide config in the shared singleton so
|
|
1311
1343
|
// DecoRootLayout (SSR) emits the tag in <head>. Inert unless the site opts in.
|
|
@@ -2475,32 +2507,39 @@ export function createDecoWorkerEntry(
|
|
|
2475
2507
|
}
|
|
2476
2508
|
|
|
2477
2509
|
// CMS redirects (website/loaders/redirect.ts blocks) — checked before
|
|
2478
|
-
// ?asJson and commerce proxy so every request path respects redirects
|
|
2479
|
-
//
|
|
2480
|
-
//
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2510
|
+
// ?asJson and commerce proxy so every request path respects redirects,
|
|
2511
|
+
// except requests the site's `redirects.skip` marks as never a redirect
|
|
2512
|
+
// source (typically its proxied paths).
|
|
2513
|
+
if (!skipCmsRedirects?.(url, request)) {
|
|
2514
|
+
// Revision-keyed so cross-bundle setBlocks() calls are detected even when
|
|
2515
|
+
// onChange listeners don't fire across Vite split-bundle module instances.
|
|
2516
|
+
// The semantics are part of the key: a map keys its rules for the
|
|
2517
|
+
// semantics in force when it is built.
|
|
2518
|
+
const currentRevision = `${getRevision()}:${getRedirectSemantics()}`;
|
|
2519
|
+
if (_redirectMapRevision !== currentRevision) {
|
|
2520
|
+
_redirectMap = loadRedirects(loadBlocks());
|
|
2521
|
+
_redirectMapRevision = currentRevision;
|
|
2522
|
+
}
|
|
2523
|
+
// Exact before glob, across BOTH sources. On a fast-deploy site the exact
|
|
2524
|
+
// rules live under their own `redirect:<id>:<path>` KV keys and are absent
|
|
2525
|
+
// from the decofile, so the in-memory exact hit is the non-fast-deploy
|
|
2526
|
+
// case. Going straight to `matchRedirect` and only then to KV would let a
|
|
2527
|
+
// glob win over an exact rule, inverting the precedence.
|
|
2528
|
+
// Query-scoped rules (`/x?map=ft`) live in memory only, so the KV lookup
|
|
2529
|
+
// stays one read, keyed by pathname.
|
|
2530
|
+
// That precedence holds for navigations (GET/HEAD) only: other methods
|
|
2531
|
+
// skip the KV read (see `lookupExactRedirect`), so for a POST a glob can
|
|
2532
|
+
// answer where a KV-only exact rule would have.
|
|
2533
|
+
const cmsRedirect =
|
|
2534
|
+
matchExactRedirect(url.pathname, _redirectMap!, url.search) ??
|
|
2535
|
+
(await lookupExactRedirect(env as Record<string, unknown>, url.pathname, request.method)) ??
|
|
2536
|
+
matchPatternRedirect(url.pathname, _redirectMap!);
|
|
2537
|
+
if (cmsRedirect) {
|
|
2538
|
+
return new Response(null, {
|
|
2539
|
+
status: cmsRedirect.status,
|
|
2540
|
+
headers: { Location: redirectLocation(cmsRedirect, url.search) },
|
|
2541
|
+
});
|
|
2542
|
+
}
|
|
2504
2543
|
}
|
|
2505
2544
|
|
|
2506
2545
|
// ?renderJson — structured JSON of the page for the mobile app. Lean shape
|