diff --git a/packages/void/src/asset-worker-bypass.ts b/packages/void/src/asset-worker-bypass.ts index 042a55d89..efe88bef9 100644 --- a/packages/void/src/asset-worker-bypass.ts +++ b/packages/void/src/asset-worker-bypass.ts @@ -253,7 +253,10 @@ function resolveStaticPageBypass( } else if (files.length > 0) { headers = renderGeneratedAssetHeaders(files); } - const runWorkerFirst = [...VOID_INTERNAL_WORKER_FIRST, ...[...rules].sort()]; + const runWorkerFirst = withoutRedundantRules([ + ...VOID_INTERNAL_WORKER_FIRST, + ...[...rules].sort(), + ]); return fitsRunWorkerFirstLimits(runWorkerFirst) ? { runWorkerFirst, headers } : null; } @@ -285,6 +288,18 @@ function workerPathRules(pattern: string): Array | null { : [rule]; } +/** + * Leaves out each rule that a `/prefix/*` rule in the list already matches. + * Cloudflare rejects a list with such a rule ("rule '/blog/*' makes it + * redundant"), and the wider rule already sends those paths to the Worker. + */ +function withoutRedundantRules(rules: Array): Array { + const prefixes = rules.filter((rule) => rule.endsWith('/*')).map((rule) => rule.slice(0, -1)); + return rules.filter( + (rule) => !prefixes.some((prefix) => rule !== prefix + '*' && rule.startsWith(prefix)), + ); +} + function fitsRunWorkerFirstLimits(rules: Array): boolean { return ( rules.length <= MAX_RUN_WORKER_FIRST_RULES && diff --git a/packages/void/test/integration/native-asset-cache-policy.test.ts b/packages/void/test/integration/native-asset-cache-policy.test.ts index d62836ad5..0f79c3d53 100644 --- a/packages/void/test/integration/native-asset-cache-policy.test.ts +++ b/packages/void/test/integration/native-asset-cache-policy.test.ts @@ -643,11 +643,22 @@ describe('native output: "static" pages in real Workers', () => { // The `/proxy/*` route also answers `/proxy`, so this file must not. writeFileSync(join(assets, 'proxy.html'), '

proxy file

'); writeFileSync(join(assets, 'assets/app-abcdefgh.js'), 'export const value = 1;'); + // `/items/tags/:tag` and `/api/hello` sit under the rules of `/items/:id` and + // `/api/:name`. Miniflare rejects a list that names them too, as Cloudflare does. + const serverPatterns = [ + '/products', + '/contact', + '/items/:id', + '/items/tags/:tag', + '/api/hello', + '/api/:name', + '/proxy/*', + ]; const plan = resolveAssetWorkerBypass( bypassFacts({ - patterns: ['/', '/about', '/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'], + patterns: ['/', '/about', ...serverPatterns], immutableFiles: ['assets/app-abcdefgh.js'], - serverPatterns: ['/products', '/contact', '/items/:id', '/api/hello', '/proxy/*'], + serverPatterns, }), )!; writeFileSync(join(assets, '_headers'), plan.headers!); @@ -713,7 +724,9 @@ describe('native output: "static" pages in real Workers', () => { expect(await action.text()).toBe('action ran'); for (const path of [ '/items/42', + '/items/tags/new', '/api/hello', + '/api/someone', '/proxy', '/proxy/a/b', '/missing', diff --git a/packages/void/test/unit/asset-worker-bypass.test.ts b/packages/void/test/unit/asset-worker-bypass.test.ts index 4ef178c6f..c9e565c15 100644 --- a/packages/void/test/unit/asset-worker-bypass.test.ts +++ b/packages/void/test/unit/asset-worker-bypass.test.ts @@ -170,6 +170,36 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => { ).toEqual(['/__void', '/__void/*', '/docs/*', '/proxy', '/proxy/*']); }); + it('leaves out rules that a wider prefix rule already matches', () => { + expect( + resolveAssetWorkerBypass( + facts({ + serverPatterns: [ + '/api/hello', + '/api/:name', + '/api-docs', + '/blog/:slug', + '/blog/tags/:tag', + '/blog/tags/featured', + '/docs/archive', + '/docs/:path{.+}', + '/proxy/*', + '/proxy/health', + ], + }), + )?.runWorkerFirst, + ).toEqual([ + '/__void', + '/__void/*', + '/api-docs', + '/api/*', + '/blog/*', + '/docs/*', + '/proxy', + '/proxy/*', + ]); + }); + it.each<[string, Partial, number | null]>([ ['no hashed files', { immutableFiles: [] }, null], ['an empty assetsDir', { assetsDir: '' }, null], @@ -195,6 +225,17 @@ describe('resolveAssetWorkerBypass for output: "static" Pages builds', () => { }); }); + it('leaves out a server path that the assets folder rule already matches', () => { + expect( + resolveAssetWorkerBypass( + facts({ serverPatterns: ['/products', '/assets/guide'], immutableFiles: files(101) }), + ), + ).toEqual({ + runWorkerFirst: ['/__void', '/__void/*', '/assets/*', '/products'], + headers: null, + }); + }); + it('keeps every path on the Worker when no rule can cover the hashed files', () => { expect( resolveAssetWorkerBypass(