diff options
author | 2024-10-21 10:08:17 +0200 | |
---|---|---|
committer | 2024-10-21 10:08:17 +0200 | |
commit | 6ebcb9b2f5a2027e8b34d990e67acab7bd1e08e6 (patch) | |
tree | b9869bb9b8dd52be1153722c1d03e9abceb14699 | |
parent | 50a79c6503f408d4286cc51e5476418a0db65d15 (diff) | |
download | astro-6ebcb9b2f5a2027e8b34d990e67acab7bd1e08e6.tar.gz astro-6ebcb9b2f5a2027e8b34d990e67acab7bd1e08e6.tar.zst astro-6ebcb9b2f5a2027e8b34d990e67acab7bd1e08e6.zip |
feat: improve _routes.json generation (#423)
9 files changed, 102 insertions, 5 deletions
diff --git a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts index 353d275c6..03d8aad5e 100644 --- a/packages/integrations/cloudflare/src/utils/generate-routes-json.ts +++ b/packages/integrations/cloudflare/src/utils/generate-routes-json.ts @@ -307,11 +307,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/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/routes-json.test.js b/packages/integrations/cloudflare/test/routes-json.test.js index 028549bad..b7494b1c5 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: [], + }); + }); + }); }); |