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
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import type { ModernSourceMapPayload } from '../lib/source-maps'
import { isDeferredEntry } from '../../build/entries'
import {
isMetadataRoute,
isMetadataRouteFile,
} from '../../lib/metadata/is-metadata-route'
import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'
import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'
import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'
import {
......@@ -602,20 +599,15 @@ export async function createHotReloaderTurbopack(
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
// runtime: app pages and regular route handlers. Edge routes, Pages Router
// pages, middleware/instrumentation, and metadata routes (manifest.ts,
// 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.
// runtime: app pages and route handlers (including metadata routes). Edge
// routes, Pages Router pages, and middleware/instrumentation are excluded.
const usesServerHmr =
serverFastRefresh &&
entryType === 'app' &&
writtenEndpoint.type !== 'edge' &&
!isMetadataRoute(entryPage)
writtenEndpoint.type !== 'edge'
const filesToDelete: string[] = []
for (const file of serverPaths) {
......@@ -623,9 +615,10 @@ export async function createHotReloaderTurbopack(
const relativePath = relative(distDir, file)
if (
// For Pages Router, edge routes, middleware, and manifest files:
// clear the sharedCache in evalManifest(), Node.js require.cache,
// and edge runtime module contexts.
// For Pages Router, edge routes, middleware, and any entry not
// participating in server HMR: clear the sharedCache in
// evalManifest(), Node.js require.cache, and edge runtime module
// contexts.
force ||
!usesServerHmr ||
!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 {
name: 'Version 0',
name: manifestVersion,
short_name: 'v0',
start_url: '/',
display: 'standalone',
depEvaluatedAt,
manifestEvaluatedAt,
}
}
......@@ -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
.fetch('/manifest.webmanifest')
.then((res) => res.json())
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')
)
......@@ -211,6 +211,33 @@ describe('server-hmr', () => {
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', () => {
......
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