Unverified Commit 82adaee0 authored by Javi Velasco's avatar Javi Velasco Committed by GitHub

Allow to opt-out from preflight cache (#32767)

Fixes #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.
parent 4d307716
......@@ -63,6 +63,7 @@ interface NextHistoryState {
}
interface PreflightData {
cache?: string | null
redirect?: string | null
refresh?: boolean
rewrite?: string | null
......@@ -1950,6 +1951,7 @@ 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'),
......@@ -1957,7 +1959,7 @@ export default class Router implements BaseRouter {
}
})
.then((data) => {
if (shouldCache) {
if (shouldCache && data.cache !== 'no-cache') {
this.sde[cacheKey] = data
}
......
......@@ -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
}
}
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 || '' },
})
......@@ -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 = '') {
......
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