Skip to content

Commit

Permalink
Merge branch 'canary' into update/node-browser-polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 3, 2022
2 parents baca9fa + 82adaee commit 4c77318
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 4c77318

Please sign in to comment.