Unverified Commit 3da1f17d authored by Javi Velasco's avatar Javi Velasco Committed by GitHub

[middleware] Fix hydration for rewrites to dynamic pages (#32534)

Fixes #32302 

This PR fixes an issue on hydration where we rewrote using a middleware to a dynamic path. In such cases we need to run preflight to find out the middleware operation metadata.

## Bug

- [X] Related issues linked using `fixes #number`
- [X] Integration tests added
- [X] Errors have helpful link attached, see `contributing.md`
parent e75361fd
...@@ -1134,11 +1134,14 @@ export default class Router implements BaseRouter { ...@@ -1134,11 +1134,14 @@ export default class Router implements BaseRouter {
resolvedAs = delLocale(delBasePath(resolvedAs), this.locale) resolvedAs = delLocale(delBasePath(resolvedAs), this.locale)
/** /**
* If the route update was triggered for client-side hydration then * If the route update was triggered for client-side hydration and
* do not check the preflight request. Otherwise when rendering * the rendered route is not dynamic do not check the preflight
* a page with refresh it might get into an infinite loop. * request as it is not necessary.
*/ */
if ((options as any)._h !== 1) { if (
(options as any)._h !== 1 ||
isDynamicRoute(removePathTrailingSlash(pathname))
) {
const effect = await this._preflightRequest({ const effect = await this._preflightRequest({
as, as,
cache: process.env.NODE_ENV === 'production', cache: process.env.NODE_ENV === 'production',
...@@ -1158,7 +1161,7 @@ export default class Router implements BaseRouter { ...@@ -1158,7 +1161,7 @@ export default class Router implements BaseRouter {
} else if (effect.type === 'redirect' && effect.destination) { } else if (effect.type === 'redirect' && effect.destination) {
window.location.href = effect.destination window.location.href = effect.destination
return new Promise(() => {}) return new Promise(() => {})
} else if (effect.type === 'refresh') { } else if (effect.type === 'refresh' && as !== window.location.pathname) {
window.location.href = as window.location.href = as
return new Promise(() => {}) return new Promise(() => {})
} }
......
const PartsPage = () => {
return <div className="title">Parts page</div>
}
export default PartsPage
...@@ -57,6 +57,10 @@ export async function middleware(request) { ...@@ -57,6 +57,10 @@ export async function middleware(request) {
return fetch(url) return fetch(url)
} }
if (url.pathname.endsWith('/dynamic-replace')) {
return NextResponse.rewrite('/_interface/dynamic-path')
}
return new Response(null, { return new Response(null, {
headers: { headers: {
'req-url-basepath': request.nextUrl.basePath, 'req-url-basepath': request.nextUrl.basePath,
......
...@@ -455,6 +455,17 @@ function interfaceTests(locale = '') { ...@@ -455,6 +455,17 @@ function interfaceTests(locale = '') {
) )
expect(res.headers.get('x-dynamic-path')).toBe('true') expect(res.headers.get('x-dynamic-path')).toBe('true')
}) })
it(`${locale} renders correctly rewriting to a different dynamic path`, async () => {
const browser = await webdriver(
context.appPort,
'/interface/dynamic-replace'
)
const element = await browser.elementByCss('.title')
expect(await element.text()).toEqual('Parts page')
const logs = await browser.log()
expect(logs.every((log) => log.source === 'log')).toEqual(true)
})
} }
function getCookieFromResponse(res, cookieName) { function getCookieFromResponse(res, cookieName) {
......
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