Skip to content
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

Update to use HEAD request for middleware query hydration #40973

Merged
merged 1 commit into from Sep 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1551,6 +1551,7 @@ export default class Router implements BaseRouter {
isPreview: nextState.isPreview,
hasMiddleware: isMiddlewareMatch,
unstable_skipClientCache: options.unstable_skipClientCache,
isQueryUpdating: isQueryUpdating && !this.isFallback,
})

if ('route' in routeInfo && isMiddlewareMatch) {
Expand Down Expand Up @@ -1896,6 +1897,7 @@ export default class Router implements BaseRouter {
hasMiddleware,
isPreview,
unstable_skipClientCache,
isQueryUpdating,
}: {
route: string
pathname: string
Expand All @@ -1907,6 +1909,7 @@ export default class Router implements BaseRouter {
locale: string | undefined
isPreview: boolean
unstable_skipClientCache?: boolean
isQueryUpdating?: boolean
}) {
/**
* This `route` binding can change if there's a rewrite
Expand Down Expand Up @@ -1949,6 +1952,7 @@ export default class Router implements BaseRouter {
persistCache: !isPreview,
isPrefetch: false,
unstable_skipClientCache,
isBackground: isQueryUpdating,
}

const data = await withMiddlewareEffects({
Expand All @@ -1957,6 +1961,10 @@ export default class Router implements BaseRouter {
locale: locale,
router: this,
})

if (isQueryUpdating && data) {
data.json = self.__NEXT_DATA__.props
}
handleCancelled()

if (
Expand Down
25 changes: 24 additions & 1 deletion test/e2e/middleware-general/test/index.test.ts
Expand Up @@ -200,9 +200,32 @@ describe('Middleware Runtime', () => {
})

it('should have correct query values for rewrite to ssg page', async () => {
const browser = await webdriver(next.url, '/to-ssg')
const browser = await webdriver(next.url, '/to-ssg', {
waitHydration: false,
})
const requests = []

browser.on('request', (req) => {
console.error('request', req.url(), req.method())
if (req.method() === 'HEAD') {
requests.push(req.url())
}
})
await browser.eval('window.beforeNav = 1')

await check(() => {
return requests.some((req) =>
new URL(req, 'http://n').pathname.endsWith('/to-ssg.json')
)
? 'found'
: JSON.stringify(requests)
}, 'found')

await check(
() => browser.eval('document.documentElement.innerHTML'),
/"from":"middleware"/
)

await check(() => browser.elementByCss('body').text(), /\/to-ssg/)

expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
Expand Down