@decocms/start 4.5.0 → 4.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/.cursor/skills/deco-edge-caching/SKILL.md +14 -10
- package/package.json +1 -1
- package/src/sdk/workerEntry.ts +39 -21
- package/src/vite/plugin.js +55 -0
- package/src/vite/plugin.test.js +60 -1
- package/vitest.config.ts +1 -1
|
@@ -183,17 +183,21 @@ This is per-isolate in-memory cache (V8 Map). Resets on cold start. Includes req
|
|
|
183
183
|
|
|
184
184
|
## Cache Versioning with BUILD_HASH
|
|
185
185
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
186
|
+
Every cached entry's key is suffixed with `__v=<hash>` so a new deploy starts a fresh cache namespace and previously-cached HTML (which references now-deleted asset filenames like `/assets/main-XYZ.js`) stops being served the moment the new worker is live. Old entries become orphaned and expire naturally — no purge endpoint call required.
|
|
187
|
+
|
|
188
|
+
The hash is resolved automatically by `decoVitePlugin()` at build time and injected into the worker bundle as `__DECO_BUILD_HASH__`. Resolution order:
|
|
189
|
+
|
|
190
|
+
1. `WORKERS_CI_COMMIT_SHA` — Cloudflare Workers Builds default env var ([CF docs](https://developers.cloudflare.com/changelog/2025-06-10-default-env-vars/)). This is the production deploy path-of-record per `MIGRATION_TOOLING_PLAN.md` D6.3.
|
|
191
|
+
2. `git rev-parse --short=12 HEAD` — for someone running `wrangler deploy` from a developer laptop.
|
|
192
|
+
3. `Date.now().toString(36)` — last-resort fallback so the cache-bust invariant never silently regresses.
|
|
193
|
+
|
|
194
|
+
`createDecoWorkerEntry` reads `env.BUILD_HASH` first (explicit override path, e.g. `wrangler deploy --var BUILD_HASH:foo`) and falls back to the `__DECO_BUILD_HASH__` constant. Sites running `decoVitePlugin()` get the behaviour for free — **no per-site dashboard, `wrangler.jsonc`, or `--var` configuration required**.
|
|
195
195
|
|
|
196
|
-
The
|
|
196
|
+
The active version is exposed on every cached response via the `X-Cache-Version` header for observability. Confirm a new deploy is shipping the right hash with:
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
curl -sI https://www.example.com/ | grep -i x-cache-version
|
|
200
|
+
```
|
|
197
201
|
|
|
198
202
|
## Site-Level Cache Pattern Registration
|
|
199
203
|
|
package/package.json
CHANGED
package/src/sdk/workerEntry.ts
CHANGED
|
@@ -45,6 +45,17 @@ import { getAppMiddleware } from "./setupApps";
|
|
|
45
45
|
import { cleanPathForCacheKey } from "./urlUtils";
|
|
46
46
|
import { type Device, isMobileUA } from "./useDevice";
|
|
47
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Build-time identifier injected by `decoVitePlugin()` (see
|
|
50
|
+
* `src/vite/plugin.js`). Falls back to `undefined` if the consuming site
|
|
51
|
+
* isn't using the plugin or the symbol wasn't `define`d at bundle time.
|
|
52
|
+
*
|
|
53
|
+
* The runtime `env.BUILD_HASH` (when explicitly set, e.g. via
|
|
54
|
+
* `wrangler deploy --var BUILD_HASH:foo`) takes precedence — see
|
|
55
|
+
* `getBuildHash()` below.
|
|
56
|
+
*/
|
|
57
|
+
declare const __DECO_BUILD_HASH__: string | undefined;
|
|
58
|
+
|
|
48
59
|
/**
|
|
49
60
|
* Append Link preload headers for CSS and fonts so the browser starts
|
|
50
61
|
* fetching them before parsing HTML. Only applied to HTML responses.
|
|
@@ -673,6 +684,23 @@ export function createDecoWorkerEntry(
|
|
|
673
684
|
return parts.join("|");
|
|
674
685
|
}
|
|
675
686
|
|
|
687
|
+
/**
|
|
688
|
+
* Resolve the per-deploy cache-key version with this priority:
|
|
689
|
+
* 1. `env[cacheVersionEnv]` — explicit override (e.g. `wrangler
|
|
690
|
+
* deploy --var BUILD_HASH:foo`). Wins so callers can always
|
|
691
|
+
* force a specific value.
|
|
692
|
+
* 2. `__DECO_BUILD_HASH__` — build-time constant injected by
|
|
693
|
+
* `decoVitePlugin()` from WORKERS_CI_COMMIT_SHA / git rev-parse.
|
|
694
|
+
* This is the production path on Cloudflare Workers Builds.
|
|
695
|
+
* 3. Empty string — versioning disabled (legacy pre-plugin sites).
|
|
696
|
+
*/
|
|
697
|
+
function getBuildHash(env: Record<string, unknown>): string {
|
|
698
|
+
if (cacheVersionEnv === false) return "";
|
|
699
|
+
const fromEnv = (env[cacheVersionEnv] as string) || "";
|
|
700
|
+
if (fromEnv) return fromEnv;
|
|
701
|
+
return typeof __DECO_BUILD_HASH__ !== "undefined" ? __DECO_BUILD_HASH__ : "";
|
|
702
|
+
}
|
|
703
|
+
|
|
676
704
|
function buildCacheKey(
|
|
677
705
|
request: Request,
|
|
678
706
|
env: Record<string, unknown>,
|
|
@@ -685,11 +713,9 @@ export function createDecoWorkerEntry(
|
|
|
685
713
|
url.search = cleanUrl.search;
|
|
686
714
|
}
|
|
687
715
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
url.searchParams.set("__v", version);
|
|
692
|
-
}
|
|
716
|
+
const version = getBuildHash(env);
|
|
717
|
+
if (version) {
|
|
718
|
+
url.searchParams.set("__v", version);
|
|
693
719
|
}
|
|
694
720
|
|
|
695
721
|
// Include CF geo data in cache key so location matcher results don't leak
|
|
@@ -791,10 +817,8 @@ export function createDecoWorkerEntry(
|
|
|
791
817
|
for (const seg of segments) {
|
|
792
818
|
for (const cc of geoKeys) {
|
|
793
819
|
const url = new URL(p, baseUrl);
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
if (version) url.searchParams.set("__v", version);
|
|
797
|
-
}
|
|
820
|
+
const purgeVersion = getBuildHash(env);
|
|
821
|
+
if (purgeVersion) url.searchParams.set("__v", purgeVersion);
|
|
798
822
|
url.searchParams.set("__seg", hashSegment(seg));
|
|
799
823
|
if (cc) url.searchParams.set("__cf_geo", cc);
|
|
800
824
|
const key = new Request(url.toString(), { method: "GET" });
|
|
@@ -816,10 +840,8 @@ export function createDecoWorkerEntry(
|
|
|
816
840
|
for (const device of devices) {
|
|
817
841
|
for (const cc of geoKeys) {
|
|
818
842
|
const url = new URL(p, baseUrl);
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
if (version) url.searchParams.set("__v", version);
|
|
822
|
-
}
|
|
843
|
+
const purgeVersion = getBuildHash(env);
|
|
844
|
+
if (purgeVersion) url.searchParams.set("__v", purgeVersion);
|
|
823
845
|
if (device) url.searchParams.set("__cf_device", device);
|
|
824
846
|
if (cc) url.searchParams.set("__cf_geo", cc);
|
|
825
847
|
const key = new Request(url.toString(), { method: "GET" });
|
|
@@ -1190,10 +1212,8 @@ export function createDecoWorkerEntry(
|
|
|
1190
1212
|
// different regions or channels get separate cache entries.
|
|
1191
1213
|
const cacheKeyUrl = new URL(request.url);
|
|
1192
1214
|
cacheKeyUrl.searchParams.set("__body", bodyHash);
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
if (version) cacheKeyUrl.searchParams.set("__v", version);
|
|
1196
|
-
}
|
|
1215
|
+
const sfnVersion = getBuildHash(env);
|
|
1216
|
+
if (sfnVersion) cacheKeyUrl.searchParams.set("__v", sfnVersion);
|
|
1197
1217
|
if (sfnSegment) {
|
|
1198
1218
|
cacheKeyUrl.searchParams.set("__seg", hashSegment(sfnSegment));
|
|
1199
1219
|
} else if (deviceSpecificKeys) {
|
|
@@ -1409,10 +1429,8 @@ export function createDecoWorkerEntry(
|
|
|
1409
1429
|
out.headers.set("X-Cache", xCache);
|
|
1410
1430
|
out.headers.set("X-Cache-Profile", profile);
|
|
1411
1431
|
if (segment) out.headers.set("X-Cache-Segment", hashSegment(segment));
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
if (v) out.headers.set("X-Cache-Version", v);
|
|
1415
|
-
}
|
|
1432
|
+
const headerVersion = getBuildHash(env);
|
|
1433
|
+
if (headerVersion) out.headers.set("X-Cache-Version", headerVersion);
|
|
1416
1434
|
if (extra) for (const [k, v] of Object.entries(extra)) out.headers.set(k, v);
|
|
1417
1435
|
appendResourceHints(out);
|
|
1418
1436
|
return out;
|
package/src/vite/plugin.js
CHANGED
|
@@ -31,8 +31,49 @@
|
|
|
31
31
|
* export default defineConfig({ plugins: [decoVitePlugin(), ...] });
|
|
32
32
|
* ```
|
|
33
33
|
*/
|
|
34
|
+
import { execFileSync } from "node:child_process";
|
|
34
35
|
import { existsSync, readFileSync } from "node:fs";
|
|
35
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Resolve a per-build identifier for cache-key versioning.
|
|
39
|
+
*
|
|
40
|
+
* The returned string is injected into the worker bundle as the
|
|
41
|
+
* `__DECO_BUILD_HASH__` global via Vite `define`. `createDecoWorkerEntry`
|
|
42
|
+
* appends it (or `env.BUILD_HASH` if explicitly set) as `__v=<hash>` on
|
|
43
|
+
* every Cache API key, so each new deploy gets its own cache namespace
|
|
44
|
+
* — old edge-cached HTML referencing dead asset filenames stops being
|
|
45
|
+
* served the moment the new worker is live.
|
|
46
|
+
*
|
|
47
|
+
* Resolution order:
|
|
48
|
+
* 1. WORKERS_CI_COMMIT_SHA — Cloudflare Workers Builds default env var
|
|
49
|
+
* (the production deploy path-of-record). Sliced to 12 chars.
|
|
50
|
+
* 2. `git rev-parse --short=12 HEAD` — local `wrangler deploy` from a
|
|
51
|
+
* developer laptop. Try/catch so missing git or shallow clones don't
|
|
52
|
+
* fail the build.
|
|
53
|
+
* 3. `Date.now().toString(36)` — last-resort fallback so the cache-bust
|
|
54
|
+
* invariant never silently regresses to "always the same key".
|
|
55
|
+
*
|
|
56
|
+
* For dev (`command !== "build"`), the value is the literal `"dev"`.
|
|
57
|
+
*
|
|
58
|
+
* @returns {string}
|
|
59
|
+
*/
|
|
60
|
+
function resolveBuildHash() {
|
|
61
|
+
const ciSha = process.env.WORKERS_CI_COMMIT_SHA;
|
|
62
|
+
if (ciSha?.trim()) return ciSha.trim().slice(0, 12);
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const sha = execFileSync("git", ["rev-parse", "--short=12", "HEAD"], {
|
|
66
|
+
encoding: "utf-8",
|
|
67
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
68
|
+
}).trim();
|
|
69
|
+
if (sha) return sha;
|
|
70
|
+
} catch {
|
|
71
|
+
// git absent, not a repo, or shallow clone w/o history — fall through.
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return Date.now().toString(36);
|
|
75
|
+
}
|
|
76
|
+
|
|
36
77
|
// Bare-specifier stubs resolved by ID before Vite touches them.
|
|
37
78
|
/** @type {Record<string, string>} */
|
|
38
79
|
const CLIENT_STUBS = {
|
|
@@ -227,6 +268,20 @@ export function decoVitePlugin() {
|
|
|
227
268
|
};
|
|
228
269
|
}
|
|
229
270
|
|
|
271
|
+
// Inject a per-build identifier as `__DECO_BUILD_HASH__` so
|
|
272
|
+
// createDecoWorkerEntry can fall back to it when env.BUILD_HASH is
|
|
273
|
+
// unset (the default on Cloudflare Workers Builds, where there's
|
|
274
|
+
// no GH-Actions step injecting --var BUILD_HASH).
|
|
275
|
+
//
|
|
276
|
+
// Dev gets the literal "dev" so SSR doesn't crash on an undefined
|
|
277
|
+
// identifier; prod gets WORKERS_CI_COMMIT_SHA → git rev-parse →
|
|
278
|
+
// time-based fallback (see resolveBuildHash above).
|
|
279
|
+
const buildHash = command === "build" ? resolveBuildHash() : "dev";
|
|
280
|
+
cfg.define = {
|
|
281
|
+
...cfg.define,
|
|
282
|
+
__DECO_BUILD_HASH__: JSON.stringify(buildHash),
|
|
283
|
+
};
|
|
284
|
+
|
|
230
285
|
// Only split chunks for production builds — dev uses unbundled ESM.
|
|
231
286
|
if (command !== "build") return cfg;
|
|
232
287
|
return {
|
package/src/vite/plugin.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, expect, it } from "vitest";
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { decoVitePlugin } from "./plugin.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -52,3 +52,62 @@ describe("decoVitePlugin client stubs (regression guard)", () => {
|
|
|
52
52
|
expect(id).toBeUndefined();
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
+
|
|
56
|
+
describe("decoVitePlugin __DECO_BUILD_HASH__ injection", () => {
|
|
57
|
+
let originalEnv;
|
|
58
|
+
|
|
59
|
+
beforeEach(() => {
|
|
60
|
+
originalEnv = { ...process.env };
|
|
61
|
+
delete process.env.WORKERS_CI_COMMIT_SHA;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
afterEach(() => {
|
|
65
|
+
process.env = originalEnv;
|
|
66
|
+
vi.restoreAllMocks();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
function callConfig(command) {
|
|
70
|
+
const p = getPlugin();
|
|
71
|
+
return p.config({}, { command });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
it("injects the literal 'dev' for non-build commands", () => {
|
|
75
|
+
const cfg = callConfig("serve");
|
|
76
|
+
expect(cfg.define.__DECO_BUILD_HASH__).toBe(JSON.stringify("dev"));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("uses WORKERS_CI_COMMIT_SHA (sliced to 12 chars) when set on a build", () => {
|
|
80
|
+
process.env.WORKERS_CI_COMMIT_SHA = "abcdef1234567890fedcba";
|
|
81
|
+
const cfg = callConfig("build");
|
|
82
|
+
expect(cfg.define.__DECO_BUILD_HASH__).toBe(JSON.stringify("abcdef123456"));
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("falls back to git rev-parse when WORKERS_CI_COMMIT_SHA is unset", async () => {
|
|
86
|
+
// The plugin module imports execFileSync at top-level, so we can't easily
|
|
87
|
+
// mock it after the fact. Instead, exercise the real git binary against
|
|
88
|
+
// this repo (CI runs in the repo working tree). Assert the value is a
|
|
89
|
+
// 12-char lowercase hex SHA — that proves git was consulted, not that
|
|
90
|
+
// the time-based fallback was hit.
|
|
91
|
+
const cfg = callConfig("build");
|
|
92
|
+
const value = JSON.parse(cfg.define.__DECO_BUILD_HASH__);
|
|
93
|
+
// Either git produced a SHA (CI / dev machine inside a repo) or the
|
|
94
|
+
// time-based fallback ran. Both are acceptable; we just assert non-empty
|
|
95
|
+
// and length sanity.
|
|
96
|
+
expect(typeof value).toBe("string");
|
|
97
|
+
expect(value.length).toBeGreaterThan(0);
|
|
98
|
+
// Time-based fallback produces base36 characters; git short SHAs are
|
|
99
|
+
// 12 hex chars. Both fit in this superset regex.
|
|
100
|
+
expect(value).toMatch(/^[0-9a-z]+$/);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("preserves allowedHosts behaviour (regression: define is additive, not replacing)", () => {
|
|
104
|
+
process.env.DECO_SITE_NAME = "test-site";
|
|
105
|
+
try {
|
|
106
|
+
const cfg = callConfig("serve");
|
|
107
|
+
expect(cfg.server?.allowedHosts).toContain(".deco.studio");
|
|
108
|
+
expect(cfg.define.__DECO_BUILD_HASH__).toBeDefined();
|
|
109
|
+
} finally {
|
|
110
|
+
delete process.env.DECO_SITE_NAME;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
});
|