Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix metadata basePath for manifest #76681

Merged
merged 3 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions crates/next-core/src/app_page_loader_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,18 @@ impl AppPageLoaderTreeBuilder {
return Ok(());
};

let manifest_route = &format!("/{}", get_metadata_route_name(manifest).await?);
let metadata_manifest_route = get_metadata_route_name(manifest).await?;
// prefix with base_path if it exists
let manifest_route = if let Some(base_path) = &self.base_path {
format!("{}/{}", base_path, metadata_manifest_route)
} else {
metadata_manifest_route.to_string()
};

writeln!(
self.loader_tree_code,
" manifest: {},",
StringifyJs(manifest_route)
StringifyJs(&manifest_route)
)?;

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion packages/next/src/build/webpack/loaders/metadata/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export async function createStaticMetadataFromRoute(
const extension = staticManifestExtension.includes(ext.slice(1))
? ext.slice(1)
: 'webmanifest'
staticImagesMetadata.manifest = JSON.stringify(`/${name}.${extension}`)
staticImagesMetadata.manifest = JSON.stringify(
`${basePath}/${name}.${extension}`
)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function resolveUrl(
}

// Handle relative or absolute paths
const basePath = metadataBase.pathname || ''
const joinedPath = path.posix.join(basePath, url)
const pathname = metadataBase.pathname || ''
const joinedPath = path.posix.join(pathname, url)

return new URL(joinedPath, metadataBase)
}
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/app-basepath/app/manifest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { MetadataRoute } from 'next'

export default function manifest(): MetadataRoute.Manifest {
return {
name: 'NextJS Manifest',
}
}
3 changes: 3 additions & 0 deletions test/e2e/app-dir/app-basepath/app/metadata/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div id="page-metadata">Page Metadata</div>
}
11 changes: 8 additions & 3 deletions test/e2e/app-dir/app-basepath/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ describe('app dir - basepath', () => {
).toBe(`Page 2`)
})

it('should prefix metadata og image with basePath', async () => {
const $ = await next.render$('/base/another')
it('should prefix segment metadata og image with basePath and pathname', async () => {
const $ = await next.render$('/base/metadata')
const ogImageHref = $('meta[property="og:image"]').attr('content')
expect(ogImageHref).toContain('/base/metadata/opengraph-image.png')
})

expect(ogImageHref).toContain('/base/another/opengraph-image.png')
it('should prefix manifest with basePath', async () => {
const $ = await next.render$('/base/metadata')
const manifestHref = $('link[rel="manifest"]').attr('href')
expect(manifestHref).toContain('/base/manifest.webmanifest')
})

it('should prefix redirect() with basePath', async () => {
Expand Down
Loading