File size: 11,046 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
import { NextInstance } from 'e2e-utils'
import { createNext, FileRef } from 'e2e-utils'
import {
check,
fetchViaHTTP,
renderViaHTTP,
retry,
waitFor,
} from 'next-test-utils'
import { join } from 'path'
import webdriver from 'next-webdriver'
import assert from 'assert'
describe('Prerender prefetch', () => {
let next: NextInstance
const runTests = ({
optimisticClientCache,
}: {
optimisticClientCache?: boolean
}) => {
it('should not revalidate during prefetching', async () => {
const cliOutputStart = next.cliOutput.length
for (let i = 0; i < 3; i++) {
for (const path of ['/blog/first', '/blog/second']) {
const res = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}${path}.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
expect(res.status).toBe(200)
}
// do requests three times with 1 second between
// to go over revalidate period
await waitFor(1000)
}
expect(next.cliOutput.substring(cliOutputStart)).not.toContain(
'revalidating /blog'
)
})
it('should trigger revalidation after navigation', async () => {
const getData = () =>
fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/blog/first.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
const initialDataRes = await getData()
const initialData = await initialDataRes.json()
const browser = await webdriver(next.url, '/')
await browser.elementByCss('#to-blog-first').click()
await check(async () => {
const data = await getData()
assert.notDeepEqual(initialData, data)
return 'success'
}, 'success')
})
it('should update cache using prefetch 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/)
// wait for the revalidation to finish by comparing timestamps
await retry(async () => {
const timeRes = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/blog/first.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
const updatedTime = (await timeRes.json()).pageProps.now
expect(updatedTime).not.toBe(startTime)
})
// now trigger cache update and navigate again
await browser.eval(
'next.router.prefetch("/blog/first", undefined, { unstable_skipClientCache: true }).finally(() => { window.prefetchDone = "yes" })'
)
await check(() => browser.eval('window.prefetchDone'), 'yes')
await browser.elementByCss('#to-blog-first').click()
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)
})
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/)
// wait for the revalidation to finish by comparing timestamps
await retry(async () => {
const timeRes = await fetchViaHTTP(
next.url,
`/_next/data/${next.buildId}/blog/first.json`,
undefined,
{
headers: {
purpose: 'prefetch',
},
}
)
const updatedTime = (await timeRes.json()).pageProps.now
expect(updatedTime).not.toBe(startTime)
})
// 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, '/')
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()
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) => {
requests.push(req.url())
})
// now trigger cache update and navigate again
await check(async () => {
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()
}
return requests.some((url) => url.includes('/blog/first.json'))
? 'success'
: requests
}, 'success')
})
} else {
it('should not attempt client cache update on link hover/touch start', async () => {
const browser = await webdriver(next.url, '/')
let requests = []
browser.on('request', (req) => {
requests.push(req.url())
})
await check(async () => {
const cacheKeys = await browser.eval(
'Object.keys(window.next.router.sdc)'
)
return cacheKeys.some((url) => url.includes('/blog/first')) &&
cacheKeys.some((url) => url.includes('/blog/second'))
? 'success'
: JSON.stringify(requests, null, 2)
}, '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([])
})
}
it('should handle failed data fetch and empty cache correctly', async () => {
const browser = await webdriver(next.url, '/')
await browser.elementByCss('#to-blog-first').click()
// ensure we use the same port when restarting
const port = new URL(next.url).port
next.forcedPort = port
// trigger new build so buildId changes
await next.stop()
await next.start()
// clear router cache
await browser.eval('window.next.router.sdc = {}')
await browser.eval('window.beforeNav = 1')
await browser.back()
await browser.waitForElementByCss('#to-blog-first')
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: {},
env: {
// Simulate that a CDN has consumed the SWR cache-control header,
// otherwise the browser will cache responses and which messes with
// the expectations in this test.
// See https://github.com/vercel/next.js/pull/70674 for context.
NEXT_PRIVATE_CDN_CONSUMED_SWR_CACHE_CONTROL: '1',
},
})
})
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: {},
env: {
// Simulate that a CDN has consumed the SWR cache-control header,
// otherwise the browser will cache responses and which messes with
// the expectations in this test.
// See https://github.com/vercel/next.js/pull/70674 for context.
NEXT_PRIVATE_CDN_CONSUMED_SWR_CACHE_CONTROL: '1',
},
})
})
afterAll(() => next.destroy())
runTests({ optimisticClientCache: false })
})
})
|