Files
next.js/test/e2e/app-dir/ppr-navigations/stale-prefetch-entry/stale-prefetch-entry.test.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

107 lines
3.7 KiB
TypeScript

import { createNext } from 'e2e-utils'
import { findPort } from 'next-test-utils'
import { createTestDataServer } from 'test-data-service/writer'
import { createTestLog } from 'test-log'
describe('stale-prefetch-entry', () => {
if ((global as any).isNextDev || (global as any).isNextDeploy) {
// this is skipped in dev because PPR is not enabled in dev
// and in deploy we can't rely on this test data service existing
test('should skip dev & deploy', () => {})
return
}
let server
let next
afterEach(async () => {
await next?.destroy()
server?.close()
})
// This is a regression test where a condition that checked whether a cached
// route entry was stale accidentally caused the router to treat prefetched
// data as if it were complete data that didn't contain dynamic holes. This
// prevented the dynamic data from ever streaming in.
test(
'works if a prefetched route entry has become stale (too much ' +
'time has elapsed since it was prefetched)',
async () => {
const TestLog = createTestLog()
let autoresolveRequests = true
let pendingRequests = new Map()
server = createTestDataServer(async (key, res) => {
TestLog.log('REQUEST: ' + key)
if (autoresolveRequests) {
res.resolve()
return
}
if (pendingRequests.has(key)) {
throw new Error('Request already pending for ' + key)
}
pendingRequests.set(key, res)
})
const port = await findPort()
server.listen(port)
next = await createNext({
files: __dirname,
env: { TEST_DATA_SERVICE_URL: `http://localhost:${port}` },
})
TestLog.assert(['REQUEST: Some data [static]'])
autoresolveRequests = false
const browser = await next.browser('/', {
// Override Date.now so we can simulate time passing to expire a
// prefetch entry.
async beforePageLoad(page) {
await page.addInitScript(`
__FAKE_NOW__ = 0
Date = class extends Date {
constructor(...args) {
if (args.length === 0) {
super(__FAKE_NOW__)
} else {
super(...args)
}
}
static now() {
return __FAKE_NOW__
}
}
`)
},
})
const startTime = await browser.eval(() => Date.now())
// Explicitly hover over the link to trigger a prefetch.
const link = await browser.elementByCss('a[href="/some-page"]')
await link.hover()
// Increment time by 3 minutes. This is longer than the default expiration
// interval for prefetch entries.
await browser.eval(`__FAKE_NOW__ = 60 * 1000 * 3`)
const endTime = await browser.eval(() => Date.now())
// Sanity check.
expect(endTime - startTime).toBe(60 * 1000 * 3)
// Navigate. The prefetch entry will be stale.
await link.click()
// The static UI appears immediately because it was prerendered at
// build time.
const staticContainer = await browser.elementById('static')
expect(await staticContainer.innerText()).toBe('Some data [static]')
// The dynamic data is fetched on navigation. (In the regression case that
// this test simulates, the dynamic data was never requested, and the page
// got stuck in a loading state.)
await TestLog.waitFor(['REQUEST: Some data [dynamic]'])
pendingRequests.get('Some data [dynamic]').resolve()
// Now the dynamic data appears.
const dynamicContainer = await browser.elementById('dynamic')
expect(await dynamicContainer.innerText()).toBe('Some data [dynamic]')
}
)
})