Skip to content

Commit 633855b

Browse files
Use workerd's compatibility date for Cloudflare defaults (#17819)
* fix(cli): use hardcoded compatibility_date in `astro add cloudflare` scaffolding Replace `new Date().toISOString().slice(0, 10)` with a hardcoded `CLOUDFLARE_COMPATIBILITY_DATE` constant. workerd rejects dates newer than ~7 days past its build date, so using today's date can produce a wrangler.jsonc that fails immediately. Fixes #17796 * fix(cloudflare): use workerd compatibility date * fix: preserve Vitest Vite resolution * docs(cloudflare): document Wrangler requirement --------- Co-authored-by: factory[bot] <factory[bot]@users.noreply.github.com>
1 parent 55d38c8 commit 633855b

11 files changed

Lines changed: 542 additions & 44 deletions

File tree

‎.changeset/funny-carrots-add.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'astro': patch
3+
'@astrojs/cloudflare': patch
4+
---
5+
6+
Updates generated and default Cloudflare `compatibility_date` values to match the installed runtime and requires Wrangler `^4.125.0`

‎knip.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ export default {
8282
'rehype-toc',
8383
'remark-code-titles',
8484
'@types/http-cache-semantics',
85+
// Resolved from the user's project by `astro add cloudflare`.
86+
'@astrojs/cloudflare',
8587
// Optional peer dep: dynamically imported in config validation for the legacy
8688
// remark/rehype pipeline. Knip flags it because it's referenced from source.
8789
'@astrojs/markdown-remark',
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createRequire } from 'node:module';
2+
import { pathToFileURL } from 'node:url';
3+
4+
interface CloudflareInfo {
5+
getLocalWorkerdCompatibilityDate(): { date: string };
6+
}
7+
8+
export async function getCloudflareCompatibilityDate(root: URL): Promise<string> {
9+
const require = createRequire(root);
10+
const infoPath = require.resolve('@astrojs/cloudflare/info');
11+
const infoUrl = pathToFileURL(infoPath).toString();
12+
const infoModule = (await import(infoUrl)) as CloudflareInfo;
13+
return infoModule.getLocalWorkerdCompatibilityDate().date;
14+
}

‎packages/astro/src/cli/add/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { eventCliSession, telemetry } from '../../events/index.js';
3131
import { exec } from '../exec.js';
3232
import { createLoggerFromFlags, type Flags, flagsToAstroInlineConfig } from '../flags.js';
3333
import { fetchPackageJson, fetchPackageVersions } from '../install-package.js';
34+
import { getCloudflareCompatibilityDate } from './cloudflare.js';
3435

3536
const { bold, cyan, dim, green, magenta, red, yellow } = colors;
3637

