Commit 81e81710 authored by Will Binns-Smith's avatar Will Binns-Smith

fix: use live userland for routing-config reads in AppRouteRouteModule after HMR

In Turbopack dev mode, `this.userland` (set once at construction time from the
static import) was used for `fetchCache`, `dynamic`, and `hasNonStaticMethods`
checks in `handle()`, even though the underlying module may have been replaced
by server HMR. Only the HTTP handler function was already being refreshed via
`_getUserland`.

Resolve the live userland once per request at the top of `handle()` using
`this._getUserland()` when available (Turbopack dev), falling back to the
eagerly-resolved static import otherwise (webpack/production). Use this
`liveUserland` for all three routing-config reads and for the handler function,
so all per-request behavior reflects the current module after HMR.

Remove the now-dead `resolveWithGetter` helper.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 5f258858
...@@ -313,21 +313,6 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -313,21 +313,6 @@ export class AppRouteRouteModule extends RouteModule<
return autoImplementMethods(this.userland as AppRouteUserlandModule)[method] return autoImplementMethods(this.userland as AppRouteUserlandModule)[method]
} }
/**
* Like resolve(), but re-fetches the userland module on every call via the
* async getter. Only used in Turbopack dev mode, where server HMR disposes
* modules between requests. The async wrapper also unwraps async-module
* Promises produced by ESM-only serverExternalPackages.
*/
private async resolveWithGetter(
method: string,
getUserland: () => Promise<AppRouteUserlandModule>
): Promise<AppRouteHandlerFn> {
if (!isHTTPMethod(method)) return () => new Response(null, { status: 400 })
const userland = await getUserland()
return autoImplementMethods(userland)[method]
}
private async do( private async do(
handler: AppRouteHandlerFn, handler: AppRouteHandlerFn,
actionStore: ActionStore, actionStore: ActionStore,
...@@ -720,11 +705,21 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -720,11 +705,21 @@ export class AppRouteRouteModule extends RouteModule<
req: NextRequest, req: NextRequest,
context: AppRouteRouteHandlerContext context: AppRouteRouteHandlerContext
): Promise<Response> { ): Promise<Response> {
// Get the handler function for the given method. In Turbopack dev mode, // In Turbopack dev mode, re-fetch the live userland on every request so
// use resolveWithGetter() to re-fetch the live userland on every request // that server HMR updates (including routing-config exports like `dynamic`,
// In all other modes, resolve() is synchronous. // `fetchCache`, and HTTP method presence) are picked up without a process
const handler = this._getUserland // restart. In all other modes, use the eagerly-resolved static import that
? await this.resolveWithGetter(req.method, this._getUserland) // was captured before the constructor ran.
const liveUserland: AppRouteUserlandModule = this._getUserland
? await this._getUserland()
: (this.userland as AppRouteUserlandModule)
// Get the handler function for the given method. Preserve the isHTTPMethod
// guard to prevent RCE via arbitrary method names.
const handler: AppRouteHandlerFn = this._getUserland
? isHTTPMethod(req.method)
? autoImplementMethods(liveUserland)[req.method]
: () => new Response(null, { status: 400 })
: this.resolve(req.method) : this.resolve(req.method)
// Get the context for the static generation. // Get the context for the static generation.
...@@ -736,7 +731,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -736,7 +731,7 @@ export class AppRouteRouteModule extends RouteModule<
} }
// Add the fetchCache option to the renderOpts. // Add the fetchCache option to the renderOpts.
staticGenerationContext.renderOpts.fetchCache = this.userland.fetchCache staticGenerationContext.renderOpts.fetchCache = liveUserland.fetchCache
const actionStore: ActionStore = { const actionStore: ActionStore = {
isAppRoute: true, isAppRoute: true,
...@@ -770,7 +765,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -770,7 +765,7 @@ export class AppRouteRouteModule extends RouteModule<
this.workAsyncStorage.run(workStore, async () => { this.workAsyncStorage.run(workStore, async () => {
// Check to see if we should bail out of static generation based on // Check to see if we should bail out of static generation based on
// having non-static methods. // having non-static methods.
if (hasNonStaticMethods(this.userland)) { if (hasNonStaticMethods(liveUserland)) {
if (workStore.isStaticGeneration) { if (workStore.isStaticGeneration) {
const err = new DynamicServerError( const err = new DynamicServerError(
'Route is configured with methods that cannot be statically generated.' 'Route is configured with methods that cannot be statically generated.'
...@@ -786,7 +781,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -786,7 +781,7 @@ export class AppRouteRouteModule extends RouteModule<
let request = req let request = req
// Update the static generation store based on the dynamic property. // Update the static generation store based on the dynamic property.
const { dynamic } = this.userland const { dynamic } = liveUserland
switch (dynamic) { switch (dynamic) {
case 'force-dynamic': { case 'force-dynamic': {
// Routes of generated paths should be dynamic // Routes of generated paths should be dynamic
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment