Skip to content

Commit

Permalink
fix: prerendered json handling (#242)
Browse files Browse the repository at this point in the history
* fix: prerendered json handling

* apply suggestions

* Update src/buildApplication/fixPrerenderedRoutes.ts

Co-authored-by: Dario Piotrowicz <[email protected]>

* Update tests/src/buildApplication/generateFunctionsMap.test.ts

Co-authored-by: Dario Piotrowicz <[email protected]>

---------

Co-authored-by: Dario Piotrowicz <[email protected]>
  • Loading branch information
james-elicx and dario-piotrowicz authored May 13, 2023
1 parent e2d2046 commit f1d76cd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-turtles-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@cloudflare/next-on-pages': patch
---

Fix the prerendered route handling for generated JSON files.
6 changes: 5 additions & 1 deletion src/buildApplication/fixPrerenderedRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion tests/_helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
31 changes: 31 additions & 0 deletions tests/src/buildApplication/generateFunctionsMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit f1d76cd

Please sign in to comment.