Skip to content

Commit cd2d728

Browse files
wbinnssmithclaude
andcommitted
refactor(metadata-route): extract make_route_entry_source helper, simplify HMR require pattern
Extract the repeated 5-line VirtualSource creation block into a shared make_route_entry_source helper. Shorten the per-handler comment to a single line and use optional chaining + nullish coalescing (?? / ?.) instead of ternary for the multi-export (sitemap/image-with-metadata) variants, reducing noise without changing behaviour. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 91fab9c commit cd2d728

File tree

1 file changed

+31
-61
lines changed
  • crates/next-core/src/next_app/metadata

1 file changed

+31
-61
lines changed

crates/next-core/src/next_app/metadata/route.rs

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ async fn get_base64_file_content(path: FileSystemPath) -> Result<String> {
128128
})
129129
}
130130

131+
/// Creates a virtual `{name}--route-entry.js` source under `parent`.
132+
fn make_route_entry_source(
133+
parent: FileSystemPath,
134+
name: &str,
135+
code: String,
136+
) -> Result<Vc<Box<dyn Source>>> {
137+
let file = File::from(code);
138+
let source = VirtualSource::new(
139+
parent.join(&format!("{name}--route-entry.js"))?,
140+
AssetContent::file(FileContent::Content(file).cell()),
141+
);
142+
Ok(Vc::upcast(source))
143+
}
144+
131145
#[turbo_tasks::function]
132146
async fn static_route_source(mode: NextMode, path: FileSystemPath) -> Result<Vc<Box<dyn Source>>> {
133147
let stem = path.file_stem();
@@ -201,14 +215,7 @@ async fn static_route_source(mode: NextMode, path: FileSystemPath) -> Result<Vc<
201215
// Use full filename (stem + extension) to avoid conflicts when multiple icon
202216
// formats exist (e.g., icon.png and icon.svg)
203217
let filename = path.file_name();
204-
205-
let file = File::from(code);
206-
let source = VirtualSource::new(
207-
path.parent().join(&format!("{filename}--route-entry.js"))?,
208-
AssetContent::file(FileContent::Content(file).cell()),
209-
);
210-
211-
Ok(Vc::upcast(source))
218+
make_route_entry_source(path.parent(), filename, code)
212219
}
213220

214221
#[turbo_tasks::function]
@@ -237,10 +244,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
237244
}}
238245
239246
export async function GET() {{
240-
// In Turbopack dev mode, re-require the user's module on every
241-
// request so that server HMR updates are reflected immediately.
242-
// The static `handler` import above is kept for the type check
243-
// and re-exports; at runtime in dev we bypass it here.
247+
// In dev, re-require on every request to pick up server HMR updates.
244248
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
245249
? require({resource_path}).default
246250
: handler
@@ -263,13 +267,7 @@ async fn dynamic_text_route_source(path: FileSystemPath) -> Result<Vc<Box<dyn So
263267
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
264268
};
265269

266-
let file = File::from(code);
267-
let source = VirtualSource::new(
268-
path.parent().join(&format!("{stem}--route-entry.js"))?,
269-
AssetContent::file(FileContent::Content(file).cell()),
270-
);
271-
272-
Ok(Vc::upcast(source))
270+
make_route_entry_source(path.parent(), stem, code)
273271
}
274272

