Unverified Commit a2fd4ea5 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Enable server HMR for metadata routes (manifest.ts, robots.ts, etc.) (#93110)

**This PR removes the special-case handling for metadata routes so it
benefits from server hmr. It also adds tests.**

Previously, metadata routes were excluded from Turbopack's server-side
HMR and fell back to a full require-cache eviction on every change. This
meant unmodified dependency modules were unnecessarily re-evaluated.

Metadata routes use the same app-route.js template and getUserland()
per-request getter as regular route handlers, so they can participate in
in-place server HMR the same way. Remove the isMetadataRoute() exclusion
from the usesServerHmr gate.

Test Plan: Added e2e test for testing selective hmr eviction
Co-authored-by: 's avatarWill Binns-Smith <wbinnssmith@users.noreply.github.com>
Co-authored-by: 's avatarClaude <noreply@anthropic.com>
parent 00a671b7
...@@ -82,10 +82,7 @@ import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-de ...@@ -82,10 +82,7 @@ import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-de
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import type { ModernSourceMapPayload } from '../lib/source-maps' import type { ModernSourceMapPayload } from '../lib/source-maps'
import { isDeferredEntry } from '../../build/entries' import { isDeferredEntry } from '../../build/entries'
import { import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'
isMetadataRoute,
isMetadataRouteFile,
} from '../../lib/metadata/is-metadata-route'
import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect' import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'
import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware' import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'
import { import {
...@@ -602,20 +599,15 @@ export async function createHotReloaderTurbopack( ...@@ -602,20 +599,15 @@ export async function createHotReloaderTurbopack(
join(distDir, p) join(distDir, p)
) )
const { type: entryType, page: entryPage } = splitEntryKey(key) const { type: entryType } = splitEntryKey(key)
// Server HMR applies to App Router entries built with the Turbopack Node.js // Server HMR applies to App Router entries built with the Turbopack Node.js
// runtime: app pages and regular route handlers. Edge routes, Pages Router // runtime: app pages and route handlers (including metadata routes). Edge
// pages, middleware/instrumentation, and metadata routes (manifest.ts, // routes, Pages Router pages, and middleware/instrumentation are excluded.
// robots.ts, sitemap.ts, icon.tsx, etc.) are excluded. Metadata routes are
// excluded because they serve HTTP responses directly and must re-execute
// on every request to pick up file changes; the in-place module update
// model of Server HMR does not apply to them.
const usesServerHmr = const usesServerHmr =
serverFastRefresh && serverFastRefresh &&
entryType === 'app' && entryType === 'app' &&
writtenEndpoint.type !== 'edge' && writtenEndpoint.type !== 'edge'
!isMetadataRoute(entryPage)
const filesToDelete: string[] = [] const filesToDelete: string[] = []
for (const file of serverPaths) { for (const file of serverPaths) {
...@@ -623,9 +615,10 @@ export async function createHotReloaderTurbopack( ...@@ -623,9 +615,10 @@ export async function createHotReloaderTurbopack(
const relativePath = relative(distDir, file) const relativePath = relative(distDir, file)
if ( if (
// For Pages Router, edge routes, middleware, and manifest files: // For Pages Router, edge routes, middleware, and any entry not
// clear the sharedCache in evalManifest(), Node.js require.cache, // participating in server HMR: clear the sharedCache in
// and edge runtime module contexts. // evalManifest(), Node.js require.cache, and edge runtime module
// contexts.
force || force ||
!usesServerHmr || !usesServerHmr ||
!serverHmrSubscriptions?.has(relativePath) !serverHmrSubscriptions?.has(relativePath)
......
export const depEvaluatedAt = Date.now()
export const manifestVersion = 'Version 0'
import type { MetadataRoute } from 'next' import { depEvaluatedAt, manifestVersion } from './manifest-dep'
export default function manifest(): MetadataRoute.Manifest { let _hmrTrigger = 0
const manifestEvaluatedAt = Date.now()
export default function manifest() {
return { return {
name: 'Version 0', name: manifestVersion,
short_name: 'v0', short_name: 'v0',
start_url: '/', start_url: '/',
display: 'standalone', display: 'standalone',
depEvaluatedAt,
manifestEvaluatedAt,
} }
} }
...@@ -194,13 +194,13 @@ describe('server-hmr', () => { ...@@ -194,13 +194,13 @@ describe('server-hmr', () => {
} }
) )
it('reflects manifest.ts changes on fetch/refresh', async () => { it('reflects manifest dep changes on fetch/refresh', async () => {
const initial = await next const initial = await next
.fetch('/manifest.webmanifest') .fetch('/manifest.webmanifest')
.then((res) => res.json()) .then((res) => res.json())
expect(initial.name).toBe('Version 0') expect(initial.name).toBe('Version 0')
await next.patchFile('app/manifest.ts', (content) => await next.patchFile('app/manifest-dep.ts', (content) =>
content.replace('Version 0', 'Version 1') content.replace('Version 0', 'Version 1')
) )
...@@ -211,6 +211,33 @@ describe('server-hmr', () => { ...@@ -211,6 +211,33 @@ describe('server-hmr', () => {
expect(updated.name).toBe('Version 1') expect(updated.name).toBe('Version 1')
}) })
}) })
itTurbopackDev(
'does not re-evaluate an unmodified dep when manifest changes',
async () => {
const initial = await next
.fetch('/manifest.webmanifest')
.then((res) => res.json())
const initialDepEvaluatedAt = initial.depEvaluatedAt
// Patch manifest.ts itself, not the dep module
await next.patchFile('app/manifest.ts', (content) =>
content.replace('_hmrTrigger = 0', '_hmrTrigger = 1')
)
await retry(async () => {
const updated = await next
.fetch('/manifest.webmanifest')
.then((res) => res.json())
// manifest.ts should have been re-evaluated (new timestamp)
expect(updated.manifestEvaluatedAt).not.toBe(
initial.manifestEvaluatedAt
)
// manifest-dep.ts should NOT have been re-evaluated
expect(updated.depEvaluatedAt).toBe(initialDepEvaluatedAt)
})
}
)
}) })
describe('route handler hmr', () => { describe('route handler hmr', () => {
......
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