Unverified Commit a154c157 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix middleware + afterFiles SSG rewrite case (#39153)

parent c567d7d2
......@@ -1747,7 +1747,10 @@ export default class Router implements BaseRouter {
route = removeTrailingSlash(data.effect.resolvedHref)
pathname = data.effect.resolvedHref
query = { ...query, ...data.effect.parsedAs.query }
resolvedAs = data.effect.parsedAs.pathname
resolvedAs = removeBasePath(
normalizeLocalePath(data.effect.parsedAs.pathname, this.locales)
.pathname
)
// Check again the cache with the new destination.
existingInfo = this.components[route]
......@@ -1806,20 +1809,24 @@ export default class Router implements BaseRouter {
const { props } = await this._getData(async () => {
if (shouldFetchData && !useStreamedFlightData) {
const { json } =
data ||
(await fetchNextData({
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
asPath: resolvedAs,
locale,
}),
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
}))
data?.json &&
data?.response.headers
.get('content-type')
?.includes('application/json')
? data
: await fetchNextData({
dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }),
asPath: resolvedAs,
locale,
}),
isServerRender: this.isSsr,
parseJSON: true,
inflightCache: this.sdc,
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
})
return {
props: json,
......
......@@ -16,6 +16,10 @@ module.exports = {
source: '/afterfiles-rewrite',
destination: '/ab-test/b',
},
{
source: '/afterfiles-rewrite-ssg',
destination: '/fallback-true-blog/first',
},
],
fallback: [],
}
......
......@@ -27,6 +27,30 @@ describe('Middleware Rewrite', () => {
testsWithLocale('/fr')
function tests() {
it('should have props for afterFiles rewrite to SSG page', async () => {
let browser = await webdriver(next.url, '/')
await browser.eval(`next.router.push("/afterfiles-rewrite-ssg")`)
await check(
() => browser.eval('next.router.isReady ? "yup": "nope"'),
'yup'
)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/"slug":"first"/
)
browser = await webdriver(next.url, '/afterfiles-rewrite-ssg')
await check(
() => browser.eval('next.router.isReady ? "yup": "nope"'),
'yup'
)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/"slug":"first"/
)
})
it('should hard navigate on 404 for data request', async () => {
const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1')
......
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