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> { ...@@ -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`. /// Creates a virtual `{name}--route-entry.js` source under `parent`.
fn make_route_entry_source( fn make_route_entry_source(
parent: FileSystemPath, parent: FileSystemPath,
...@@ -245,7 +250,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So ...@@ -245,7 +250,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
export async function GET() {{ export async function GET() {{
// In dev, re-require on every request to pick up server HMR updates. // 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 ? require({resource_path}).default
: handler : handler
const data = await _handler() const data = await _handler()
...@@ -265,6 +270,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So ...@@ -265,6 +270,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
content_type = StringifyJs(&content_type), content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem), file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE), cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
}; };
make_route_entry_source(path.parent(), stem, code) make_route_entry_source(path.parent(), stem, code)
...@@ -299,7 +305,7 @@ async fn dynamic_sitemap_route_with_generate_source( ...@@ -299,7 +305,7 @@ async fn dynamic_sitemap_route_with_generate_source(
const id = await idPromise const id = await idPromise
const hasXmlExtension = id ? id.endsWith('.xml') : false const hasXmlExtension = id ? id.endsWith('.xml') : false
// In dev, re-require on every request to pick up server HMR updates. // 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}) ? require({resource_path})
: null : null
const _handler = _m?.default ?? handler const _handler = _m?.default ?? handler
...@@ -355,6 +361,7 @@ async fn dynamic_sitemap_route_with_generate_source( ...@@ -355,6 +361,7 @@ async fn dynamic_sitemap_route_with_generate_source(
content_type = StringifyJs(&content_type), content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem), file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE), cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
}; };
make_route_entry_source(path.parent(), stem, code) make_route_entry_source(path.parent(), stem, code)
...@@ -384,7 +391,7 @@ async fn dynamic_sitemap_route_without_generate_source( ...@@ -384,7 +391,7 @@ async fn dynamic_sitemap_route_without_generate_source(
export async function GET() {{ export async function GET() {{
// In dev, re-require on every request to pick up server HMR updates. // 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 ? require({resource_path}).default
: handler : handler
const data = await _handler() const data = await _handler()
...@@ -404,6 +411,7 @@ async fn dynamic_sitemap_route_without_generate_source( ...@@ -404,6 +411,7 @@ async fn dynamic_sitemap_route_without_generate_source(
content_type = StringifyJs(&content_type), content_type = StringifyJs(&content_type),
file_type = StringifyJs(&stem), file_type = StringifyJs(&stem),
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE), cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
dev_condition = JS_DEV_CONDITION,
}; };
make_route_entry_source(path.parent(), stem, code) make_route_entry_source(path.parent(), stem, code)
...@@ -447,7 +455,7 @@ async fn dynamic_image_route_with_metadata_source( ...@@ -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. // 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}) ? require({resource_path})
: null : null
const _handler = _m?.default ?? handler const _handler = _m?.default ?? handler
...@@ -488,6 +496,7 @@ async fn dynamic_image_route_with_metadata_source( ...@@ -488,6 +496,7 @@ async fn dynamic_image_route_with_metadata_source(
}} }}
"#, "#,
resource_path = StringifyJs(&format!("./{stem}.{ext}")), resource_path = StringifyJs(&format!("./{stem}.{ext}")),
dev_condition = JS_DEV_CONDITION,
}; };
make_route_entry_source(path.parent(), stem, code) make_route_entry_source(path.parent(), stem, code)
...@@ -511,7 +520,7 @@ async fn dynamic_image_route_without_metadata_source( ...@@ -511,7 +520,7 @@ async fn dynamic_image_route_without_metadata_source(
export async function GET(_, ctx) {{ export async function GET(_, ctx) {{
// In dev, re-require on every request to pick up server HMR updates. // 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 ? require({resource_path}).default
: handler : handler
return _handler({{ params: ctx.params }}) return _handler({{ params: ctx.params }})
...@@ -520,6 +529,7 @@ async fn dynamic_image_route_without_metadata_source( ...@@ -520,6 +529,7 @@ async fn dynamic_image_route_without_metadata_source(
export * from {resource_path} export * from {resource_path}
"#, "#,
resource_path = StringifyJs(&format!("./{stem}.{ext}")), resource_path = StringifyJs(&format!("./{stem}.{ext}")),
dev_condition = JS_DEV_CONDITION,
}; };
make_route_entry_source(path.parent(), stem, code) make_route_entry_source(path.parent(), stem, code)
......
...@@ -153,8 +153,8 @@ export declare function minify( ...@@ -153,8 +153,8 @@ export declare function minify(
export declare function minifySync(input: Buffer, opts: Buffer): TransformOutput export declare function minifySync(input: Buffer, opts: Buffer): TransformOutput
export interface NapiEndpointConfig {} export interface NapiEndpointConfig {}
export interface NapiAssetPath { export interface NapiAssetPath {
path: RcStr path: string
contentHash: RcStr contentHash: string
} }
export interface NapiWrittenEndpoint { export interface NapiWrittenEndpoint {
type: string type: string
...@@ -363,7 +363,7 @@ export interface AppPageNapiRoute { ...@@ -363,7 +363,7 @@ export interface AppPageNapiRoute {
} }
export interface NapiRoute { export interface NapiRoute {
/** The router path */ /** The router path */
pathname: RcStr pathname: string
/** The relative path from project_path to the route file */ /** The relative path from project_path to the route file */
originalName?: RcStr originalName?: RcStr
/** The type of route, eg a Page or App */ /** The type of route, eg a Page or App */
...@@ -522,13 +522,13 @@ export declare function rootTaskDispose(rootTask: { ...@@ -522,13 +522,13 @@ export declare function rootTaskDispose(rootTask: {
export interface NapiIssue { export interface NapiIssue {
severity: string severity: string
stage: string stage: string
filePath: RcStr filePath: string
title: any title: any
description?: any description?: any
detail?: any detail?: any
source?: NapiIssueSource source?: NapiIssueSource
additionalSources: Array<NapiAdditionalIssueSource> additionalSources: Array<NapiAdditionalIssueSource>
documentationLink: RcStr documentationLink: string
importTraces: any importTraces: any
/** /**
* Pre-rendered code frame for the issue's source location, if available. * Pre-rendered code frame for the issue's source location, if available.
...@@ -537,7 +537,7 @@ export interface NapiIssue { ...@@ -537,7 +537,7 @@ export interface NapiIssue {
codeFrame?: string codeFrame?: string
} }
export interface NapiAdditionalIssueSource { export interface NapiAdditionalIssueSource {
description: RcStr description: string
source: NapiIssueSource source: NapiIssueSource
/** Pre-rendered code frame for this additional source location, if available. */ /** Pre-rendered code frame for this additional source location, if available. */
codeFrame?: string codeFrame?: string
...@@ -551,16 +551,16 @@ export interface NapiIssueSourceRange { ...@@ -551,16 +551,16 @@ export interface NapiIssueSourceRange {
end: NapiSourcePos end: NapiSourcePos
} }
export interface NapiSource { export interface NapiSource {
ident: RcStr ident: string
filePath: RcStr filePath: string
} }
export interface NapiSourcePos { export interface NapiSourcePos {
line: number line: number
column: number column: number
} }
export interface NapiDiagnostic { export interface NapiDiagnostic {
category: RcStr category: string
name: RcStr name: string
payload: Record<string, string> payload: Record<string, string>
} }
export declare function expandNextJsTemplate( export declare function expandNextJsTemplate(
......
...@@ -157,23 +157,26 @@ describe('server-hmr', () => { ...@@ -157,23 +157,26 @@ describe('server-hmr', () => {
}) })
describe('metadata route hmr', () => { describe('metadata route hmr', () => {
itTurbopackDev('reflects manifest.ts changes on fetch/refresh', async () => { itTurbopackDev(
const initial = await next 'reflects manifest.ts changes on fetch/refresh',
.fetch('/manifest.webmanifest') async () => {
.then((res) => res.json()) const initial = await next
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') .fetch('/manifest.webmanifest')
.then((res) => res.json()) .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', () => { describe('route handler hmr', () => {
...@@ -227,7 +230,6 @@ describe('server-hmr', () => { ...@@ -227,7 +230,6 @@ describe('server-hmr', () => {
} }
) )
}) })
}) })
describe('server-hmr config opt-out', () => { 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