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 canonical url for dynamic routes #49512

Merged
merged 6 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ function findDynamicParamFromRouterState(
export async function renderToHTMLOrFlight(
req: IncomingMessage,
res: ServerResponse,
pathname: string,
pagePath: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts
): Promise<RenderResult> {
const isFlight = req.headers[RSC.toLowerCase()] !== undefined
const initialCanonicalUrl = validateURL(req.url)
const pathname = new URL(req.url!, 'http://n').pathname
huozhi marked this conversation as resolved.
Show resolved Hide resolved

const {
buildManifest,
Expand Down Expand Up @@ -699,7 +701,7 @@ export async function renderToHTMLOrFlight(
!isValidElementType(Component)
) {
throw new Error(
`The default export is not a React Component in page: "${pathname}"`
`The default export is not a React Component in page: "${page}"`
)
}

Expand Down Expand Up @@ -1179,7 +1181,7 @@ export async function renderToHTMLOrFlight(
injectedCSS: new Set(),
injectedFontPreloadTags: new Set(),
rootLayoutIncluded: false,
asNotFound: pathname === '/404' || options?.asNotFound,
asNotFound: pagePath === '/404' || options?.asNotFound,
})
).map((path) => path.slice(1)) // remove the '' (root) segment

Expand Down Expand Up @@ -1216,8 +1218,6 @@ export async function renderToHTMLOrFlight(
Uint8Array
> = new TransformStream()

const initialCanonicalUrl = validateURL(req.url)

// Get the nonce from the incoming request if it has one.
const csp = req.headers['content-security-policy']
let nonce: string | undefined
Expand Down Expand Up @@ -1361,13 +1361,13 @@ export async function renderToHTMLOrFlight(
)
}

getTracer().getRootSpanAttributes()?.set('next.route', pathname)
getTracer().getRootSpanAttributes()?.set('next.route', pagePath)
const bodyResult = getTracer().wrap(
AppRenderSpan.getBodyResult,
{
spanName: `render route (app) ${pathname}`,
spanName: `render route (app) ${pagePath}`,
attributes: {
'next.route': pathname,
'next.route': pagePath,
},
},
async ({
Expand Down Expand Up @@ -1497,8 +1497,8 @@ export async function renderToHTMLOrFlight(
}
if (err.digest === NEXT_DYNAMIC_NO_SSR_CODE) {
warn(
`Entire page ${pathname} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`,
pathname
`Entire page ${pagePath} deopted into client-side rendering. https://nextjs.org/docs/messages/deopted-into-client-rendering`,
pagePath
)
}
if (isNotFoundError(err)) {
Expand Down Expand Up @@ -1571,7 +1571,7 @@ export async function renderToHTMLOrFlight(

const renderResult = new RenderResult(
await bodyResult({
asNotFound: pathname === '/404',
asNotFound: pagePath === '/404',
})
)

Expand Down Expand Up @@ -1625,7 +1625,7 @@ export async function renderToHTMLOrFlight(
() =>
StaticGenerationAsyncStorageWrapper.wrap(
staticGenerationAsyncStorage,
{ pathname, renderOpts },
{ pathname: pagePath, renderOpts },
() => wrappedRender()
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return 'alternate-child-dynamic-slug'
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ createNextDescribe(
rel: 'alternate',
href: 'https://example.com/alternates/child/de-DE',
})

await browser.loadPage(next.url + '/alternates/child/123')
await matchDom('link', 'rel="canonical"', {
href: 'https://example.com/alternates/child/123',
})
})

it('should support robots tags', async () => {
Expand Down