Skip to content

Commit

Permalink
Allow to opt-out from preflight cache (vercel#32767)
Browse files Browse the repository at this point in the history
Fixes vercel#32727

With this PR we introduce a new header that can be used to respond from Middleware `x-middleware-cache`. When the value of this header is set to `no-cache`, the client will **not** store the effects read from a preflight response to be used in an upcoming check.

Instead of using `Cache-Control` we are using a custom header to not mess with browser specific caching. Accepting a specific value to opt out (`no-cache`) opens the future opportunity of having other caching strategies.

This feature solves the issue of having a preflight request whose parameters can change from the client affecting the effects. For example, having a cookie that would make a middleware rewrite to one pathname or another and allowing to change that cookie from the client. In that case we'd always need to revalidate the effects on navigation synchronously.

Of course when using this feature it is possible that we add some latency on navigation so the preferred mechanism will be caching by default since it covers the most common use cases.
  • Loading branch information
javivelasco committed Jan 3, 2022
1 parent 4d30771 commit 82adaee
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/next/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ interface NextHistoryState {
}

interface PreflightData {
cache?: string | null
redirect?: string | null
refresh?: boolean
rewrite?: string | null
Expand Down Expand Up @@ -1950,14 +1951,15 @@ export default class Router implements BaseRouter {
}

return {
cache: res.headers.get('x-middleware-cache'),
redirect: res.headers.get('Location'),
refresh: res.headers.has('x-middleware-refresh'),
rewrite: res.headers.get('x-middleware-rewrite'),
ssr: !!res.headers.get('x-middleware-ssr'),
}
})
.then((data) => {
if (shouldCache) {
if (shouldCache && data.cache !== 'no-cache') {
this.sde[cacheKey] = data
}

Expand Down
10 changes: 8 additions & 2 deletions test/integration/middleware/core/pages/rewrites/_middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ export async function middleware(request) {
}

if (url.pathname === '/rewrites/rewrite-me-without-hard-navigation') {
url.pathname = '/rewrites/about'
url.searchParams.set('middleware', 'foo')
return NextResponse.rewrite(url)
url.pathname =
request.cookies['about-bypass'] === '1'
? '/rewrites/about-bypass'
: '/rewrites/about'

const response = NextResponse.rewrite(url)
response.headers.set('x-middleware-cache', 'no-cache')
return response
}
}
12 changes: 12 additions & 0 deletions test/integration/middleware/core/pages/rewrites/about-bypass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function AboutBypass({ message }) {
return (
<div>
<h1 className="title">About Bypassed Page</h1>
<p className={message}>{message}</p>
</div>
)
}

export const getServerSideProps = ({ query }) => ({
props: { message: query.message || '' },
})
12 changes: 12 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ function rewriteTests(locale = '') {
const element = await browser.elementByCss('.middleware')
expect(await element.text()).toEqual('foo')
})

it('should allow to opt-out preflight caching', async () => {
const browser = await webdriver(context.appPort, '/rewrites/')
await browser.addCookie({ name: 'about-bypass', value: '1' })
await browser.eval('window.__SAME_PAGE = true')
await browser.elementByCss('#link-with-rewritten-url').click()
await browser.waitForElementByCss('.refreshed')
await browser.deleteCookies()
expect(await browser.eval('window.__SAME_PAGE')).toBe(true)
const element = await browser.elementByCss('.title')
expect(await element.text()).toEqual('About Bypassed Page')
})
}

function redirectTests(locale = '') {
Expand Down

0 comments on commit 82adaee

Please sign in to comment.