Skip to content

Commit

Permalink
fix(loader_tree): global, layout metadata correction
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Oct 20, 2023
1 parent 0447891 commit 72ddb5c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 15 deletions.
21 changes: 19 additions & 2 deletions packages/next-swc/crates/next-core/src/app_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,12 @@ async fn directory_tree_to_entrypoints_internal(

// Capture the current page for the metadata to calculate segment relative to
// the corresponding page for the static metadata files.
/*
let metadata = Metadata {
base_page: Some(app_page.clone()),
..components.metadata.clone()
}; */

let components = if components.metadata.base_page.is_some() {
components
} else {
Expand All @@ -740,6 +746,17 @@ async fn directory_tree_to_entrypoints_internal(
let current_level_is_parallel_route = is_parallel_route(&directory_name);

if let Some(page) = components.page {
// When resolving metadata with corresponding module
// (https://github.com/vercel/next.js/blob/aa1ee5995cdd92cc9a2236ce4b6aa2b67c9d32b2/packages/next/src/lib/metadata/resolve-metadata.ts#L340)
// layout takes precedence over page (https://github.com/vercel/next.js/blob/aa1ee5995cdd92cc9a2236ce4b6aa2b67c9d32b2/packages/next/src/server/lib/app-dir-module.ts#L22)
// If the component have layout and page both, do not attach same metadata to
// the page.
let metadata = if components.layout.is_some() {
Default::default()
} else {
components.metadata.clone()
};

add_app_page(
app_dir,
&mut result,
Expand All @@ -751,7 +768,7 @@ async fn directory_tree_to_entrypoints_internal(
parallel_routes: IndexMap::new(),
components: Components {
page: Some(page),
metadata: components.metadata.clone(),
metadata,
..Default::default()
}
.cell(),
Expand All @@ -769,7 +786,7 @@ async fn directory_tree_to_entrypoints_internal(
parallel_routes: IndexMap::new(),
components: Components {
page: Some(page),
metadata: components.metadata.clone(),
metadata,
..Default::default()
}
.cell(),
Expand Down
50 changes: 37 additions & 13 deletions packages/next-swc/crates/next-core/src/loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl LoaderTreeBuilder {
&mut self,
app_page: &AppPage,
metadata: &Metadata,
global_metadata: &GlobalMetadata,
global_metadata: Option<&GlobalMetadata>,
) -> Result<()> {
if metadata.is_empty() {
return Ok(());
Expand All @@ -172,15 +172,26 @@ impl LoaderTreeBuilder {
sitemap: _,
base_page,
} = metadata;
let GlobalMetadata {
favicon: _,
manifest,
robots: _,
} = global_metadata;

let app_page = base_page.as_ref().unwrap_or(app_page);
self.loader_tree_code += " metadata: {";

// naively convert metadataitem -> metadatawithaltitem to iterate along with
// other icon items
let icon = if let Some(favicon) = global_metadata.and_then(|m| m.favicon) {
let item = match favicon {
MetadataItem::Static { path } => MetadataWithAltItem::Static {
path,
alt_path: None,
},
MetadataItem::Dynamic { path } => MetadataWithAltItem::Dynamic { path },
};
let mut item = vec![item];
item.extend(icon.iter());
item
} else {
icon.clone()
};

self.write_metadata_items(app_page, "icon", icon.iter())
.await?;
self.write_metadata_items(app_page, "apple", apple.iter())
Expand All @@ -189,7 +200,11 @@ impl LoaderTreeBuilder {
.await?;
self.write_metadata_items(app_page, "openGraph", open_graph.iter())
.await?;
self.write_metadata_manifest(*manifest).await?;

if let Some(global_metadata) = global_metadata {
self.write_metadata_manifest(global_metadata.manifest)
.await?;
}
self.loader_tree_code += " },";
Ok(())
}
Expand Down Expand Up @@ -350,7 +365,7 @@ impl LoaderTreeBuilder {
}

#[async_recursion]
async fn walk_tree(&mut self, loader_tree: Vc<LoaderTree>) -> Result<()> {
async fn walk_tree(&mut self, loader_tree: Vc<LoaderTree>, root: bool) -> Result<()> {
use std::fmt::Write;

let LoaderTree {
Expand All @@ -369,7 +384,7 @@ impl LoaderTreeBuilder {
// add parallel_routes
for (key, &parallel_route) in parallel_routes.iter() {
write!(self.loader_tree_code, "{key}: ", key = StringifyJs(key))?;
self.walk_tree(parallel_route).await?;
self.walk_tree(parallel_route, false).await?;
writeln!(self.loader_tree_code, ",")?;
}
writeln!(self.loader_tree_code, "}}, {{")?;
Expand All @@ -396,14 +411,23 @@ impl LoaderTreeBuilder {
.await?;
self.write_component(ComponentType::NotFound, *not_found)
.await?;
self.write_metadata(app_page, metadata, &*global_metadata.await?)
.await?;

// Ensure global metadata being written only once at the root level
// Otherwise child pages will have redundant metadata
let global_metadata = &*global_metadata.await?;
self.write_metadata(
app_page,
metadata,
if root { Some(global_metadata) } else { None },
)
.await?;

write!(self.loader_tree_code, "}}]")?;
Ok(())
}

async fn build(mut self, loader_tree: Vc<LoaderTree>) -> Result<LoaderTreeModule> {
self.walk_tree(loader_tree).await?;
self.walk_tree(loader_tree, true).await?;
Ok(LoaderTreeModule {
imports: self.imports,
loader_tree_code: self.loader_tree_code,
Expand Down

0 comments on commit 72ddb5c

Please sign in to comment.