Unverified Commit 2a6199c2 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

fix(next-core): honor basepath for the metadata property (#62846)

### What?

When we write path for the og metadata, basePath was omitted regardless
of config. (Actual asset emission was correct, just writing the path in
the metadata route)

PR updates template code to use config's basePath if exists.

Closes PACK-2655
parent 446083e2
...@@ -40,6 +40,8 @@ pub struct LoaderTreeBuilder { ...@@ -40,6 +40,8 @@ pub struct LoaderTreeBuilder {
context: Vc<ModuleAssetContext>, context: Vc<ModuleAssetContext>,
server_component_transition: Vc<Box<dyn Transition>>, server_component_transition: Vc<Box<dyn Transition>>,
pages: Vec<Vc<FileSystemPath>>, pages: Vec<Vc<FileSystemPath>>,
/// next.config.js' basePath option to construct og metadata.
base_path: Option<String>,
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
...@@ -71,6 +73,7 @@ impl LoaderTreeBuilder { ...@@ -71,6 +73,7 @@ impl LoaderTreeBuilder {
fn new( fn new(
context: Vc<ModuleAssetContext>, context: Vc<ModuleAssetContext>,
server_component_transition: Vc<Box<dyn Transition>>, server_component_transition: Vc<Box<dyn Transition>>,
base_path: Option<String>,
) -> Self { ) -> Self {
LoaderTreeBuilder { LoaderTreeBuilder {
inner_assets: IndexMap::new(), inner_assets: IndexMap::new(),
...@@ -80,6 +83,7 @@ impl LoaderTreeBuilder { ...@@ -80,6 +83,7 @@ impl LoaderTreeBuilder {
context, context,
server_component_transition, server_component_transition,
pages: Vec::new(), pages: Vec::new(),
base_path,
} }
} }
...@@ -272,6 +276,7 @@ impl LoaderTreeBuilder { ...@@ -272,6 +276,7 @@ impl LoaderTreeBuilder {
alt_path: Option<Vc<FileSystemPath>>, alt_path: Option<Vc<FileSystemPath>>,
) -> Result<()> { ) -> Result<()> {
let i = self.unique_number(); let i = self.unique_number();
let identifier = magic_identifier::mangle(&format!("{name} #{i}")); let identifier = magic_identifier::mangle(&format!("{name} #{i}"));
let inner_module_id = format!("METADATA_{i}"); let inner_module_id = format!("METADATA_{i}");
let helper_import = "import { fillMetadataSegment } from \ let helper_import = "import { fillMetadataSegment } from \
...@@ -295,13 +300,17 @@ impl LoaderTreeBuilder { ...@@ -295,13 +300,17 @@ impl LoaderTreeBuilder {
let s = " "; let s = " ";
writeln!(self.loader_tree_code, "{s}(async (props) => [{{")?; writeln!(self.loader_tree_code, "{s}(async (props) => [{{")?;
let pathname_prefix = if let Some(base_path) = &self.base_path {
format!("{}/{}", base_path, app_page)
} else {
app_page.to_string()
};
let metadata_route = &*get_metadata_route_name((*item).into()).await?; let metadata_route = &*get_metadata_route_name((*item).into()).await?;
writeln!( writeln!(
self.loader_tree_code, self.loader_tree_code,
"{s} url: fillMetadataSegment({}, props.params, {}) + \ "{s} url: fillMetadataSegment({}, props.params, {}) + \
`?${{{identifier}.src.split(\"/\").splice(-1)[0]}}`,", `?${{{identifier}.src.split(\"/\").splice(-1)[0]}}`,",
StringifyJs(&app_page.to_string()), StringifyJs(&pathname_prefix),
StringifyJs(metadata_route), StringifyJs(metadata_route),
)?; )?;
...@@ -435,8 +444,9 @@ impl LoaderTreeModule { ...@@ -435,8 +444,9 @@ impl LoaderTreeModule {
loader_tree: Vc<LoaderTree>, loader_tree: Vc<LoaderTree>,
context: Vc<ModuleAssetContext>, context: Vc<ModuleAssetContext>,
server_component_transition: Vc<Box<dyn Transition>>, server_component_transition: Vc<Box<dyn Transition>>,
base_path: Option<String>,
) -> Result<Self> { ) -> Result<Self> {
LoaderTreeBuilder::new(context, server_component_transition) LoaderTreeBuilder::new(context, server_component_transition, base_path)
.build(loader_tree) .build(loader_tree)
.await .await
} }
......
...@@ -51,8 +51,10 @@ pub async fn get_app_page_entry( ...@@ -51,8 +51,10 @@ pub async fn get_app_page_entry(
let server_component_transition = Vc::upcast(NextServerComponentTransition::new()); let server_component_transition = Vc::upcast(NextServerComponentTransition::new());
let base_path = next_config.await?.base_path.clone();
let loader_tree = let loader_tree =
LoaderTreeModule::build(loader_tree, context, server_component_transition).await?; LoaderTreeModule::build(loader_tree, context, server_component_transition, base_path)
.await?;
let LoaderTreeModule { let LoaderTreeModule {
inner_assets, inner_assets,
......
...@@ -2389,11 +2389,10 @@ ...@@ -2389,11 +2389,10 @@
"app dir - basepath should render usePathname without the basePath", "app dir - basepath should render usePathname without the basePath",
"app dir - basepath should successfully hard navigate from pages -> app", "app dir - basepath should successfully hard navigate from pages -> app",
"app dir - basepath should support Link with basePath prefixed", "app dir - basepath should support Link with basePath prefixed",
"app dir - basepath should support `basePath`" "app dir - basepath should support `basePath`",
],
"failed": [
"app dir - basepath should prefix metadata og image with basePath" "app dir - basepath should prefix metadata og image with basePath"
], ],
"failed": [],
"pending": [], "pending": [],
"flakey": [], "flakey": [],
"runtimeError": false "runtimeError": false
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