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 { ...@@ -1747,7 +1747,10 @@ export default class Router implements BaseRouter {
route = removeTrailingSlash(data.effect.resolvedHref) route = removeTrailingSlash(data.effect.resolvedHref)
pathname = data.effect.resolvedHref pathname = data.effect.resolvedHref
query = { ...query, ...data.effect.parsedAs.query } 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. // Check again the cache with the new destination.
existingInfo = this.components[route] existingInfo = this.components[route]
...@@ -1806,8 +1809,12 @@ export default class Router implements BaseRouter { ...@@ -1806,8 +1809,12 @@ export default class Router implements BaseRouter {
const { props } = await this._getData(async () => { const { props } = await this._getData(async () => {
if (shouldFetchData && !useStreamedFlightData) { if (shouldFetchData && !useStreamedFlightData) {
const { json } = const { json } =
data || data?.json &&
(await fetchNextData({ data?.response.headers
.get('content-type')
?.includes('application/json')
? data
: await fetchNextData({
dataHref: this.pageLoader.getDataHref({ dataHref: this.pageLoader.getDataHref({
href: formatWithValidation({ pathname, query }), href: formatWithValidation({ pathname, query }),
asPath: resolvedAs, asPath: resolvedAs,
...@@ -1819,7 +1826,7 @@ export default class Router implements BaseRouter { ...@@ -1819,7 +1826,7 @@ export default class Router implements BaseRouter {
persistCache: !isPreview, persistCache: !isPreview,
isPrefetch: false, isPrefetch: false,
unstable_skipClientCache, unstable_skipClientCache,
})) })
return { return {
props: json, props: json,
......
...@@ -16,6 +16,10 @@ module.exports = { ...@@ -16,6 +16,10 @@ module.exports = {
source: '/afterfiles-rewrite', source: '/afterfiles-rewrite',
destination: '/ab-test/b', destination: '/ab-test/b',
}, },
{
source: '/afterfiles-rewrite-ssg',
destination: '/fallback-true-blog/first',
},
], ],
fallback: [], fallback: [],
} }
......
...@@ -27,6 +27,30 @@ describe('Middleware Rewrite', () => { ...@@ -27,6 +27,30 @@ describe('Middleware Rewrite', () => {
testsWithLocale('/fr') testsWithLocale('/fr')
function tests() { 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 () => { it('should hard navigate on 404 for data request', async () => {
const browser = await webdriver(next.url, '/') const browser = await webdriver(next.url, '/')
await browser.eval('window.beforeNav = 1') 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