Unverified Commit 7fa72048 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add config for opting out of optimistic client cache behavior (#38774)

* Add config for opting out of optimistic client cache behavior

* update test
parent 0de6269c
...@@ -1526,6 +1526,9 @@ export default async function getBaseWebpackConfig( ...@@ -1526,6 +1526,9 @@ export default async function getBaseWebpackConfig(
'process.env.__NEXT_NEW_LINK_BEHAVIOR': JSON.stringify( 'process.env.__NEXT_NEW_LINK_BEHAVIOR': JSON.stringify(
config.experimental.newNextLinkBehavior config.experimental.newNextLinkBehavior
), ),
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify(
config.experimental.optimisticClientCache
),
'process.env.__NEXT_CROSS_ORIGIN': JSON.stringify(crossOrigin), 'process.env.__NEXT_CROSS_ORIGIN': JSON.stringify(crossOrigin),
'process.browser': JSON.stringify(isClient), 'process.browser': JSON.stringify(isClient),
'process.env.__NEXT_TEST_MODE': JSON.stringify( 'process.env.__NEXT_TEST_MODE': JSON.stringify(
......
...@@ -328,6 +328,9 @@ const configSchema = { ...@@ -328,6 +328,9 @@ const configSchema = {
}, },
] as any, ] as any,
}, },
optimisticClientCache: {
type: 'boolean',
},
outputFileTracingRoot: { outputFileTracingRoot: {
minLength: 1, minLength: 1,
type: 'string', type: 'string',
......
...@@ -79,6 +79,7 @@ export interface NextJsWebpackConfig { ...@@ -79,6 +79,7 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
optimisticClientCache?: boolean
legacyBrowsers?: boolean legacyBrowsers?: boolean
browsersListForSwc?: boolean browsersListForSwc?: boolean
manualClientBasePath?: boolean manualClientBasePath?: boolean
...@@ -517,6 +518,7 @@ export const defaultConfig: NextConfig = { ...@@ -517,6 +518,7 @@ export const defaultConfig: NextConfig = {
swcMinify: false, swcMinify: false,
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined, output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
experimental: { experimental: {
optimisticClientCache: true,
runtime: undefined, runtime: undefined,
manualClientBasePath: false, manualClientBasePath: false,
// TODO: change default in next major release (current v12.1.5) // TODO: change default in next major release (current v12.1.5)
......
...@@ -2158,7 +2158,9 @@ export default class Router implements BaseRouter { ...@@ -2158,7 +2158,9 @@ export default class Router implements BaseRouter {
persistCache: !this.isPreview, persistCache: !this.isPreview,
isPrefetch: true, isPrefetch: true,
unstable_skipClientCache: unstable_skipClientCache:
options.unstable_skipClientCache || options.priority, options.unstable_skipClientCache ||
(options.priority &&
!!process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE),
}).then(() => false) }).then(() => false)
: false : false
}), }),
......
...@@ -9,16 +9,11 @@ import assert from 'assert' ...@@ -9,16 +9,11 @@ import assert from 'assert'
describe('Prerender prefetch', () => { describe('Prerender prefetch', () => {
let next: NextInstance let next: NextInstance
beforeAll(async () => { const runTests = ({
next = await createNext({ optimisticClientCache,
files: { }: {
pages: new FileRef(join(__dirname, 'app/pages')), optimisticClientCache?: boolean
}, }) => {
dependencies: {},
})
})
afterAll(() => next.destroy())
it('should not revalidate during prefetching', async () => { it('should not revalidate during prefetching', async () => {
const reqs = {} const reqs = {}
...@@ -36,7 +31,9 @@ describe('Prerender prefetch', () => { ...@@ -36,7 +31,9 @@ describe('Prerender prefetch', () => {
// wait for prefetch to occur // wait for prefetch to occur
await check(async () => { await check(async () => {
const cache = await browser.eval('JSON.stringify(window.next.router.sdc)') const cache = await browser.eval(
'JSON.stringify(window.next.router.sdc)'
)
return cache.includes('/blog/first') && cache.includes('/blog/second') return cache.includes('/blog/first') && cache.includes('/blog/second')
? 'success' ? 'success'
: cache : cache
...@@ -47,7 +44,9 @@ describe('Prerender prefetch', () => { ...@@ -47,7 +44,9 @@ describe('Prerender prefetch', () => {
// reload after revalidate period and wait for prefetch again // reload after revalidate period and wait for prefetch again
await check(async () => { await check(async () => {
const cache = await browser.eval('JSON.stringify(window.next.router.sdc)') const cache = await browser.eval(
'JSON.stringify(window.next.router.sdc)'
)
return cache.includes('/blog/first') && cache.includes('/blog/second') return cache.includes('/blog/first') && cache.includes('/blog/second')
? 'success' ? 'success'
: cache : cache
...@@ -129,13 +128,15 @@ describe('Prerender prefetch', () => { ...@@ -129,13 +128,15 @@ describe('Prerender prefetch', () => {
await browser.elementByCss('#to-blog-first').click() await browser.elementByCss('#to-blog-first').click()
await check(() => browser.elementByCss('#page').text(), 'blog/[slug]') await check(() => browser.elementByCss('#page').text(), 'blog/[slug]')
const newTime = JSON.parse(await browser.elementByCss('#props').text()).now const newTime = JSON.parse(
await browser.elementByCss('#props').text()
).now
expect(newTime).not.toBe(startTime) expect(newTime).not.toBe(startTime)
expect(isNaN(newTime)).toBe(false) expect(isNaN(newTime)).toBe(false)
}) })
if (process.env.DEVICE_NAME) { if (optimisticClientCache) {
it('should attempt cache update on touchstart', async () => { it('should attempt cache update on link hover/touch start', async () => {
const browser = await webdriver(next.url, '/') const browser = await webdriver(next.url, '/')
const timeRes = await fetchViaHTTP( const timeRes = await fetchViaHTTP(
next.url, next.url,
...@@ -153,9 +154,9 @@ describe('Prerender prefetch', () => { ...@@ -153,9 +154,9 @@ describe('Prerender prefetch', () => {
await browser.elementByCss('#to-blog-first').click() await browser.elementByCss('#to-blog-first').click()
await check(() => browser.elementByCss('#page').text(), 'blog/[slug]') await check(() => browser.elementByCss('#page').text(), 'blog/[slug]')
expect(JSON.parse(await browser.elementByCss('#props').text()).now).toBe( expect(
startTime JSON.parse(await browser.elementByCss('#props').text()).now
) ).toBe(startTime)
await browser.back().waitForElementByCss('#to-blog-first') await browser.back().waitForElementByCss('#to-blog-first')
const requests = [] const requests = []
...@@ -165,50 +166,48 @@ describe('Prerender prefetch', () => { ...@@ -165,50 +166,48 @@ describe('Prerender prefetch', () => {
// now trigger cache update and navigate again // now trigger cache update and navigate again
await check(async () => { await check(async () => {
if (process.env.DEVICE_NAME) {
await browser.elementByCss('#to-blog-second').touchStart() await browser.elementByCss('#to-blog-second').touchStart()
await browser.elementByCss('#to-blog-first').touchStart() await browser.elementByCss('#to-blog-first').touchStart()
} else {
await browser.elementByCss('#to-blog-second').moveTo()
await browser.elementByCss('#to-blog-first').moveTo()
}
return requests.some((url) => url.includes('/blog/first.json')) return requests.some((url) => url.includes('/blog/first.json'))
? 'success' ? 'success'
: requests : requests
}, 'success') }, 'success')
}) })
} else { } else {
it('should attempt cache update on link hover', async () => { it('should not attempt client cache update on link hover/touch start', async () => {
const browser = await webdriver(next.url, '/') const browser = await webdriver(next.url, '/')
const timeRes = await fetchViaHTTP( let requests = []
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()
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')
const requests = []
browser.on('request', (req) => { browser.on('request', (req) => {
requests.push(req.url()) requests.push(req.url())
}) })
// now trigger cache update and navigate again
await check(async () => { await check(async () => {
await browser.elementByCss('#to-blog-second').moveTo() const cacheKeys = await browser.eval(
await browser.elementByCss('#to-blog-first').moveTo() 'Object.keys(window.next.router.sdc)'
return requests.some((url) => url.includes('/blog/first.json')) )
return cacheKeys.some((url) => url.includes('/blog/first')) &&
cacheKeys.some((url) => url.includes('/blog/second'))
? 'success' ? 'success'
: requests : JSON.stringify(requests, null, 2)
}, 'success') }, 'success')
requests = []
if (process.env.DEVICE_NAME) {
await browser.elementByCss('#to-blog-second').touchStart()
await browser.elementByCss('#to-blog-first').touchStart()
} else {
await browser.elementByCss('#to-blog-second').moveTo()
await browser.elementByCss('#to-blog-first').moveTo()
}
expect(requests.filter((url) => url.includes('.json'))).toEqual([])
}) })
} }
...@@ -232,4 +231,38 @@ describe('Prerender prefetch', () => { ...@@ -232,4 +231,38 @@ describe('Prerender prefetch', () => {
await browser.waitForElementByCss('#to-blog-first') await browser.waitForElementByCss('#to-blog-first')
expect(await browser.eval('window.beforeNav')).toBeFalsy() expect(await browser.eval('window.beforeNav')).toBeFalsy()
}) })
}
describe('with optimisticClientCache enabled', () => {
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
},
dependencies: {},
})
})
afterAll(() => next.destroy())
runTests({ optimisticClientCache: true })
})
describe('with optimisticClientCache disabled', () => {
beforeAll(async () => {
next = await createNext({
files: {
pages: new FileRef(join(__dirname, 'app/pages')),
},
nextConfig: {
experimental: {
optimisticClientCache: false,
},
},
dependencies: {},
})
})
afterAll(() => next.destroy())
runTests({ optimisticClientCache: false })
})
}) })
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