Unverified Commit 488984cc authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

fix: default relative canonical url should not contain search (#63843)

### What

Strip the search query for the `urlPathname` passed down to metadata
handling.

### Why
This is because the `urlPathname` from `staticGenerationStore` contains
query, so it will contain `?rsc` query for client navigation, which lead
to the relative path canonical url (e.g. `./`) will have the search
query along with it. This PR is to remove that and make sure always uses
pathname.

Reported by @pacocoursey 

Closes NEXT-2963
parent c116da32
...@@ -58,7 +58,8 @@ export function createMetadataComponents({ ...@@ -58,7 +58,8 @@ export function createMetadataComponents({
) => ParsedUrlQuery ) => ParsedUrlQuery
}): [React.ComponentType, React.ComponentType] { }): [React.ComponentType, React.ComponentType] {
const metadataContext = { const metadataContext = {
pathname, // Make sure the pathname without query string
pathname: pathname.split('?')[0],
trailingSlash, trailingSlash,
} }
......
export default function Page() { export default function Page() {
return 'alternate' return <p id="alternates">alternate</p>
} }
export async function generateMetadata(props, parentResolvingMetadata) { export async function generateMetadata(props, parentResolvingMetadata) {
......
...@@ -352,6 +352,23 @@ createNextDescribe( ...@@ -352,6 +352,23 @@ createNextDescribe(
}) })
}) })
it('should not contain query in canonical url after client navigation', async () => {
const browser = await next.browser('/')
await browser.waitForElementByCss('p#index')
await browser.eval(`next.router.push('/alternates')`)
// wait for /alternates page is loaded
await browser.waitForElementByCss('p#alternates')
const matchDom = createDomMatcher(browser)
await matchDom('link', 'rel="canonical"', {
href: 'https://example.com/alternates',
})
await matchDom('link', 'title="js title"', {
type: 'application/rss+xml',
href: 'https://example.com/blog/js.rss',
})
})
it('should support robots tags', async () => { it('should support robots tags', async () => {
const $ = await next.render$('/robots') const $ = await next.render$('/robots')
const matchMultiDom = createMultiHtmlMatcher($) const matchMultiDom = createMultiHtmlMatcher($)
......
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