Skip to content

Commit

Permalink
Update to use HEAD request for middleware query hydration (#40973)
Browse files Browse the repository at this point in the history
When fetching the middleware rewrite information via `_next/data` for a static page that is not a `fallback` we can use a `HEAD` request instead of a `GET` request which provides the necessary header information and saves some bandwidth by avoiding sending back the data that is already present in the page. 

x-ref: [slack thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1664229966495469)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`
  • Loading branch information
ijjk committed Sep 28, 2022
1 parent 918b109 commit 91f0a7c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 91f0a7c

Please sign in to comment.