Unverified Commit accbe62f authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

test: force the failing response ordering in the loading.js prefetch repro

Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 3e18c216
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
// Stress repro for the canary flake in // Repro for the canary flake in
// router-autoscroll.test.ts › "Should apply scroll when loading.js is used". // router-autoscroll.test.ts › "Should apply scroll when loading.js is used".
// //
// Clicking the link scrolls it into view, which starts the link's // Clicking the off-screen link scrolls it into view, which starts the link's
// loading-boundary prefetch at about the same time as the navigation. When the // loading-boundary prefetch together with the navigation. That prefetch marks
// navigation is seeded while that prefetch is still in flight, the page // `__PAGE__` (below `loading-scroll/loading.tsx`) as Pending, and the
// segment's prefetch placeholder can resolve to `null` and render as empty // navigation uses `waitForSegmentCacheEntry(...).then(e => e ? e.rsc : null)`
// content instead of suspending, so `loading.js` is never shown and the page // as its prefetch data. The prefetch response fulfills `loading-scroll` and
// stays blank until the dynamic response finishes (~5s). The race is // rejects `__PAGE__` in the same pass, so `__PAGE__` resolves to `null` and
// timing-dependent, so the scenario is repeated. // renders as an empty segment instead of suspending into `loading.js`. The
// page stays blank until the dynamic response finishes (~5s).
//
// This test forces the ordering that fails: the navigation request is held
// until the loading-boundary prefetch has been issued (so the entries are
// Pending), then the navigation's own response arrives first and the prefetch
// lands later. Even with that ordering the outcome depends on React
// scheduling, so it's repeated.
// //
// See https://github.com/vercel/next.js/pull/99829 for the fix. // See https://github.com/vercel/next.js/pull/99829 for the fix.
const ITERATIONS = 25 const ITERATIONS = 10
const PREFETCH_DELAY_MS = 500
describe('router autoscroll - loading.js during in-flight prefetch', () => { describe('router autoscroll - loading.js during in-flight prefetch', () => {
const { next, isNextDev } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
...@@ -26,12 +34,55 @@ describe('router autoscroll - loading.js during in-flight prefetch', () => { ...@@ -26,12 +34,55 @@ describe('router autoscroll - loading.js during in-flight prefetch', () => {
} }
for (let i = 0; i < ITERATIONS; i++) { for (let i = 0; i < ITERATIONS; i++) {
it(`shows loading.js when navigating while the prefetch is in flight (#${i})`, async () => { it(`shows loading.js when the prefetch lands after the navigation response (#${i})`, async () => {
const browser = await next.browser('/') let releasePrefetch: () => void = () => {}
const prefetchHeld = new Promise<void>((r) => (releasePrefetch = r))
let onPrefetchIssued: () => void = () => {}
const prefetchIssued = new Promise<void>((r) => (onPrefetchIssued = r))
const browser = await next.browser('/', {
async beforePageLoad(page) {
await page.route(
(url) => url.pathname === '/loading-scroll',
async (route) => {
const headers = route.request().headers()
if (headers['rsc'] && !headers['next-router-prefetch']) {
// The navigation request: send it only once the
// loading-boundary prefetch is in flight.
await prefetchIssued
await route.continue()
// Let the navigation response arrive first, then release
// the prefetch.
setTimeout(releasePrefetch, PREFETCH_DELAY_MS)
return
}
// Hold the loading-boundary prefetch: a prefetch that isn't a
// per-segment (`/_tree`) request.
if (
headers['next-router-prefetch'] &&
headers['next-router-segment-prefetch'] === undefined
) {
onPrefetchIssued()
await prefetchHeld
}
await route.continue()
}
)
},
})
await browser.eval('window.scrollTo(0, 500)') await browser.eval('window.scrollTo(0, 500)')
await browser.elementByCss('#to-loading-scroll').click() await browser.elementByCss('#to-loading-scroll').click()
await Promise.race([
prefetchIssued,
new Promise((_, reject) =>
setTimeout(
() => reject(new Error('loading-boundary prefetch was not issued')),
5000
)
),
])
// The page sleeps for 5s, so the loading state must appear well before. // The page sleeps for 5s, so the loading state must appear well before.
await browser.waitForElementByCss('#loading-component', 4000) await browser.waitForElementByCss('#loading-component', 3000)
}) })
} }
}) })
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