Skip to content

Commit 6d9caf3

Browse files
authored
fix: incorrect canonicalUrl set when using output: export (#85019)
After the changes in 5ccc907, when in `output: 'export'` mode, the `canonicalUrl` value was getting corrupted to be set to one of the URLs of the RSC responses (eg `/another.txt`). This is because the original `url` gets mutated to append a `.txt` extension for the RSC requests. This ensures we preserve the original canonical URL so it doesn't get corrupted when we mutate the pathname. Failing test ref: https://github.com/vercel/next.js/actions/runs/18607156298/job/53060147186#step:34:151
1 parent 2f8b1ec commit 6d9caf3

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/next/src/client/components/router-reducer/fetch-server-response.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ export async function fetchServerResponse(
158158
headers[NEXT_URL] = nextUrl
159159
}
160160

161+
// In static export mode, we need to modify the URL to request the .txt file,
162+
// but we should preserve the original URL for the canonical URL and error handling.
163+
const originalUrl = url
164+
161165
try {
162166
// When creating a "temporary" prefetch (the "on-demand" prefetch that gets created on navigation, if one doesn't exist)
163167
// we send the request with a "high" priority as it's in response to a user interaction that could be blocking a transition.
@@ -198,7 +202,7 @@ export async function fetchServerResponse(
198202
)
199203

200204
const responseUrl = urlToUrlWithoutFlightMarker(new URL(res.url))
201-
const canonicalUrl = res.redirected ? responseUrl : url
205+
const canonicalUrl = res.redirected ? responseUrl : originalUrl
202206

203207
const contentType = res.headers.get('content-type') || ''
204208
const interception = !!res.headers.get('vary')?.includes(NEXT_URL)
@@ -285,15 +289,15 @@ export async function fetchServerResponse(
285289
} catch (err) {
286290
if (!abortController.signal.aborted) {
287291
console.error(
288-
`Failed to fetch RSC payload for ${url}. Falling back to browser navigation.`,
292+
`Failed to fetch RSC payload for ${originalUrl}. Falling back to browser navigation.`,
289293
err
290294
)
291295
}
292296

293297
// If fetch fails handle it like a mpa navigation
294298
// TODO-APP: Add a test for the case where a CORS request fails, e.g. external url redirect coming from the response.
295299
// See https://github.com/vercel/next.js/issues/43605#issuecomment-1451617521 for a reproduction.
296-
return url.toString()
300+
return originalUrl.toString()
297301
}
298302
}
299303

0 commit comments

Comments
 (0)