@@ -200,7 +201,7 @@ export async function add(names: string[], { flags }: AddOptions) {
200201

201202
if (await askToContinue({ flags, logger })) {
202203
const data = await getPackageJson();
203-
let compatibilityDate = new Date().toISOString().slice(0, 10);
204+
const compatibilityDate = await getCloudflareCompatibilityDate(root);
204205

205206
await fs.writeFile(
206207
wranglerConfigURL,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import assert from 'node:assert/strict';
2+
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
3+
import { tmpdir } from 'node:os';
4+
import { join } from 'node:path';
5+
import { describe, it } from 'node:test';
6+
import { pathToFileURL } from 'node:url';
7+
import { getCloudflareCompatibilityDate } from '../../../dist/cli/add/cloudflare.js';
8+
9+
describe('astro add cloudflare', () => {
10+
it('uses the compatibility date exported by the installed adapter', async () => {
11+
const projectDir = await mkdtemp(join(tmpdir(), 'astro-add-cloudflare-'));
12+
const adapterDir = join(projectDir, 'node_modules', '@astrojs', 'cloudflare');
13+
14+
try {
15+
await mkdir(adapterDir, { recursive: true });
16+
await writeFile(
17+
join(adapterDir, 'package.json'),
18+
JSON.stringify({
19+
name: '@astrojs/cloudflare',
20+
type: 'module',
21+
exports: { './info': './info.js' },
22+
}),
23+
);
24+
await writeFile(
25+
join(adapterDir, 'info.js'),
26+
`export function getLocalWorkerdCompatibilityDate() {
27+
return { date: '2026-08-15', source: 'workerd' };
28+
}
29+
`,
30+
);
31+
32+
const date = await getCloudflareCompatibilityDate(pathToFileURL(`${projectDir}/`));
33+
34+
assert.equal(date, '2026-08-15');
35+
} finally {
36+
await rm(projectDir, { recursive: true, force: true });
37+
}
38+
});
39+
});

‎packages/integrations/cloudflare/package.json‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
1919
"exports": {
2020
".": "./dist/index.js",
21+
"./info": "./dist/info.js",
2122
"./entrypoints/server": "./dist/entrypoints/server.js",
2223
"./entrypoints/preview": "./dist/entrypoints/preview.js",
2324
"./entrypoints/server.js": "./dist/entrypoints/server.js",
@@ -46,16 +47,16 @@
4647
"dependencies": {
4748
"@astrojs/internal-helpers": "workspace:*",
4849
"@astrojs/underscore-redirects": "workspace:*",
49-
"@cloudflare/vite-plugin": "^1.39.0",
50+
"@cloudflare/vite-plugin": "^1.53.0",
5051
"piccolore": "^0.1.3",
5152
"vite": "^8.0.13"
5253
},
5354
"peerDependencies": {
5455
"astro": "^7.2.0",
55-
"wrangler": "^4.83.0"
56+
"wrangler": "^4.125.0"
5657
},
5758
"devDependencies": {
58-
"@cloudflare/workers-types": "^4.20260526.1",
59+
"@cloudflare/workers-types": "^5.20260820.1",
5960
"@types/node": "^22.10.6",
6061
"@types/prismjs": "1.26.6",
6162
"astro": "workspace:*",
@@ -64,7 +65,7 @@
6465
"devalue": "^5.8.1",
6566
"prismjs": "^1.30.0",
6667
"tinyglobby": "^0.2.15",
67-
"wrangler": "^4.83.0"
68+
"wrangler": "^4.125.0"
6869
},
6970
"publishConfig": {
7071
"provenance": true

‎packages/integrations/cloudflare/src/hono.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function getFetchState(context: HonoCloudflareContextLike): FetchState {
7070
export function cf(): HonoMiddlewareHandler {
7171
return async (context, next) => {
7272
const state = getFetchState(context);
73-
const asset = await cfFetch(state, context.env, context.executionCtx);
73+
const asset = await cfFetch(state, context.env, context.executionCtx as ExecutionContext);
7474
if (asset) return asset;
7575
await next();
7676
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { getLocalWorkerdCompatibilityDate } from '@cloudflare/vite-plugin';

‎packages/integrations/cloudflare/src/wrangler.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import type { PluginConfig, WorkerConfig } from '@cloudflare/vite-plugin';
2+
import { getLocalWorkerdCompatibilityDate } from './info.js';
23

34
export const DEFAULT_SESSION_KV_BINDING_NAME = 'SESSION';
45
export const DEFAULT_IMAGES_BINDING_NAME = 'IMAGES';
56
export const DEFAULT_ASSETS_BINDING_NAME = 'ASSETS';
67

7-
// Default compatibility date used when the user doesn't set one in their wrangler config.
8-
// The @cloudflare/vite-plugin falls back to today's date, but that can exceed the maximum
9-
// date supported by the bundled workerd binary (which has a ~7 day buffer from its build date),
10-
// causing ERR_RUNTIME_FAILURE. A hard-coded date avoids this issue.
11-
// This should be updated when upgrading wrangler/workerd dependencies.
12-
const DEFAULT_COMPATIBILITY_DATE = '2026-04-15';
8+
const DEFAULT_COMPATIBILITY_DATE = getLocalWorkerdCompatibilityDate().date;
139

1410
interface CloudflareConfigOptions {
1511
sessionKVBindingName?: string | undefined;

‎packages/integrations/cloudflare/test/wrangler.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as assert from 'node:assert/strict';
22
import { describe, it } from 'node:test';
3+
import { getLocalWorkerdCompatibilityDate } from '../dist/info.js';
34
import {
45
cloudflareConfigCustomizer,
56
DEFAULT_ASSETS_BINDING_NAME,
@@ -8,6 +9,22 @@ import {
89
} from '../dist/wrangler.js';
910

1011
describe('cloudflareConfigCustomizer', () => {
12+
describe('compatibility date', () => {
13+
it('uses the date supported by the installed workerd', () => {
14+
const customizer = cloudflareConfigCustomizer();
15+
const result = customizer({});
16+
17+
assert.equal(result.compatibility_date, getLocalWorkerdCompatibilityDate().date);
18+
});
19+
20+
it('preserves the user compatibility date', () => {
21+
const customizer = cloudflareConfigCustomizer();
22+
const result = customizer({ compatibility_date: '2025-01-01' });
23+
24+
assert.equal(result.compatibility_date, '2025-01-01');
25+
});
26+
});
27+
1128
describe('main entrypoint', () => {
1229
it('sets main to the server entrypoint when none exists', () => {
1330
const customizer = cloudflareConfigCustomizer();

0 commit comments

Comments
 (0)