Skip to content

Commit a0234a3

Browse files
in astro add cloudflare use cloudflare/vite-plugin's function to get latest compatibility date for wrangler.jsonc scaffolding (#15386)
Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
1 parent 876b664 commit a0234a3

5 files changed

Lines changed: 30 additions & 3 deletions

File tree

.changeset/long-trams-see.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@astrojs/cloudflare': patch
3+
'astro': patch
4+
---
5+
6+
Updates `astro add cloudflare` to use the latest valid `compatibility_date` in the wrangler config, if available

knip.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export default {
4848
'rehype-toc',
4949
'remark-code-titles',
5050
'@types/http-cache-semantics',
51+
// Dynamically imported by astro add cloudflare
52+
'@astrojs/cloudflare',
5153
],
5254
},
5355
'packages/db': {

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fsMod, { existsSync, promises as fs } from 'node:fs';
2+
import { createRequire } from 'node:module';
23
import path from 'node:path';
34
import { fileURLToPath, pathToFileURL } from 'node:url';
45
import boxen from 'boxen';
@@ -81,9 +82,9 @@ export default async function seed() {
8182
// TODO
8283
}
8384
`,
84-
CLOUDFLARE_WRANGLER_CONFIG: (name: string) => `\
85+
CLOUDFLARE_WRANGLER_CONFIG: (name: string, compatibilityDate: string) => `\
8586
{
86-
"compatibility_date": ${JSON.stringify(new Date().toISOString().slice(0, 10))},
87+
"compatibility_date": ${JSON.stringify(compatibilityDate)},
8788
"compatibility_flags": ["global_fetch_strictly_public"],
8889
"name": ${JSON.stringify(name)},
8990
"main": "@astrojs/cloudflare/entrypoints/server",
@@ -217,10 +218,22 @@ export async function add(names: string[], { flags }: AddOptions) {
217218

218219
if (await askToContinue({ flags, logger })) {
219220
const data = await getPackageJson();
221+
let compatibilityDate: string;
222+
try {
223+
const require = createRequire(root);
224+
const { getLocalWorkerdCompatibilityDate } = await import(
225+
require.resolve('@astrojs/cloudflare/info')
226+
);
227+
({ date: compatibilityDate } = getLocalWorkerdCompatibilityDate({
228+
projectPath: rootPath,
229+
}));
230+
} catch {
231+
compatibilityDate = new Date().toISOString().slice(0, 10);
232+
}
220233

221234
await fs.writeFile(
222235
wranglerConfigURL,
223-
STUBS.CLOUDFLARE_WRANGLER_CONFIG(data?.name ?? 'example'),
236+
STUBS.CLOUDFLARE_WRANGLER_CONFIG(data?.name ?? 'example', compatibilityDate),
224237
'utf-8',
225238
);
226239
}

packages/integrations/cloudflare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
2020
"exports": {
2121
".": "./dist/index.js",
22+
"./info": "./dist/info.js",
2223
"./entrypoints/server": "./dist/entrypoints/server.js",
2324
"./entrypoints/preview": "./dist/entrypoints/preview.js",
2425
"./entrypoints/server.js": "./dist/entrypoints/server.js",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Re-exports utilities for use by astro add CLI.
3+
* This provides a resolvable path from the user's project.
4+
*/
5+
export { getLocalWorkerdCompatibilityDate } from '@cloudflare/vite-plugin';

0 commit comments

Comments
 (0)