diff options
15 files changed, 133 insertions, 15 deletions
diff --git a/packages/integrations/cloudflare/CHANGELOG.md b/packages/integrations/cloudflare/CHANGELOG.md index 18bce6fc1..f9443cd25 100644 --- a/packages/integrations/cloudflare/CHANGELOG.md +++ b/packages/integrations/cloudflare/CHANGELOG.md @@ -48,6 +48,16 @@ - [#392](https://github.com/withastro/adapters/pull/392) [`3a49eb7`](https://github.com/withastro/adapters/commit/3a49eb7802c44212ccfab06034b7dc5f2b060e94) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Updates internal code for Astro 5 changes. No changes is required to your project, apart from using Astro 5 +## 11.2.0 + +### Minor Changes + +- [#423](https://github.com/withastro/adapters/pull/423) [`169ac24`](https://github.com/withastro/adapters/commit/169ac24451d8ac0e47dda27f7148d2ddad66e3dc) Thanks [@schummar](https://github.com/schummar)! - Changes the logic which generates the `_routes.json` file to improve generation for projects with many static pages, while still making sure all routes work as expected. + +### Patch Changes + +- [#409](https://github.com/withastro/adapters/pull/409) [`d63bed8`](https://github.com/withastro/adapters/commit/d63bed81afe549f98d705573d365de5204cab134) Thanks [@alexanderniebuhr](https://github.com/alexanderniebuhr)! - Fixes an issue where `cloudflare:` scoped imports made the build fail. We externalize all imports with the `cloudflare:` scope by default now. + ## 11.1.0 ### Minor Changes diff --git a/packages/integrations/cloudflare/package.json b/packages/integrations/cloudflare/package.json index 0cb3e0636..5cd4b6564 100644 --- a/packages/integrations/cloudflare/package.json +++ b/packages/integrations/cloudflare/package.json @@ -30,14 +30,14 @@ "dependencies": { "@astrojs/internal-helpers": "0.4.1", "@astrojs/underscore-redirects": "^0.4.0-alpha.0", - "@cloudflare/workers-types": "^4.20240925.0", + "@cloudflare/workers-types": "^4.20241022.0", "esbuild": "^0.23.1", "estree-walker": "^3.0.3", - "magic-string": "^0.30.11", - "miniflare": "^3.20240925.0", + "magic-string": "^0.30.12", + "miniflare": "^3.20241022.0", "tiny-glob": "^0.2.9", - "wrangler": "^3.78.12", - "@inox-tools/astro-when": "^0.2.3" + "wrangler": "^3.84.0", + "@inox-tools/astro-when": "^0.2.4" }, "peerDependencies": { "astro": "^5.0.0-alpha.8" @@ -49,9 +49,9 @@ "cheerio": "1.0.0", "execa": "^8.0.1", "fast-glob": "^3.3.2", - "rollup": "^4.22.5", + "rollup": "^4.24.3", "strip-ansi": "^7.1.0", - "vite": "^5.4.8" + "vite": "^5.4.10" }, "publishConfig": { "provenance": true diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index c2b2f2de0..0acd86fd8 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -129,6 +129,16 @@ export default function createIntegration(args?: Options): AstroIntegration { // https://developers.cloudflare.com/pages/functions/module-support/ // Allows imports of '.wasm', '.bin', and '.txt' file types cloudflareModulePlugin, + { + name: 'vite:cf-imports', + enforce: 'pre', + resolveId(source) { + if (source.startsWith('cloudflare:')) { + return { id: source, external: true }; + } + return null; + }, + }, ], }, integrations: [astroWhen()], diff --git a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts index 43adeb675..3d7f8a643 100644 --- a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts +++ b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts @@ -225,6 +225,7 @@ export async function createRoutesFile( const convertedPath = segmentsToCfSyntax(route.segments, _config); if (route.pathname === '/404' && route.prerender === true) hasPrerendered404 = true; + // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check switch (route.type) { case 'page': if (route.prerender === false) includePaths.push(convertedPath); @@ -307,11 +308,10 @@ export async function createRoutesFile( const EXTENDED_EXCLUDE_RULES_COUNT = excludeExtends?.length ?? 0; const EXCLUDE_RULES_COUNT = AUTOMATIC_EXCLUDE_RULES_COUNT + EXTENDED_EXCLUDE_RULES_COUNT; - if ( - !hasPrerendered404 || - INCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT || - EXCLUDE_RULES_COUNT > CLOUDFLARE_COMBINED_LIMIT - ) { + const OPTION2_TOTAL_COUNT = + INCLUDE_RULES_COUNT + (includedPathsHaveWildcard ? EXCLUDE_RULES_COUNT : 0); + + if (!hasPrerendered404 || OPTION2_TOTAL_COUNT > CLOUDFLARE_COMBINED_LIMIT) { await writeRoutesFileToOutDir( _config, logger, diff --git a/packages/integrations/cloudflare/test/fixtures/astro-dev-platform/package.json b/packages/integrations/cloudflare/test/fixtures/astro-dev-platform/package.json index 2392c6172..12ae29808 100644 --- a/packages/integrations/cloudflare/test/fixtures/astro-dev-platform/package.json +++ b/packages/integrations/cloudflare/test/fixtures/astro-dev-platform/package.json @@ -7,6 +7,6 @@ "astro": "^5.0.0-alpha.8" }, "devDependencies": { - "wrangler": "^3.78.12" + "wrangler": "^3.84.0" } } diff --git a/packages/integrations/cloudflare/test/fixtures/astro-env/package.json b/packages/integrations/cloudflare/test/fixtures/astro-env/package.json index 2598bee1a..e4e8e0fa9 100644 --- a/packages/integrations/cloudflare/test/fixtures/astro-env/package.json +++ b/packages/integrations/cloudflare/test/fixtures/astro-env/package.json @@ -7,6 +7,6 @@ "astro": "^5.0.0-alpha.8" }, "devDependencies": { - "wrangler": "^3.78.12" + "wrangler": "^3.84.0" } } diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/env.d.ts b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/env.d.ts new file mode 100644 index 000000000..4e6b85c4a --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/env.d.ts @@ -0,0 +1 @@ +/// <reference path="../../.astro/types.d.ts" />
\ No newline at end of file diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/[id].astro b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/[id].astro new file mode 100644 index 000000000..1931fc8a6 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/[id].astro @@ -0,0 +1,16 @@ +--- +import type { GetStaticPaths } from "astro"; + +export const getStaticPaths = (() => { + + return Array.from({length:100}).map((_, i) => ({ + params: { + id: i.toString() + } + })); +}) satisfies GetStaticPaths; + +const { id } = Astro.params; +--- + +id={id} diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/dynamic.astro b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/dynamic.astro new file mode 100644 index 000000000..13502d70c --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStatic/pages/dynamic.astro @@ -0,0 +1,5 @@ +--- +export const prerender = false; +--- + +dynamic diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/env.d.ts b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/env.d.ts new file mode 100644 index 000000000..4e6b85c4a --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/env.d.ts @@ -0,0 +1 @@ +/// <reference path="../../.astro/types.d.ts" />
\ No newline at end of file diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/404.astro b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/404.astro new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/404.astro diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/[id].astro b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/[id].astro new file mode 100644 index 000000000..1931fc8a6 --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/[id].astro @@ -0,0 +1,16 @@ +--- +import type { GetStaticPaths } from "astro"; + +export const getStaticPaths = (() => { + + return Array.from({length:100}).map((_, i) => ({ + params: { + id: i.toString() + } + })); +}) satisfies GetStaticPaths; + +const { id } = Astro.params; +--- + +id={id} diff --git a/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/dynamic.astro b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/dynamic.astro new file mode 100644 index 000000000..13502d70c --- /dev/null +++ b/packages/integrations/cloudflare/test/fixtures/routes-json/src/manyStaticWith404/pages/dynamic.astro @@ -0,0 +1,5 @@ +--- +export const prerender = false; +--- + +dynamic diff --git a/packages/integrations/cloudflare/test/fixtures/with-solid-js/package.json b/packages/integrations/cloudflare/test/fixtures/with-solid-js/package.json index 13df3b222..8f3a34596 100644 --- a/packages/integrations/cloudflare/test/fixtures/with-solid-js/package.json +++ b/packages/integrations/cloudflare/test/fixtures/with-solid-js/package.json @@ -6,6 +6,6 @@ "@astrojs/cloudflare": "workspace:*", "@astrojs/solid-js": "^4.4.2", "astro": "^5.0.0-alpha.8", - "solid-js": "^1.9.1" + "solid-js": "^1.9.3" } } diff --git a/packages/integrations/cloudflare/test/routes-json.test.js b/packages/integrations/cloudflare/test/routes-json.test.js index 95a50c306..ee788721d 100644 --- a/packages/integrations/cloudflare/test/routes-json.test.js +++ b/packages/integrations/cloudflare/test/routes-json.test.js @@ -185,4 +185,58 @@ describe('_routes.json generation', () => { }); }); }); + + describe('with many static files', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/routes-json/', import.meta.url).toString(), + srcDir: './src/manyStatic', + adapter: cloudflare({}), + }); + await fixture.build(); + }); + + it('creates a wildcard `include` and `exclude` for as many static assets and redirects as possible, truncating after 100 rules', async () => { + const _routesJson = await fixture.readFile('/_routes.json'); + const routes = JSON.parse(_routesJson); + + assert.deepEqual(routes, { + version: 1, + include: ['/*'], + exclude: [ + '/_astro/*', + '/redirectme', + '/public.txt', + '/a/*', + ...Array.from({ length: 95 }, (_, i) => `/${i}`), + ], + }); + }); + }); + + describe('with many static files when a static 404 is present', () => { + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/routes-json/', import.meta.url).toString(), + srcDir: './src/manyStaticWith404', + adapter: cloudflare({}), + }); + await fixture.build(); + }); + + it('creates `include` for on-demand and `exclude` that are supposed to match nothin', async () => { + const _routesJson = await fixture.readFile('/_routes.json'); + const routes = JSON.parse(_routesJson); + + assert.deepEqual(routes, { + version: 1, + include: ['/_image', '/dynamic'], + exclude: [], + }); + }); + }); }); |