275273
async fn dynamic_sitemap_route_with_generate_source(
@@ -300,13 +298,12 @@ async fn dynamic_sitemap_route_with_generate_source(
300298
301299
const id = await idPromise
302300
const hasXmlExtension = id ? id.endsWith('.xml') : false
303-
// In Turbopack dev mode, re-require the user's module on every
304-
// request so that server HMR updates are reflected immediately.
305-
const _mod = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
301+
// In dev, re-require on every request to pick up server HMR updates.
302+
const _m = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
306303
? require({resource_path})
307304
: null
308-
const _handler = _mod ? _mod.default : handler
309-
const _generateSitemaps = _mod ? _mod.generateSitemaps : generateSitemaps
305+
const _handler = _m?.default ?? handler
306+
const _generateSitemaps = _m?.generateSitemaps ?? generateSitemaps
310307
const sitemaps = await _generateSitemaps()
311308
let foundId
312309
for (const item of sitemaps) {{
@@ -360,13 +357,7 @@ async fn dynamic_sitemap_route_with_generate_source(
360357
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
361358
};
362359

363-
let file = File::from(code);
364-
let source = VirtualSource::new(
365-
path.parent().join(&format!("{stem}--route-entry.js"))?,
366-
AssetContent::file(FileContent::Content(file).cell()),
367-
);
368-
369-
Ok(Vc::upcast(source))
360+
make_route_entry_source(path.parent(), stem, code)
370361
}
371362

372363
async fn dynamic_sitemap_route_without_generate_source(
@@ -392,8 +383,7 @@ async fn dynamic_sitemap_route_without_generate_source(
392383
}}
393384
394385
export async function GET() {{
395-
// In Turbopack dev mode, re-require the user's module on every
396-
// request so that server HMR updates are reflected immediately.
386+
// In dev, re-require on every request to pick up server HMR updates.
397387
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
398388
? require({resource_path}).default
399389
: handler
@@ -416,13 +406,7 @@ async fn dynamic_sitemap_route_without_generate_source(
416406
cache_control = StringifyJs(CACHE_HEADER_REVALIDATE),
417407
};
418408

419-
let file = File::from(code);
420-
let source = VirtualSource::new(
421-
path.parent().join(&format!("{stem}--route-entry.js"))?,
422-
AssetContent::file(FileContent::Content(file).cell()),
423-
);
424-
425-
Ok(Vc::upcast(source))
409+
make_route_entry_source(path.parent(), stem, code)
426410
}
427411

428412
#[turbo_tasks::function]
@@ -462,13 +446,12 @@ async fn dynamic_image_route_with_metadata_source(
462446
return rest
463447
}})
464448
465-
// In Turbopack dev mode, re-require the user's module on every
466-
// request so that server HMR updates are reflected immediately.
467-
const _mod = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
449+
// In dev, re-require on every request to pick up server HMR updates.
450+
const _m = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
468451
? require({resource_path})
469452
: null
470-
const _handler = _mod ? _mod.default : handler
471-
const _generateImageMetadata = _mod ? _mod.generateImageMetadata : generateImageMetadata
453+
const _handler = _m?.default ?? handler
454+
const _generateImageMetadata = _m?.generateImageMetadata ?? generateImageMetadata
472455
const restParams = await restParamsPromise
473456
const __metadata_id__ = await idPromise
474457
const imageMetadata = await _generateImageMetadata({{ params: restParams }})
@@ -507,13 +490,7 @@ async fn dynamic_image_route_with_metadata_source(
507490
resource_path = StringifyJs(&format!("./{stem}.{ext}")),
508491
};
509492

510-
let file = File::from(code);
511-
let source = VirtualSource::new(
512-
path.parent().join(&format!("{stem}--route-entry.js"))?,
513-
AssetContent::file(FileContent::Content(file).cell()),
514-
);
515-
516-
Ok(Vc::upcast(source))
493+
make_route_entry_source(path.parent(), stem, code)
517494
}
518495

519496
async fn dynamic_image_route_without_metadata_source(
@@ -533,8 +510,7 @@ async fn dynamic_image_route_without_metadata_source(
533510
}}
534511
535512
export async function GET(_, ctx) {{
536-
// In Turbopack dev mode, re-require the user's module on every
537-
// request so that server HMR updates are reflected immediately.
513+
// In dev, re-require on every request to pick up server HMR updates.
538514
const _handler = (process.env.TURBOPACK && process.env.__NEXT_DEV_SERVER)
539515
? require({resource_path}).default
540516
: handler
@@ -546,13 +522,7 @@ async fn dynamic_image_route_without_metadata_source(
546522
resource_path = StringifyJs(&format!("./{stem}.{ext}")),
547523
};
548524

549-
let file = File::from(code);
550-
let source = VirtualSource::new(
551-
path.parent().join(&format!("{stem}--route-entry.js"))?,
552-
AssetContent::file(FileContent::Content(file).cell()),
553-
);
554-
555-
Ok(Vc::upcast(source))
525+
make_route_entry_source(path.parent(), stem, code)
556526
}
557527

558528
#[turbo_tasks::function]

0 commit comments

Comments
 (0)