Commit f7121339 authored by Will Binns-Smith's avatar Will Binns-Smith

refactor(metadata-route): extract JS_DEV_CONDITION const, revert accidental…

refactor(metadata-route): extract JS_DEV_CONDITION const, revert accidental generated-native.d.ts changes

- Extract the repeated `process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER`
  guard into a named Rust constant (JS_DEV_CONDITION) used across all 5
  metadata route wrapper templates, so a single change point updates all of them.
- Revert accidental string→RcStr changes in generated-native.d.ts that were
  unrelated to the server HMR fix.
- Remove spurious blank line in server-hmr.test.ts.
Co-Authored-By: 's avatarClaude <noreply@anthropic.com>
parent 8a52ed9c
......@@ -128,6 +128,11 @@ async fn get_base64_file_content(path: FileSystemPath) -> Result<String> {
})
}
/// JS guard that is `true` only in Turbopack dev builds.
/// Route wrappers use this to re-require the user module on every request so
/// that server HMR changes are reflected immediately.
const JS_DEV_CONDITION: &str = "process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER";
/// Creates a virtual `{name}--route-entry.js` source under `parent`.
fn make_route_entry_source(
parent: FileSystemPath,
......@@ -245,7 +250,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
export async function GET() {{
// In dev, re-require on every request to pick up server HMR updates.
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
const _handler = ({dev_condition})
? require({resource_path}).default
: handler
const data = await _handler()
......@@ -265,6 +270,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
};
make_route_entry_source(path.parent(), stem, code)
......@@ -299,7 +305,7 @@ async fn dynamic_sitemap_route_with_generate_source(
const id = await idPromise
const hasXmlExtension = id ? id.endsWith('.xml') : false
// In dev, re-require on every request to pick up server HMR updates.
const _m = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
const _m = ({dev_condition})
? require({resource_path})
: null
const _handler = _m?.default ?? handler
......@@ -355,6 +361,7 @@ async fn dynamic_sitemap_route_with_generate_source(
content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
};
make_route_entry_source(path.parent(), stem, code)
......@@ -384,7 +391,7 @@ async fn dynamic_sitemap_route_without_generate_source(
export async function GET() {{
// In dev, re-require on every request to pick up server HMR updates.
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
const _handler = ({dev_condition})
? require({resource_path}).default
: handler
const data = await _handler()
......@@ -404,6 +411,7 @@ async fn dynamic_sitemap_route_without_generate_source(
content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
};
make_route_entry_source(path.parent(), stem, code)
......@@ -447,7 +455,7 @@ async fn dynamic_image_route_with_metadata_source(
}})
// In dev, re-require on every request to pick up server HMR updates.
const _m = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
const _m = ({dev_condition})
? require({resource_path})
: null
const _handler = _m?.default ?? handler
......@@ -488,6 +496,7 @@ async fn dynamic_image_route_with_metadata_source(
}}
"#,
resource_path = StringifyJs(&format!("./{stem}.{ext}")),
dev_condition = JS_DEV_CONDITION,
};
make_route_entry_source(path.parent(), stem, code)
......@@ -511,7 +520,7 @@ async fn dynamic_image_route_without_metadata_source(
export async function GET(_, ctx) {{
// In dev, re-require on every request to pick up server HMR updates.
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
const _handler = ({dev_condition})
? require({resource_path}).default
: handler
return _handler({{ params: ctx.params }})
......@@ -520,6 +529,7 @@ async fn dynamic_image_route_without_metadata_source(
export * from {resource_path}
"#,
resource_path = StringifyJs(&format!("./{stem}.{ext}")),
dev_condition = JS_DEV_CONDITION,
};
make_route_entry_source(path.parent(), stem, code)
......
......@@ -153,8 +153,8 @@ export declare function minify(
export declare function minifySync(input: Buffer, opts: Buffer): TransformOutput
export interface NapiEndpointConfig {}
export interface NapiAssetPath {
path: RcStr
contentHash: RcStr
path: string
contentHash: string
}
export interface NapiWrittenEndpoint {
type: string
......@@ -363,7 +363,7 @@ export interface AppPageNapiRoute {
}
export interface NapiRoute {
/** The router path */
pathname: RcStr
pathname: string
/** The relative path from project_path to the route file */
originalName?: RcStr
/** The type of route, eg a Page or App */
......@@ -522,13 +522,13 @@ export declare function rootTaskDispose(rootTask: {
export interface NapiIssue {
severity: string
stage: string
filePath: RcStr
filePath: string
title: any
description?: any
detail?: any
source?: NapiIssueSource
additionalSources: Array<NapiAdditionalIssueSource>
documentationLink: RcStr
documentationLink: string
importTraces: any
/**
* Pre-rendered code frame for the issue's source location, if available.
......@@ -537,7 +537,7 @@ export interface NapiIssue {
codeFrame?: string
}
export interface NapiAdditionalIssueSource {
description: RcStr
description: string
source: NapiIssueSource
/** Pre-rendered code frame for this additional source location, if available. */
codeFrame?: string
......@@ -551,16 +551,16 @@ export interface NapiIssueSourceRange {
end: NapiSourcePos
}
export interface NapiSource {
ident: RcStr
filePath: RcStr
ident: string
filePath: string
}
export interface NapiSourcePos {
line: number
column: number
}
export interface NapiDiagnostic {
category: RcStr
name: RcStr
category: string
name: string
payload: Record<string, string>
}
export declare function expandNextJsTemplate(
......
......@@ -157,23 +157,26 @@ describe('server-hmr', () => {
})
describe('metadata route hmr', () => {
itTurbopackDev('reflects manifest.ts 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) =>
content.replace('Version 0', 'Version 1')
)
await retry(async () => {
const updated = await next
itTurbopackDev(
'reflects manifest.ts changes on fetch/refresh',
async () => {
const initial = await next
.fetch('/manifest.webmanifest')
.then((res) => res.json())
expect(updated.name).toBe('Version 1')
})
})
expect(initial.name).toBe('Version 0')
await next.patchFile('app/manifest.ts', (content) =>
content.replace('Version 0', 'Version 1')
)
await retry(async () => {
const updated = await next
.fetch('/manifest.webmanifest')
.then((res) => res.json())
expect(updated.name).toBe('Version 1')
})
}
)
})
describe('route handler hmr', () => {
......@@ -227,7 +230,6 @@ describe('server-hmr', () => {
}
)
})
})
describe('server-hmr config opt-out', () => {
......
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