File size: 13,577 Bytes
b91e262 | 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
import * as nodePath from 'node:path'
import type { Playwright } from '../../../lib/next-webdriver'
describe.each([
{
description: 'without runtime prefetch configs',
hasRuntimePrefetch: false,
fixturePath: 'fixtures/without-prefetch-config',
},
{
description: 'with runtime prefetch configs',
hasRuntimePrefetch: true,
fixturePath: 'fixtures/with-prefetch-config',
},
])(
'cache-components-dev-warmup - $description',
({ fixturePath, hasRuntimePrefetch }) => {
const { next, isTurbopack } = nextTestSetup({
files: nodePath.join(__dirname, fixturePath),
})
// Restart the dev server for each test to clear the in-memory cache.
// We're testing cache-warming behavior here, so we don't want tests to interfere with each other.
let isFirstTest = true
beforeEach(async () => {
if (isFirstTest) {
// There's no point restarting if this is the first test.
isFirstTest = false
return
}
await next.stop()
await next.clean()
await next.start()
})
function assertLog(
logs: Array<{ source: string; message: string }>,
message: string,
expectedEnvironment: string
) {
// Match logs that contain the message, with any environment.
const logPattern = new RegExp(
`^(?=.*\\b${message}\\b)(?=.*\\b(Cache|Prerender|Prefetch|Prefetchable|Server)\\b).*`
)
const logMessages = logs.map((log) => log.message)
const messages = logMessages.filter((message) => logPattern.test(message))
// If there's zero or more than one logs that match, the test is not set up correctly.
if (messages.length === 0) {
throw new Error(
`Found no logs matching '${message}':\n\n${logMessages.map((s, i) => `${i}. ${s}`).join('\n')}}`
)
}
if (messages.length > 1) {
throw new Error(
`Found multiple logs matching '${message}':\n\n${messages.map((s, i) => `${i}. ${s}`).join('\n')}`
)
}
// The message should have the expected environment.
const actualMessageText = messages[0]
const [, actualEnvironment] = actualMessageText.match(logPattern)!
expect([actualEnvironment, actualMessageText]).toEqual([
expectedEnvironment,
expect.stringContaining(message),
])
}
async function testInitialLoad(
path: string,
assertLogs: (browser: Playwright) => Promise<void>
) {
const browser = await next.browser(path)
// Initial load.
await retry(() => assertLogs(browser))
// We should not see any errors related to the aborted render.
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
// After another load (with warm caches) the logs should be the same.
await browser.loadPage(next.url + path) // clears old logs
await retry(() => assertLogs(browser))
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
if (isTurbopack) {
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console.log('WARNING: skipping revalidation assertions in turbopack')
return
}
// After a revalidation the subsequent warmup render must discard stale
// cache entries.
// This should not affect the environment labels.
await revalidatePath(path)
await browser.loadPage(next.url + path) // clears old logs
await retry(() => assertLogs(browser))
// We should not see any errors related to the aborted render.
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
}
async function testNavigation(
path: string,
assertLogs: (browser: Playwright) => Promise<void>
) {
const browser = await next.browser('/')
// Initial nav (first time loading the page)
await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser))
// We should not see any errors related to the aborted render.
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
// Reload, and perform another nav (with warm caches). the logs should be the same.
await browser.loadPage(next.url + '/') // clears old logs
await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser))
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
if (isTurbopack) {
// FIXME:
// In Turbopack, requests to the /revalidate route seem to occasionally crash
// due to some HMR or compilation issue. `revalidatePath` throws this error:
//
// Invariant: static generation store missing in revalidatePath <path>
//
// This is unrelated to the logic being tested here, so for now, we skip the assertions
// that require us to revalidate.
console.log('WARNING: skipping revalidation assertions in turbopack')
return
}
// After a revalidation the subsequent warmup render must discard stale
// cache entries.
// This should not affect the environment labels.
await revalidatePath(path)
await browser.loadPage(next.url + '/') // clears old logs
await browser.elementByCss(`a[href="${path}"]`).click()
await retry(() => assertLogs(browser))
expect(next.cliOutput).not.toContain(
'AbortError: This operation was aborted'
)
}
async function revalidatePath(path: string) {
const response = await next.fetch(
`/revalidate?path=${encodeURIComponent(path)}`
)
if (!response.ok) {
throw new Error(
`Failed to revalidate path: '${path}' - server responded with status ${response.status}`
)
}
}
const RUNTIME_ENV = hasRuntimePrefetch ? 'Prefetch' : 'Prefetchable'
describe.each([
{ description: 'initial load', isInitialLoad: true },
{ description: 'navigation', isInitialLoad: false },
])('$description', ({ isInitialLoad }) => {
describe('cached data resolves in the correct phase', () => {
it('cached data + cached fetch', async () => {
const path = '/simple'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after cache read - layout', 'Prerender')
assertLog(logs, 'after cache read - page', 'Prerender')
assertLog(logs, 'after successive cache reads - page', 'Prerender')
assertLog(logs, 'after cached fetch - layout', 'Prerender')
assertLog(logs, 'after cached fetch - page', 'Prerender')
assertLog(logs, 'after uncached fetch - layout', 'Server')
assertLog(logs, 'after uncached fetch - page', 'Server')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
it('cached data + private cache', async () => {
const path = '/private-cache'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after cache read - layout', 'Prerender')
assertLog(logs, 'after cache read - page', 'Prerender')
// Private caches are dynamic holes in static prerenders,
// so they shouldn't resolve in the static stage.
assertLog(logs, 'after private cache read - page', RUNTIME_ENV)
assertLog(logs, 'after private cache read - layout', RUNTIME_ENV)
assertLog(
logs,
'after successive private cache reads - page',
RUNTIME_ENV
)
assertLog(logs, 'after uncached fetch - layout', 'Server')
assertLog(logs, 'after uncached fetch - page', 'Server')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
it('cached data + short-lived cached data', async () => {
const path = '/short-lived-cache'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after cache read - layout', 'Prerender')
assertLog(logs, 'after cache read - page', 'Prerender')
// Short lived caches are dynamic holes in static prerenders,
// so they shouldn't resolve in the static stage.
assertLog(logs, 'after short-lived cache read - page', RUNTIME_ENV)
assertLog(
logs,
'after short-lived cache read - layout',
RUNTIME_ENV
)
assertLog(logs, 'after uncached fetch - layout', 'Server')
assertLog(logs, 'after uncached fetch - page', 'Server')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
it('cache reads that reveal more components with more caches', async () => {
const path = '/successive-caches'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
// No matter how deeply we nest the component tree,
// if all the IO is cached, it should be labeled as Prerender.
assertLog(logs, 'after cache 1', 'Prerender')
assertLog(logs, 'after cache 2', 'Prerender')
assertLog(logs, 'after caches 1 and 2', 'Prerender')
assertLog(logs, 'after cache 3', 'Prerender')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
})
it('request APIs resolve in the correct phase', async () => {
const path = '/apis/123'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after cache read - page', 'Prerender')
// TODO: we should only label this as "Prefetch" if there's a prefetch config.
assertLog(logs, `after cookies`, RUNTIME_ENV)
assertLog(logs, `after headers`, RUNTIME_ENV)
assertLog(logs, `after params`, RUNTIME_ENV)
assertLog(logs, `after searchParams`, RUNTIME_ENV)
assertLog(logs, 'after connection', 'Server')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
// FIXME: it seems like in Turbopack we sometimes get two instances of `workUnitAsyncStorage` --
// `app-render` gets a second, newer instance, different from `io()`.
// Thus, `io()` gets an undefined `workUnitStore` and does nothing, so sync IO does not get tracked at all.
// This is likely caused by the same bug that breaks `/revalidate` (see other FIXME above),
// where a route crashes due to a missing `workStore`.
if (!isTurbopack) {
it('sync IO in the static phase', async () => {
const path = '/sync-io/static'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after first cache', 'Prerender')
// sync IO in the static stage errors and advances to Server.
assertLog(logs, 'after sync io', 'Server')
assertLog(logs, 'after cache read - page', 'Server')
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
it('sync IO in the runtime phase', async () => {
const path = '/sync-io/runtime'
const assertLogs = async (browser: Playwright) => {
const logs = await browser.log()
assertLog(logs, 'after first cache', 'Prerender')
assertLog(logs, 'after cookies', RUNTIME_ENV)
if (hasRuntimePrefetch) {
// if runtime prefetching is on, sync IO in the runtime stage errors and advances to Server.
assertLog(logs, 'after sync io', 'Server')
assertLog(logs, 'after cache read - page', 'Server')
} else {
// if runtime prefetching is not on, sync IO in the runtime stage does nothing.
assertLog(logs, 'after sync io', RUNTIME_ENV)
assertLog(logs, 'after cache read - page', RUNTIME_ENV)
}
}
if (isInitialLoad) {
await testInitialLoad(path, assertLogs)
} else {
await testNavigation(path, assertLogs)
}
})
}
})
}
)
|