Skip to content

Commit

Permalink
Fix middleware + afterFiles SSG rewrite case (#39153)
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jul 29, 2022
1 parent c567d7d commit a154c15
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
37 changes: 22 additions & 15 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/middleware-rewrites/app/next.config.js
Expand Up @@ -16,6 +16,10 @@ module.exports = {
source: '/afterfiles-rewrite',
destination: '/ab-test/b',
},
{
source: '/afterfiles-rewrite-ssg',
destination: '/fallback-true-blog/first',
},
],
fallback: [],
}
Expand Down
24 changes: 24 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Expand Up @@ -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')
Expand Down

0 comments on commit a154c15

Please sign in to comment.