Skip to content

Fix hydration middleware effects #31800

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

Merged
merged 5 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,28 +1133,35 @@ export default class Router implements BaseRouter {

resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)

const effect = await this._preflightRequest({
as,
cache: process.env.NODE_ENV === 'production',
pages,
pathname,
query,
})
/**
* If the route update was triggered for client-side hydration then
* do not check the preflight request. Otherwise when rendering
* a page with refresh it might get into an infinite loop.
*/
if ((options as any)._h !== 1) {
const effect = await this._preflightRequest({
as,
cache: process.env.NODE_ENV === 'production',
pages,
pathname,
query,
})

if (effect.type === 'rewrite') {
query = { ...query, ...effect.parsedAs.query }
resolvedAs = effect.asPath
pathname = effect.resolvedHref
parsed.pathname = effect.resolvedHref
url = formatWithValidation(parsed)
} else if (effect.type === 'redirect' && effect.newAs) {
return this.change(method, effect.newUrl, effect.newAs, options)
} else if (effect.type === 'redirect' && effect.destination) {
window.location.href = effect.destination
return new Promise(() => {})
} else if (effect.type === 'refresh') {
window.location.href = as
return new Promise(() => {})
if (effect.type === 'rewrite') {
query = { ...query, ...effect.parsedAs.query }
resolvedAs = effect.asPath
pathname = effect.resolvedHref
parsed.pathname = effect.resolvedHref
url = formatWithValidation(parsed)
} else if (effect.type === 'redirect' && effect.newAs) {
return this.change(method, effect.newUrl, effect.newAs, options)
} else if (effect.type === 'redirect' && effect.destination) {
window.location.href = effect.destination
return new Promise(() => {})
} else if (effect.type === 'refresh') {
window.location.href = as
return new Promise(() => {})
}
}

const route = removePathTrailingSlash(pathname)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export async function middleware(request) {
}
}

if (url.pathname.endsWith('/root-subrequest')) {
return fetch(
`http://${request.headers.get('host')}/interface/root-subrequest`
)
}

return new Response(null, {
headers: {
'req-url-basepath': request.nextUrl.basePath,
Expand Down
9 changes: 9 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ function interfaceTests(locale = '') {
expect(res.headers.get('req-url-locale')).toBe(locale.slice(1))
}
})

it(`${locale} renders correctly rewriting with a root subrequest`, async () => {
const browser = await webdriver(
context.appPort,
'/interface/root-subrequest'
)
const element = await browser.elementByCss('.title')
expect(await element.text()).toEqual('Dynamic route')
})
}

function getCookieFromResponse(res, cookieName) {
Expand Down