Skip to content

Commit

Permalink
Ensure skipClientCache is honored for router.push (#40932)
Browse files Browse the repository at this point in the history
Fixes: #40927

## 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 27, 2022
1 parent 4f70f3a commit eb42404
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1550,6 +1550,7 @@ export default class Router implements BaseRouter {
locale: nextState.locale,
isPreview: nextState.isPreview,
hasMiddleware: isMiddlewareMatch,
unstable_skipClientCache: options.unstable_skipClientCache,
})

if ('route' in routeInfo && isMiddlewareMatch) {
Expand Down
46 changes: 46 additions & 0 deletions test/production/prerender-prefetch/index.test.ts
Expand Up @@ -135,6 +135,52 @@ describe('Prerender prefetch', () => {
expect(isNaN(newTime)).toBe(false)
})

it('should update cache using router.push with unstable_skipClientCache', async () => {
const browser = await webdriver(next.url, '/')
const timeRes = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/blog/first.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
const startTime = (await timeRes.json()).pageProps.now

// ensure stale data is used by default
await browser.elementByCss('#to-blog-first').click()
const outputIndex = next.cliOutput.length

await check(() => browser.elementByCss('#page').text(), 'blog/[slug]')

expect(JSON.parse(await browser.elementByCss('#props').text()).now).toBe(
startTime
)
await browser.back().waitForElementByCss('#to-blog-first')

// trigger revalidation of /blog/first
await check(async () => {
await renderViaHTTP(next.url, '/blog/first')
return next.cliOutput.substring(outputIndex)
}, /revalidating \/blog first/)

// now trigger cache update and navigate again
await browser.eval(
'next.router.push("/blog/first", undefined, { unstable_skipClientCache: true }).finally(() => { window.prefetchDone = "yes" })'
)
await check(() => browser.eval('window.prefetchDone'), 'yes')

await check(() => browser.elementByCss('#page').text(), 'blog/[slug]')

const newTime = JSON.parse(
await browser.elementByCss('#props').text()
).now
expect(newTime).not.toBe(startTime)
expect(isNaN(newTime)).toBe(false)
})

if (optimisticClientCache) {
it('should attempt cache update on link hover/touch start', async () => {
const browser = await webdriver(next.url, '/')
Expand Down

0 comments on commit eb42404

Please sign in to comment.