Skip to content

Commit

Permalink
fix(next-core): carry over original segment config to metadata route (#…
Browse files Browse the repository at this point in the history
…63419)

### What

Fixes to honor metadata route's segment config - turbopack replaces it
into route handler, then parsed segment config so original segment
config was ignored always.

Closes PACK-2762
  • Loading branch information
kwonoj committed Mar 18, 2024
1 parent a8b88b7 commit 4064c64
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-api/src/app.rs
Expand Up @@ -495,6 +495,7 @@ impl AppEndpoint {
Vc::upcast(FileSource::new(path)),
self.page.clone(),
self.app_project.project().project_path(),
None,
)
}

Expand Down
Expand Up @@ -89,5 +89,6 @@ pub async fn get_app_route_favicon_entry(
// TODO(alexkirsz) Get this from the metadata?
AppPage(vec![PageSegment::Static("/favicon.ico".to_string())]),
project_root,
None,
))
}
Expand Up @@ -16,22 +16,31 @@ use turbopack_binding::{
};

use crate::{
app_segment_config::NextSegmentConfig,
next_app::{AppEntry, AppPage, AppPath},
next_edge::entry::wrap_edge_entry,
parse_segment_config_from_source,
util::{load_next_js_template, NextRuntime},
};

/// Computes the entry for a Next.js app route.
/// # Arguments
///
/// * `original_segment_config` - A next segment config to be specified
/// explicitly for the given source.
/// For some cases `source` may not be the original but the handler (dynamic
/// metadata) which will lose segment config.
#[turbo_tasks::function]
pub async fn get_app_route_entry(
nodejs_context: Vc<ModuleAssetContext>,
edge_context: Vc<ModuleAssetContext>,
source: Vc<Box<dyn Source>>,
page: AppPage,
project_root: Vc<FileSystemPath>,
original_segment_config: Option<Vc<NextSegmentConfig>>,
) -> Result<Vc<AppEntry>> {
let config = parse_segment_config_from_source(source);
let config =
original_segment_config.unwrap_or_else(|| parse_segment_config_from_source(source));
let is_edge = matches!(config.await?.runtime, Some(NextRuntime::Edge));
let context = if is_edge {
edge_context
Expand Down
Expand Up @@ -9,7 +9,10 @@ use turbo_tasks::{ValueToString, Vc};
use turbopack_binding::{
turbo::tasks_fs::{File, FileContent, FileSystemPath},
turbopack::{
core::{asset::AssetContent, source::Source, virtual_source::VirtualSource},
core::{
asset::AssetContent, file_source::FileSource, source::Source,
virtual_source::VirtualSource,
},
ecmascript::utils::StringifyJs,
turbopack::ModuleAssetContext,
},
Expand All @@ -20,6 +23,7 @@ use crate::{
app_structure::MetadataItem,
mode::NextMode,
next_app::{app_entry::AppEntry, app_route_entry::get_app_route_entry, AppPage, PageSegment},
parse_segment_config_from_source,
};

/// Computes the route source for a Next.js metadata file.
Expand Down Expand Up @@ -55,12 +59,22 @@ pub fn get_app_metadata_route_entry(
mode: NextMode,
metadata: MetadataItem,
) -> Vc<AppEntry> {
// Read original source's segment config before replacing source into
// dynamic|static metadata route handler.
let original_path = match metadata {
MetadataItem::Static { path } | MetadataItem::Dynamic { path } => path,
};

let source = Vc::upcast(FileSource::new(original_path));
let config = parse_segment_config_from_source(source);

get_app_route_entry(
nodejs_context,
edge_context,
get_app_metadata_route_source(page.clone(), mode, metadata),
page,
project_root,
Some(config),
)
}

Expand Down
5 changes: 2 additions & 3 deletions test/turbopack-tests-manifest.json
Expand Up @@ -3774,11 +3774,10 @@
"app dir - metadata dynamic routes social image routes should support params as argument in dynamic routes",
"app dir - metadata dynamic routes text routes should handle robots.[ext] dynamic routes",
"app dir - metadata dynamic routes text routes should handle sitemap.[ext] dynamic routes",
"app dir - metadata dynamic routes text routes should not throw if client components are imported but not used"
],
"failed": [
"app dir - metadata dynamic routes text routes should not throw if client components are imported but not used",
"app dir - metadata dynamic routes social image routes should handle custom fonts in both edge and nodejs runtime"
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit 4064c64

Please sign in to comment.