-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For route handlers, call
loadComponents
also during next build
During `next dev` for route handlers, `loadComponents` is called to load the route module. This also has the (for now still expected) side-effect that the reference manifests singleton is set, which we need to make `'use cache'` work in router handlers. Until now, `loadComponents` was not called during `next build` for router handlers, and the module was loaded separately with `RouteModuleLoader.load`. With this PR, we are aligning both modes, so that it's always ensured that the reference manifests singleton is set. The end goal is to avoid the global singleton completely, but it's still unclear how to achive this because of the requirements and constrains that are documented in #62437.
- Loading branch information
1 parent
1b87129
commit 54b758f
Showing
5 changed files
with
55 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
test/e2e/app-dir/use-cache-route-handler-only/app/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
async function getCachedRandom() { | ||
'use cache' | ||
return Math.random() | ||
} | ||
|
||
export async function GET() { | ||
const rand1 = await getCachedRandom() | ||
// TODO: Remove this extra micro task when bug in use cache wrapper is fixed. | ||
await Promise.resolve() | ||
const rand2 = await getCachedRandom() | ||
|
||
const response = JSON.stringify({ rand1, rand2 }) | ||
|
||
return new Response(response, { | ||
headers: { 'content-type': 'application/json' }, | ||
}) | ||
} |
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/use-cache-route-handler-only/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
experimental: { | ||
dynamicIO: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
19 changes: 19 additions & 0 deletions
19
test/e2e/app-dir/use-cache-route-handler-only/use-cache-route-handler-only.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* eslint-disable jest/no-standalone-expect */ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
// Explicitly don't mix route handlers with pages in this test app, to make sure | ||
// that this also works in isolation. | ||
describe('use-cache-route-handler-only', () => { | ||
const { next, isTurbopack } = nextTestSetup({ | ||
files: __dirname, | ||
}) | ||
|
||
const itSkipTurbopack = isTurbopack ? it.skip : it | ||
|
||
itSkipTurbopack('should cache results in route handlers', async () => { | ||
const response = await next.fetch('/') | ||
const { rand1, rand2 } = await response.json() | ||
|
||
expect(rand1).toEqual(rand2) | ||
}) | ||
}) |