Unverified Commit a060fbf9 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Deflake per-page dynamic stale time test (#91492)

## Summary

- Fixes flaky test "per-page value overrides global staleTimes.dynamic
regardless of direction" introduced by #91437
- The test was flaky because `browser.back()` restored accordion state
from BFCache, causing previously-opened `LinkAccordion` links to be
immediately visible. This triggered uncontrolled re-prefetches outside
the `act` scope. When the `IntersectionObserver` fired inside a
subsequent `act` scope (after clock advancement), stale data would
trigger a prefetch that violated the `no-requests` assertion.
- Fix: instead of navigating back to a previously visited page, navigate
forward to fresh "hub" pages with their own `LinkAccordion` components.
Since these are never-visited pages, accordions start closed and no
uncontrolled prefetches are triggered.
- General principle: when using the router `act` test utility, always
use `LinkAccordion` to control when prefetches happen. Prefetches should
only be triggered inside an `act` scope by toggling an accordion, never
by navigating back to a page where links are already visible.

## Test plan

- [x] Ran the full test file 4 times locally — all 5 tests pass
consistently
parent 8283b126
This diff is collapsed.
......@@ -407,6 +407,7 @@ Core runtime/bundling rules (always apply; skills above expand on these with ver
- **`app-page.ts` is a build template compiled by the user's bundler**: Any `require()` in this file is traced by webpack/turbopack at `next build` time. You cannot require internal modules with relative paths because they won't be resolvable from the user's project. Instead, export new helpers from `entry-base.ts` and access them via `entryBase.*` in the template.
- **Reproducing CI failures locally**: Always match the exact CI env vars (check `pr-status` output for "Job Environment Variables"). Key differences: `IS_WEBPACK_TEST=1` forces webpack (turbopack is default), `NEXT_SKIP_ISOLATE=1` skips packing next.js (hides module resolution failures). Always run without `NEXT_SKIP_ISOLATE` when verifying module resolution fixes.
- **Showing full stack traces**: Set `__NEXT_SHOW_IGNORE_LISTED=true` to disable the ignore-list filtering in dev server error output. By default, Next.js collapses internal frames to `at ignore-listed frames`, which hides useful context when debugging framework internals. Defined in `packages/next/src/server/patch-error-inspect.ts`.
- **Router act tests must use LinkAccordion to control prefetches**: Always use `LinkAccordion` to control when prefetches happen inside `act` scopes. Never use `browser.back()` to return to a page where accordion links are already visible — BFCache restores state and triggers uncontrolled re-prefetches. See `$router-act` for full patterns.
### Rust/Cargo
......
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
export const unstable_dynamicStaleTime = 10
......@@ -12,8 +13,18 @@ async function Content() {
export default function Page() {
return (
<Suspense fallback="Loading...">
<Content />
</Suspense>
<>
<Suspense fallback="Loading...">
<Content />
</Suspense>
<ul>
<li>
<LinkAccordion href="/per-page-config/hub-a">Hub A</LinkAccordion>
</li>
<li>
<LinkAccordion href="/per-page-config/hub-b">Hub B</LinkAccordion>
</li>
</ul>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
export const unstable_dynamicStaleTime = 60
......@@ -12,8 +13,18 @@ async function Content() {
export default function Page() {
return (
<Suspense fallback="Loading...">
<Content />
</Suspense>
<>
<Suspense fallback="Loading...">
<Content />
</Suspense>
<ul>
<li>
<LinkAccordion href="/per-page-config/hub-a">Hub A</LinkAccordion>
</li>
<li>
<LinkAccordion href="/per-page-config/hub-c">Hub C</LinkAccordion>
</li>
</ul>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
async function Content() {
await connection()
return <div id="hub-a-content">Hub a</div>
}
export default function Page() {
return (
<>
<Suspense fallback="Loading...">
<Content />
</Suspense>
<ul>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-10">
Dynamic page with stale time of 10 seconds
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-60">
Dynamic page with stale time of 60 seconds
</LinkAccordion>
</li>
</ul>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
async function Content() {
await connection()
return <div id="hub-b-content">Hub b</div>
}
export default function Page() {
return (
<>
<Suspense fallback="Loading...">
<Content />
</Suspense>
<ul>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-10">
Dynamic page with stale time of 10 seconds
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-60">
Dynamic page with stale time of 60 seconds
</LinkAccordion>
</li>
</ul>
</>
)
}
import { Suspense } from 'react'
import { connection } from 'next/server'
import { LinkAccordion } from '../../../components/link-accordion'
async function Content() {
await connection()
return <div id="hub-c-content">Hub c</div>
}
export default function Page() {
return (
<>
<Suspense fallback="Loading...">
<Content />
</Suspense>
<ul>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-10">
Dynamic page with stale time of 10 seconds
</LinkAccordion>
</li>
<li>
<LinkAccordion href="/per-page-config/dynamic-stale-60">
Dynamic page with stale time of 60 seconds
</LinkAccordion>
</li>
</ul>
</>
)
}
......@@ -217,6 +217,11 @@ describe('segment cache (per-page dynamic stale time)', () => {
// The global staleTimes.dynamic is 30s. This test verifies that a per-page
// value of 10s (smaller) causes the data to expire sooner, and a per-page
// value of 60s (larger) causes the data to last longer.
//
// Instead of using browser.back() to return to a page that was already
// visited, this test navigates forward to fresh "hub" pages. This avoids
// flakiness caused by restored accordion state (from BFCache) triggering
// uncontrolled re-prefetches outside the act scope.
const { browser, page, act, startDate } =
await startBrowserWithFakeClock('/per-page-config')
......@@ -235,7 +240,22 @@ describe('segment cache (per-page dynamic stale time)', () => {
{ includes: 'Dynamic content (stale time 10s)' }
)
await browser.back()
// Navigate forward to hub/a — a fresh page with its own accordion links.
// Because this is a forward navigation to a never-visited page, the
// accordions start closed and no uncontrolled prefetches are triggered.
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/per-page-config/hub-a"]'
)
await toggle.click()
const link = await browser.elementByCss(
'a[href="/per-page-config/hub-a"]'
)
await link.click()
},
{ includes: 'Hub a' }
)
// At 11s the 10s page should be stale, even though the global default
// is 30s. This proves a smaller per-page value overrides the global.
......@@ -243,6 +263,10 @@ describe('segment cache (per-page dynamic stale time)', () => {
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/per-page-config/dynamic-stale-10"]'
)
await toggle.click()
const link = await browser.elementByCss(
'a[href="/per-page-config/dynamic-stale-10"]'
)
......@@ -251,7 +275,20 @@ describe('segment cache (per-page dynamic stale time)', () => {
{ includes: 'Dynamic content (stale time 10s)' }
)
await browser.back()
// Navigate forward to hub/b
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/per-page-config/hub-b"]'
)
await toggle.click()
const link = await browser.elementByCss(
'a[href="/per-page-config/hub-b"]'
)
await link.click()
},
{ includes: 'Hub b' }
)
// Now navigate to the 60s page
await act(
......@@ -268,13 +305,30 @@ describe('segment cache (per-page dynamic stale time)', () => {
{ includes: 'Dynamic content (stale time 60s)' }
)
await browser.back()
// Navigate forward to hub/c
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/per-page-config/hub-c"]'
)
await toggle.click()
const link = await browser.elementByCss(
'a[href="/per-page-config/hub-c"]'
)
await link.click()
},
{ includes: 'Hub c' }
)
// At 42s from the 60s page's navigation (11s + 31s), the data should
// still be fresh — the per-page value of 60s overrides the global 30s.
await page.clock.setFixedTime(startDate + 42 * 1000)
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/per-page-config/dynamic-stale-60"]'
)
await toggle.click()
const link = await browser.elementByCss(
'a[href="/per-page-config/dynamic-stale-60"]'
)
......
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