Skip to content

Commit

Permalink
fix metadata basePath for manifest (#76681)
Browse files Browse the repository at this point in the history
### What

Static metadata manifest file should be prefixed with `basePath` when it's specified, as all the requests are sent to the routes under `/<basePath>/*`, it should also show the correct url in the html

Fixes #56687
Closes NEXT-2707
  • Loading branch information
huozhi authored Feb 28, 2025
1 parent bbce270 commit 9483a37
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
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

0 comments on commit 9483a37

Please sign in to comment.