Unverified Commit 962d545a authored by dan's avatar dan Committed by GitHub

[test] Unflake a prefetch-related race (#96222)

Test-only.

The test assertion was racy: it asserted no with-query-param RSC request
during hash-only navigations, but a late prefetch of the
`#link-to-query-param` link makes exactly that request (failed 3/3 on a
prod CI shard).

Assert the actual invariants instead:

- hash-only navigations issue no navigation RSC requests at all
(prefetches, identified by their headers, are ignored)
- a query change *does* issue one — with `prefetch={false}` on the link
so it can't be served from the prefetch cache.
parent 1f65c764
...@@ -30,7 +30,14 @@ export default function HashPage() { ...@@ -30,7 +30,14 @@ export default function HashPage() {
To non-existent To non-existent
</Link> </Link>
<div> <div>
<Link href="?with-query-param#hash-160" id="link-to-query-param"> <Link
href="?with-query-param#hash-160"
id="link-to-query-param"
// The test asserts that hash-only navigations never request this
// query's RSC payload; prefetching this link would race that
// assertion.
prefetch={false}
>
To 160 (with query param) To 160 (with query param)
</Link> </Link>
</div> </div>
......
...@@ -148,13 +148,20 @@ describe('app dir - navigation', () => { ...@@ -148,13 +148,20 @@ describe('app dir - navigation', () => {
describe('hash', () => { describe('hash', () => {
it('should scroll to the specified hash', async () => { it('should scroll to the specified hash', async () => {
const rscRequestUrls = new Set<string>() // Requests made to navigate, excluding prefetches: a hash-only
// navigation is same-document and must not fetch data, while
// prefetch heuristics are free to fire at any time.
const navigationRscRequestUrls = new Set<string>()
const browser = await next.browser('/hash', { const browser = await next.browser('/hash', {
beforePageLoad(page) { beforePageLoad(page) {
page.on('request', (req) => { page.on('request', (req) => {
const headers = req.headers() const headers = req.headers()
if (headers['rsc']) { if (
rscRequestUrls.add(req.url()) headers['rsc'] &&
!headers['next-router-prefetch'] &&
!headers['next-router-segment-prefetch']
) {
navigationRscRequestUrls.add(req.url())
} }
}) })
}, },
...@@ -178,8 +185,8 @@ describe('app dir - navigation', () => { ...@@ -178,8 +185,8 @@ describe('app dir - navigation', () => {
} }
// Wait for all network requests to finish, and then initialize the flag // Wait for all network requests to finish, and then initialize the flag
// used to determine if any query-param RSC requests are made. // used to determine if any navigation RSC requests are made.
rscRequestUrls.clear() navigationRscRequestUrls.clear()
await checkLink(6, 128) await checkLink(6, 128)
await checkLink(50, 744) await checkLink(50, 744)
...@@ -190,21 +197,16 @@ describe('app dir - navigation', () => { ...@@ -190,21 +197,16 @@ describe('app dir - navigation', () => {
await checkLink('non-existent', 0) await checkLink('non-existent', 0)
if (!isNextDev) { if (!isNextDev) {
// Hash-only navigations should not request the query-param payload. // Hash-only navigations must not fetch any data.
// In some runtimes, hash-only transitions can still trigger RSC expect(Array.from(navigationRscRequestUrls)).toEqual([])
// requests for /hash itself, so we assert on query-param payloads.
const hasQueryParamRscRequest = Array.from(rscRequestUrls).some((url) =>
url.includes('with-query-param')
)
expect(hasQueryParamRscRequest).toBe(false)
} }
await checkLink('query-param', 2284) await checkLink('query-param', 2284)
await browser.waitForIdleNetwork() await browser.waitForIdleNetwork()
// There should be an RSC request if the query param is changed // There should be an RSC request if the query param is changed
const hasQueryParamRscRequest = Array.from(rscRequestUrls).some((url) => const hasQueryParamRscRequest = Array.from(navigationRscRequestUrls).some(
url.includes('with-query-param') (url) => url.includes('with-query-param')
) )
expect(hasQueryParamRscRequest).toBe(true) expect(hasQueryParamRscRequest).toBe(true)
}) })
......
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