From f1d76cd9822e05fbde9f9636edb29d1679d1e627 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 13 May 2023 23:15:02 +0100 Subject: [PATCH] fix: prerendered json handling (#242) * fix: prerendered json handling * apply suggestions * Update src/buildApplication/fixPrerenderedRoutes.ts Co-authored-by: Dario Piotrowicz * Update tests/src/buildApplication/generateFunctionsMap.test.ts Co-authored-by: Dario Piotrowicz --------- Co-authored-by: Dario Piotrowicz --- .changeset/tricky-turtles-drum.md | 5 +++ src/buildApplication/fixPrerenderedRoutes.ts | 6 +++- tests/_helpers/index.ts | 4 ++- .../generateFunctionsMap.test.ts | 31 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 .changeset/tricky-turtles-drum.md diff --git a/.changeset/tricky-turtles-drum.md b/.changeset/tricky-turtles-drum.md new file mode 100644 index 000000000..30c2594c1 --- /dev/null +++ b/.changeset/tricky-turtles-drum.md @@ -0,0 +1,5 @@ +--- +'@cloudflare/next-on-pages': patch +--- + +Fix the prerendered route handling for generated JSON files. diff --git a/src/buildApplication/fixPrerenderedRoutes.ts b/src/buildApplication/fixPrerenderedRoutes.ts index ab6b658d3..caa7e8b0e 100644 --- a/src/buildApplication/fixPrerenderedRoutes.ts +++ b/src/buildApplication/fixPrerenderedRoutes.ts @@ -83,6 +83,7 @@ async function getRoutePath( * // index.prerender-fallback.html -> index.html * // index.rsc.prerender-fallback.rsc -> index.rsc * // favicon.ico.prerender-fallback.body -> favicon.ico + * // data.json.prerender-fallback.json -> data.json * ``` * * @param config.fallback Fallback file configuration. @@ -98,7 +99,10 @@ async function getRouteDest( const destRoute = normalizePath( join( dirName, - fallback.fsPath.replace(/\.prerender-fallback(?:\.rsc)?(?:\.body)?/gi, '') + fallback.fsPath.replace( + /\.prerender-fallback(?:\.(?:rsc|body|json))?/gi, + '' + ) ) ); const destFile = join(outputDir, 'static', destRoute); diff --git a/tests/_helpers/index.ts b/tests/_helpers/index.ts index d742d7af9..6d1342d3a 100644 --- a/tests/_helpers/index.ts +++ b/tests/_helpers/index.ts @@ -270,7 +270,9 @@ export function mockPrerenderConfigFile(path: string, ext?: string): string { fsPath, }, initialHeaders: { - ...(path.endsWith('.rsc') && { 'content-type': 'text/x-component' }), + ...((path.endsWith('.rsc') || path.endsWith('.json')) && { + 'content-type': 'text/x-component', + }), ...(path.endsWith('.ico') && { 'content-type': 'image/x-icon' }), vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch', }, diff --git a/tests/src/buildApplication/generateFunctionsMap.test.ts b/tests/src/buildApplication/generateFunctionsMap.test.ts index 85f1b5ad3..11207381f 100644 --- a/tests/src/buildApplication/generateFunctionsMap.test.ts +++ b/tests/src/buildApplication/generateFunctionsMap.test.ts @@ -198,6 +198,37 @@ describe('generateFunctionsMap', async () => { }); }); + test('succeeds for prerendered json', async () => { + const { functionsMap, prerenderedRoutes } = + await generateFunctionsMapFrom({ + _next: { + data: { + 'data.json.func': invalidFuncDir, + 'data.json.prerender-config.json': mockPrerenderConfigFile( + 'data.json', + 'json' + ), + 'data.json.prerender-fallback.json': 'data.json', + }, + }, + 'page.func': validFuncDir, + 'page.rsc.func': validFuncDir, + }); + + expect(functionsMap.size).toEqual(2); + expect(functionsMap.get('/page')).toMatch(/\/page\.func\.js$/); + expect(functionsMap.get('/page.rsc')).toMatch(/\/page\.rsc\.func\.js$/); + + expect(prerenderedRoutes.size).toEqual(1); + expect(prerenderedRoutes.get('/_next/data/data.json')).toEqual({ + headers: { + 'content-type': 'text/x-component', + vary: 'RSC, Next-Router-State-Tree, Next-Router-Prefetch', + }, + overrides: [], + }); + }); + test('succeeds for nested prerendered routes', async () => { const { functionsMap, prerenderedRoutes } = await generateFunctionsMapFrom({