Commit b455670b authored by Janka Uryga's avatar Janka Uryga

migrate some tests to from prefetch=true to unstable_forceStale

parent 8719a85b
......@@ -16,13 +16,13 @@ export default async function Page(props) {
<Link href="/search-params?id=2">/search-params?id=2</Link>
</li>
<li>
<Link href="/search-params?id=3" prefetch>
/search-params?id=3 (prefetch: true)
<Link href="/search-params?id=3" prefetch="unstable_forceStale">
/search-params?id=3 (prefetch: unstable_forceStale)
</Link>
</li>
<li>
<Link href="/search-params" prefetch>
/search-params (prefetch: true)
<Link href="/search-params" prefetch="unstable_forceStale">
/search-params (prefetch: unstable_forceStale)
</Link>
</li>
<li>
......
......@@ -5,7 +5,7 @@ export default function Page() {
<ul>
<li>
<Link href="/with-middleware/search-params?id=1">
/search-params?id=1 (prefetch: true)
/search-params?id=1
</Link>
</li>
<li>
......@@ -14,12 +14,18 @@ export default function Page() {
</Link>
</li>
<li>
<Link href="/with-middleware/search-params?id=3" prefetch>
<Link
href="/with-middleware/search-params?id=3"
prefetch="unstable_forceStale"
>
/search-params?id=3 (prefetch: true)
</Link>
</li>
<li>
<Link href="/with-middleware/search-params" prefetch>
<Link
href="/with-middleware/search-params"
prefetch="unstable_forceStale"
>
/search-params (prefetch: true)
</Link>
</li>
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -10,7 +10,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -10,7 +10,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -10,7 +10,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -11,7 +11,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
unstable_dynamicOnHover?: boolean
}) {
const [isVisible, setIsVisible] = useState(false)
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -11,7 +11,7 @@ export function LinkAccordion({
}: {
href: string
children: string
prefetch?: boolean
prefetch?: LinkProps['prefetch']
id?: string
}) {
const [isVisible, setIsVisible] = useState(false)
......
......@@ -22,10 +22,10 @@ export default function MixedFetchStrategies() {
<li>
<LinkAccordion
id="ppr-enabled-prefetch-true"
prefetch={true}
prefetch="unstable_forceStale"
href="/mixed-fetch-strategies/has-loading-boundary/a/b/shared-layout/ppr-enabled"
>
Same link, but with prefetch=true
Same link, but with prefetch="unstable_forceStale"
</LinkAccordion>
</li>
</ul>
......
......@@ -285,7 +285,7 @@ describe('segment cache (incremental opt in)', () => {
)
it(
'when a link is prefetched with <Link prefetch=true>, no dynamic request ' +
'when a link is prefetched with <Link prefetch="unstable_forceStale">, no dynamic request ' +
'is made on navigation',
async () => {
let act
......@@ -324,7 +324,7 @@ describe('segment cache (incremental opt in)', () => {
)
it(
'when prefetching with prefetch=true, refetches cache entries that only ' +
'when prefetching with prefetch="unstable_forceStale", refetches cache entries that only ' +
'contain partial data',
async () => {
let act
......@@ -343,7 +343,7 @@ describe('segment cache (incremental opt in)', () => {
{ includes: 'Loading (PPR shell of shared-layout)...' }
)
// Prefetch the same link again, this time with prefetch=true to include
// Prefetch the same link again, this time with prefetch="unstable_forceStale" to include
// the dynamic data
await act(
async () => {
......@@ -370,7 +370,7 @@ describe('segment cache (incremental opt in)', () => {
// If this fails, it likely means that the partial cache entry that
// resulted from prefetching the normal link (<Link prefetch={false}>)
// was not properly re-fetched when the full link (<Link
// prefetch={true}>) was prefetched.
// prefetch='unstable_forceStale'>) was prefetched.
await browser.elementById('page-content')
},
// Assert that no network requests are initiated within this block.
......@@ -380,7 +380,7 @@ describe('segment cache (incremental opt in)', () => {
)
it(
'when prefetching with prefetch=true, refetches partial cache entries ' +
'when prefetching with prefetch="unstable_forceStale", refetches partial cache entries ' +
"even if there's already a pending PPR request",
async () => {
// This test is hard to describe succinctly because it involves a fairly
......@@ -427,7 +427,7 @@ describe('segment cache (incremental opt in)', () => {
)
// Before the previous prefetch finishes, prefetch the same link again,
// this time with prefetch=true to include the dynamic data.
// this time with prefetch="unstable_forceStale" to include the dynamic data.
await act(
async () => {
const checkbox = await browser.elementById(
......@@ -457,7 +457,7 @@ describe('segment cache (incremental opt in)', () => {
// If this fails, it likely means that the pending cache entry that
// resulted from prefetching the normal link (<Link prefetch={false}>)
// was not properly re-fetched when the full link (<Link
// prefetch={true}>) was prefetched.
// prefetch='unstable_forceStale'>) was prefetched.
await browser.elementById('page-content')
},
// Assert that no network requests are initiated within this block.
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -11,7 +11,7 @@ export function LinkAccordion({
}: {
href: string
children: string
prefetch?: boolean | 'unstable_forceStale' | 'auto'
prefetch?: LinkProps['prefetch']
id?: string
}) {
const [isVisible, setIsVisible] = useState(false)
......@@ -25,7 +25,6 @@ export function LinkAccordion({
id={id}
/>
{isVisible ? (
// @ts-expect-error - unstable_forceStale is not yet part of the types
<Link href={href} prefetch={prefetch}>
{children}
</Link>
......
......@@ -5,16 +5,19 @@ export default function Page() {
<>
<ul>
<li>
<LinkAccordion prefetch={true} href="/page-with-dynamic-head">
Page with dynamic head (prefetch=true))
<LinkAccordion
prefetch="unstable_forceStale"
href="/page-with-dynamic-head"
>
Page with dynamic head (prefetch=unstable_forceStale)
</LinkAccordion>
</li>
<li>
<LinkAccordion
prefetch={true}
prefetch="unstable_forceStale"
href="/rewrite-to-page-with-dynamic-head"
>
Rewrite to page with dynamic head (prefetch=true)
Rewrite to page with dynamic head (prefetch=unstable_forceStale)
</LinkAccordion>
</li>
</ul>
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -10,7 +10,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......
'use client'
import Link, { LinkProps } from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......
......@@ -10,16 +10,17 @@ export default function RefetchOnNewBaseTreeLayout({
<div style={{ backgroundColor: 'lightgray', padding: '1rem' }}>
<p>
This demonstrates what happens when a link is prefetched using{' '}
<code>{'prefetch={true}'}</code> and the URL changes. Next.js should
re-prefetch the link in case the delta between the base tree and the
target tree has changed.
<code>{'prefetch="unstable_forceStale"'}</code> and the URL changes.
Next.js should re-prefetch the link in case the delta between the base
tree and the target tree has changed.
</p>
<p>
Everything in this gray section is part of a shared layout. The links
below are prefetched using <code>{'prefetch={true}'}</code>. If the
first loaded page is "/refetch-on-new-base-tree/a", the prefetch for
this link will be empty, because there's no delta between the base
tree and the target tree.
below are prefetched using{' '}
<code>{'prefetch="unstable_forceStale"'}</code>. If the first loaded
page is "/refetch-on-new-base-tree/a", the prefetch for this link will
be empty, because there's no delta between the base tree and the
target tree.
</p>
<p>
However, if you then navigate to page B, we should re-prefetch the
......@@ -48,10 +49,16 @@ export default function RefetchOnNewBaseTreeLayout({
because it was fully prefetched.
</li>
</ul>
<LinkAccordion prefetch={true} href="/refetch-on-new-base-tree/a">
<LinkAccordion
prefetch="unstable_forceStale"
href="/refetch-on-new-base-tree/a"
>
Page A
</LinkAccordion>
<LinkAccordion prefetch={true} href="/refetch-on-new-base-tree/b">
<LinkAccordion
prefetch="unstable_forceStale"
href="/refetch-on-new-base-tree/b"
>
Page B
</LinkAccordion>
</div>
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import Form from 'next/form'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
......@@ -12,7 +12,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......@@ -92,6 +92,9 @@ export function ManualPrefetchLinkAccordion({
)
}
type Router = ReturnType<typeof useRouter>
type PrefetchOptions = Parameters<Router['prefetch']>[1]
function ManualPrefetchLink({
href,
children,
......@@ -109,8 +112,8 @@ function ManualPrefetchLink({
let didUnmount = false
const pollPrefetch = () => {
if (!didUnmount) {
// @ts-expect-error: onInvalidate is not yet part of public types
router.prefetch(href, {
kind: 'auto' as PrefetchOptions['kind'],
onInvalidate: pollPrefetch,
})
}
......
......@@ -272,7 +272,7 @@ describe('segment cache (revalidation)', () => {
includes: 'Page B content',
},
// Page A's content should not be prefetched because we're already on that
// page. When prefetching with `prefetch={true}`, we only prefetch the
// page. When prefetching with `prefetch='unstable_forceStale'`, we only prefetch the
// delta between the current route and the target route.
{
includes: 'Page A content',
......
......@@ -19,10 +19,10 @@ export default function SearchParamsPage({
</li>
<li>
<LinkAccordion
prefetch={true}
prefetch="unstable_forceStale"
href="/search-params/target-page?searchParam=b_full"
>
searchParam=b_full, prefetch=true
searchParam=b_full, prefetch="unstable_forceStale"
</LinkAccordion>
</li>
<li>
......@@ -32,10 +32,10 @@ export default function SearchParamsPage({
</li>
<li>
<LinkAccordion
prefetch={true}
prefetch="unstable_forceStale"
href="/search-params/target-page?searchParam=d_full"
>
searchParam=d_full, prefetch=true
searchParam=d_full, prefetch="unstable_forceStale"
</LinkAccordion>
</li>
</ul>
......
'use client'
import Link from 'next/link'
import Link, { type LinkProps } from 'next/link'
import { useState } from 'react'
export function LinkAccordion({
......@@ -10,7 +10,7 @@ export function LinkAccordion({
}: {
href: string
children: React.ReactNode
prefetch?: boolean
prefetch?: LinkProps['prefetch']
}) {
const [isVisible, setIsVisible] = useState(false)
return (
......
......@@ -67,7 +67,7 @@ describe('segment cache (search params)', () => {
expect(await result.innerText()).toBe('Search param: c_PPR')
})
it('when fetching without PPR (e.g. prefetch={true}), includes the search params in the cache key', async () => {
it('when fetching without PPR (e.g. prefetch="unstable_forceStale"), includes the search params in the cache key', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/search-params', {
beforePageLoad(page) {
......@@ -75,7 +75,7 @@ describe('segment cache (search params)', () => {
},
})
// Prefetch a page with search param `b_full`. This link has prefetch={true}
// Prefetch a page with search param `b_full`. This link has prefetch='unstable_forceStale'
// so it will fetch the entire page, including the search param.
const revealB = await browser.elementByCss(
'input[data-link-accordion="/search-params/target-page?searchParam=b_full"]'
......@@ -91,7 +91,7 @@ describe('segment cache (search params)', () => {
)
// Prefetch a link with a different search param, and without
// prefetch={true}. This must fetch a new shell, because it can't use the
// prefetch='unstable_forceStale'. This must fetch a new shell, because it can't use the
// entry we fetched for `searchParam=b_full` (because that one wasn't a
// shell — it included the search param).
const revealA = await browser.elementByCss(
......@@ -106,7 +106,7 @@ describe('segment cache (search params)', () => {
{ includes: 'target-page-with-search-param' }
)
// Prefetch a different link using prefetch={true}. Again, this must issue
// Prefetch a different link using prefetch='unstable_forceStale'. Again, this must issue
// a new request, because it's a full page prefetch and we haven't fetched
// this particular search param before.
// TODO: As an future optimization, if a navigation to this link occurs
......
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