Best for
- Use when writing or modifying tests that need to control the timing of internal Next.
vercel/next.js/.agents/skills/router-act/SKILL.md
How to write end-to-end tests using createRouterAct and LinkAccordion. Use when writing or modifying tests that need to control the timing of internal Next.js requests (like prefetches) or assert on their responses. Covers the act API, fixture patterns, prefetch control via LinkAccordion, fake clocks, and avoiding flaky testing patterns.
Decision brief
Use this skill when writing or modifying tests that involve prefetch requests, client router navigations, or the segment cache. The createRouterAct utility from test/lib/router-act.ts lets you assert on prefetch and navigation responses in an end-to-end way without coupling to t…
Compatibility matrix
| Platform | Status | Evidence | What to check |
|---|---|---|---|
| Codex | Not declared | No explicit evidence | Portability before use |
| Claude Code | Not declared | No explicit evidence | Portability before use |
| Cursor | Not declared | No explicit evidence | Portability before use |
| Gemini CLI | Not declared | No explicit evidence | Portability before use |
Installation
The source command is displayed only when detected. A safe inspection prompt is always available so your agent can explain every action before execution.
npx skills add https://github.com/vercel/next.js --skill ".agents/skills/router-act"Inspect the Agent Skill "router-act" from https://github.com/vercel/next.js/blob/bcea67d4e96fbaf8c51f50951e497724d2a02cfb/.agents/skills/router-act/SKILL.md at commit bcea67d4e96fbaf8c51f50951e497724d2a02cfb. List every install step, command, network request, credential, file read/write, external action, and rollback step. Explain whether it fits my task. Do not install or execute anything until I approve.
Workflow
Segment cache staleness tests use Playwright's clock API to control Date.now():
Don't bother with act if you don't need to instrument the network responses — either to control their timing or to assert on what's included in them. If all you're doing is waiting for some part of the UI to appear after a navigation, regular Playwright helpers like browser.elem…
1. Use LinkAccordion to control when prefetches happen. Never let links be visible outside an act scope. 2. Prefer 'no-requests' whenever the data should be served from cache. This is the strongest assertion — it proves the cache is working. 3. Avoid retry/polling timers. The ac…
When App Shells are enabled (the default when Cache Components is on), a prefetch is split into two phases: an App Shell prefetch — the param/searchParam-independent chrome of the route (layouts, loading boundaries, static shell) — and a separate per-link/per-page data prefetch.…
Review the “Config Options” section in the pinned source before continuing.
Permission review
No configured static risk pattern was detected
This is not proof of safety. Runtime behavior, indirect dependencies, and hidden external systems are outside the static scan.
Evidence record
| Signal | Value | Evidence type | Meaning |
|---|---|---|---|
| Quality score | 90/100 | Computed | Documentation, specificity, maintenance, and trust rules |
| Repository stars | 141,364 | Source | Repository attention, not individual Skill quality |
| Compatibility | 0 platforms | Source | Declared in the catalog source record |
| Usage guide | automated source guide | Editorial | Generated or reviewed according to the visible evidence level |
Pinned source
Use this skill when writing or modifying tests that involve prefetch requests, client router navigations, or the segment cache. The createRouterAct utility from test/lib/router-act.ts lets you assert on prefetch and navigation responses in an end-to-end way without coupling to the exact number of requests or the protocol details. This is why most client router-related tests use this pattern.
actDon't bother with act if you don't need to instrument the network responses — either to control their timing or to assert on what's included in them. If all you're doing is waiting for some part of the UI to appear after a navigation, regular Playwright helpers like browser.elementById(), browser.elementByCss(), and browser.waitForElementByCss() are sufficient.
LinkAccordion to control when prefetches happen. Never let links be visible outside an act scope.'no-requests' whenever the data should be served from cache. This is the strongest assertion — it proves the cache is working.act utility exists specifically to replace inherently flaky patterns like retry() loops or setTimeout waits for network activity. If you find yourself wanting to poll, you're probably not using act correctly.block feature. It's prone to false negatives. Prefer includes and 'no-requests' assertions instead.// Assert NO router requests are made (data served from cache).
// Prefer this whenever possible — it's the strongest assertion.
await act(async () => { ... }, 'no-requests')
// Expect at least one response containing this substring
await act(async () => { ... }, { includes: 'Page content' })
// Expect multiple responses (checked in order)
await act(async () => { ... }, [
{ includes: 'First response' },
{ includes: 'Second response' },
])
// Assert the same content appears in two separate responses
await act(async () => { ... }, [
{ includes: 'Repeated content' },
{ includes: 'Repeated content' },
])
// Expect at least one request, don't assert on content
await act(async () => { ... })
includes Matching Worksincludes substring is matched against the HTTP response body. Use text content that appears literally in the rendered output (e.g. 'Dynamic content (stale time 60s)').includes assertion are silently ignored — you only need to assert on the responses you care about. This keeps tests decoupled from the exact number of requests the router makes.includes expectation claims exactly one response. If the same substring appears in N separate responses, provide N separate { includes: '...' } entries.When App Shells are enabled (the default when Cache Components is on), a prefetch is split into two phases: an App Shell prefetch — the param/searchParam-independent chrome of the route (layouts, loading boundaries, static shell) — and a separate per-link/per-page data prefetch. The App Shell is conceptually part of the route, not prefetch data, so act ignores App Shell requests for all assertion purposes (they carry a next-router-prefetch: '3' header).
This means you generally do not need to account for the extra App Shell response in your assertions. If a Loading... fallback now arrives in both the App Shell prefetch and the per-link prefetch, you still write a single { includes: 'Loading...' } — the App Shell copy is invisible to matching. Likewise, 'no-requests' still passes even if an App Shell prefetch fires, and block: 'reject' won't match content that appears only in the App Shell.
App Shell requests are still intercepted, fulfilled, and awaited (so the shell is cached and no requests are left in flight) — they just don't participate in includes matching, no-requests, block: 'reject', or the "at least one request" check. An App Shell response that returns an error status (4xx/5xx) still fails the test.
To assert on App Shell responses directly — for tests specifically about App Shell behavior — opt in at the act instance level:
const act = createRouterAct(page, { includeAppShellRequests: true })
With this option, App Shell requests are treated like any other router request. Prefer expressing App Shell behavior through observable outcomes (e.g. an instant navigation rendering the cached shell before the data response arrives) rather than asserting on prefetch content where practical. See test/e2e/app-dir/segment-cache/prefetch-app-shell/prefetch-app-shell.test.ts for the canonical example.
act Does Internallyact intercepts all router requests — prefetches, navigations, and Server Actions — made during the scope:
requestIdleCallback (captures IntersectionObserver-triggered prefetches)Responses are buffered and only forwarded to the browser after the scope function returns. This means you cannot navigate to a new page and wait for it to render within the same scope — that would deadlock. Trigger the navigation (click the link) and let act handle the rest. Read destination page content after act returns:
await act(
async () => {
/* toggle accordion, click link */
},
{ includes: 'Page content' }
)
// Read content after act returns, not inside the scope
expect(await browser.elementById('my-content').text()).toBe('Page content')
LinkAccordion controls when <Link> components enter the DOM. A Next.js <Link> triggers a prefetch when it enters the viewport (via IntersectionObserver). By hiding the Link behind a checkbox toggle, you control exactly when prefetches happen — only when you explicitly toggle the accordion inside an act scope.
// components/link-accordion.tsx
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({ href, children, prefetch }) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
`${children} (link is hidden)`
)}
</>
)
}
Always toggle the accordion and click the link inside the same act scope:
await act(
async () => {
// 1. Toggle accordion — Link enters DOM, triggers prefetch
const toggle = await browser.elementByCss(
'input[data-link-accordion="/target-page"]'
)
await toggle.click()
// 2. Click the now-visible link — triggers navigation
const link = await browser.elementByCss('a[href="/target-page"]')
await link.click()
},
{ includes: 'Expected page content' }
)
browser.back() with open accordionsDo not use browser.back() to return to a page where accordions were previously opened. BFCache restores the full React state including useState values, so previously-opened Links are immediately visible. This triggers IntersectionObserver callbacks outside any act scope — if the cached data is stale, uncontrolled re-prefetches fire and break subsequent no-requests assertions.
The only safe use of browser.back()/browser.forward() is when testing BFCache behavior specifically.
Fix: navigate forward to a fresh hub page instead. See Hub Pages.
<Link> components outside act scopesAny <Link> visible in the viewport can trigger a prefetch at any time via IntersectionObserver. If this happens outside an act scope, the request is uncontrolled and can interfere with subsequent assertions. Always hide links behind LinkAccordion and only toggle them inside act.
retry(), setTimeout, or any polling pattern to wait for prefetches or navigations to settle is inherently flaky. act deterministically waits for all router requests to complete before returning.
act scopeResponses are buffered until the scope exits. Clicking a link then reading destination content in the same scope deadlocks. Read page content after act returns instead.
When you need to navigate away from a page and come back to test staleness, use "hub" pages instead of browser.back(). Each hub is a fresh page with its own LinkAccordion components that start closed.
Hub pages use connection() to ensure they are dynamically rendered. This guarantees that navigating to a hub always produces a router request, which lets act properly manage the navigation and wait for the page to fully render before continuing.
Hub page pattern:
// app/my-test/hub-a/page.tsx
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="/my-test/target-page">Target page</LinkAccordion>
</li>
</ul>
</>
)
}
Target pages link to hubs via LinkAccordion too:
// On target pages, add LinkAccordion links to hub pages
<LinkAccordion href="/my-test/hub-a">Hub A</LinkAccordion>
Test flow:
// 1. Navigate to target (first visit)
await act(
async () => {
/* toggle accordion, click link */
},
{ includes: 'Target content' }
)
// 2. Navigate to hub-a (fresh page, all accordions closed)
await act(
async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/my-test/hub-a"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/my-test/hub-a"]')
await link.click()
},
{ includes: 'Hub a' }
)
// 3. Advance time
await page.clock.setFixedTime(startDate + 60 * 1000)
// 4. Navigate back to target from hub (controlled prefetch)
await act(async () => {
const toggle = await browser.elementByCss(
'input[data-link-accordion="/my-test/target-page"]'
)
await toggle.click()
const link = await browser.elementByCss('a[href="/my-test/target-page"]')
await link.click()
}, 'no-requests') // or { includes: '...' } if data is stale
Segment cache staleness tests use Playwright's clock API to control Date.now():
async function startBrowserWithFakeClock(url: string) {
let page!: Playwright.Page
const startDate = Date.now()
const browser = await next.browser(url, {
async beforePageLoad(p: Playwright.Page) {
page = p
await page.clock.install()
await page.clock.setFixedTime(startDate)
},
})
const act = createRouterAct(page)
return { browser, page, act, startDate }
}
setFixedTime changes Date.now() return value but timers still run in real timeDate.now() for staleness checkssetFixedTime does NOT fire pending setTimeout/setInterval callbackscreateRouterAct: test/lib/router-act.tsLinkAccordion: test/e2e/app-dir/segment-cache/staleness/components/link-accordion.tsxtest/e2e/app-dir/segment-cache/staleness/Alternatives
vercel/next.js
Drive a Next.js route to instant navigation by setting up an agentic loop, under Cache Components / PPR, on initial load (hard navigation) and client-side navigation (soft navigation). Encode the goal as a failing @next/playwright instant() e2e and work it to green, one verified route at a time; the shipped test then guards against regression. Use when asked to make a route's navigation instant (its static shell commits immediately), fix a route whose static shell isn't prerendered/served/prefet
vercel/next.js
Maintain @next/rspack-core and @next/rspack-binding packages. Use when editing rspack/package.json, rspack/crates/binding/Cargo.toml, rspack/rust-toolchain.toml, or packages/next-rspack/package.json. Covers upgrading @rspack/core npm version, rspack_* crate versions, Rust toolchain version, building and linking for local testing, and NEXT_RSPACK environment variable usage. Does NOT apply to root rust-toolchain.toml (that's for Turbopack).
vercel/next.js
Benchmark React or Next.js changes on Vercel Sandbox VMs with paired A/B statistics: react PR/commit vs base, or Next.js PR/commit vs base, measured end-to-end through the bench/render-pipeline app (rps, latency, p95; TTFB, RSS and document/Flight bytes when the Next side captures them) and, for React changes, through the react repo's flight-ssr-bench fixture (Node AND Edge web-streams paths, Fizz and Flight+Fizz). Use whenever the user asks to bench, perf test, or A/B a React PR, a react-server
alirezarezvani/claude-skills
Frontend development skill for React, Next.js, TypeScript, and Tailwind CSS applications. Use when building React components, optimizing Next.js performance, analyzing bundle sizes, scaffolding frontend projects, implementing accessibility, or reviewing frontend code quality.