Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Mar 6, 2024
1 parent 04ee80b commit 1413c39
Show file tree
Hide file tree
Showing 9 changed files with 517 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,12 @@ export function getOrCreatePrefetchCacheEntry({
existingCacheEntry.kind !== PrefetchKind.FULL &&
kind === PrefetchKind.FULL

const hasReusableLoadingState =
// If staletime is 0, we'd be throwing away the prefetch entry every navigation.
// This means we'd never get a chance to re-use the previous loading state, de-opting out of instant navigations.
PREFETCH_STALE_TIME === 0 &&
(existingCacheEntry.loadingStatus === PrefetchCacheEntryStatus.fresh ||
existingCacheEntry.loadingStatus === PrefetchCacheEntryStatus.reusable)
const hasReusableData =
const hasReusablePrefetch =
existingCacheEntry.status === PrefetchCacheEntryStatus.reusable ||
existingCacheEntry.status === PrefetchCacheEntryStatus.fresh

// we'll let the router use the existing prefetch entry if anything can be reused (loading state, or the data itself)
// we'll let the router use the existing prefetch entry if anything can be reused
// otherwise we will fetch fresh data from the server and update the cache entry
const hasReusablePrefetch = hasReusableLoadingState || hasReusableData

if (switchedToFullPrefetch || !hasReusablePrefetch) {
return createLazyPrefetchEntry({
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/app-dir/app-client-cache/app/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export default function HomePage() {
To Random Number - prefetch: true
</Link>
</div>
<div>
<Link href="/0?timeout=1000" prefetch={true}>
To Random Number - prefetch: true, slow
</Link>
</div>
<div>
<Link href="/1">To Random Number - prefetch: auto</Link>
</div>
Expand All @@ -15,6 +20,11 @@ export default function HomePage() {
To Random Number 2 - prefetch: false
</Link>
</div>
<div>
<Link href="/2?timeout=1000" prefetch={false}>
To Random Number 2 - prefetch: false, slow
</Link>
</div>
<div>
<Link href="/1?timeout=1000">
To Random Number - prefetch: auto, slow
Expand Down
21 changes: 21 additions & 0 deletions test/e2e/app-dir/app-client-cache/app/without-loading/[id]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Link from 'next/link'

export default async function Page({ searchParams: { timeout } }) {
const randomNumber = await new Promise((resolve) => {
setTimeout(
() => {
resolve(Math.random())
},
timeout !== undefined ? Number.parseInt(timeout, 10) : 0
)
})

return (
<>
<div>
<Link href="/without-loading">Back to Home</Link>
</div>
<div id="random-number">{randomNumber}</div>
</>
)
}
36 changes: 36 additions & 0 deletions test/e2e/app-dir/app-client-cache/app/without-loading/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Link from 'next/link'

export default function Page() {
return (
<>
<div>
<Link href="/without-loading/0?timeout=0" prefetch={true}>
To Random Number - prefetch: true
</Link>
</div>
<div>
<Link href="/without-loading/0?timeout=1000" prefetch={true}>
To Random Number - prefetch: true, slow
</Link>
</div>
<div>
<Link href="/without-loading/1">To Random Number - prefetch: auto</Link>
</div>
<div>
<Link href="/without-loading/2" prefetch={false}>
To Random Number 2 - prefetch: false
</Link>
</div>
<div>
<Link href="/without-loading/2?timeout=1000" prefetch={false}>
To Random Number 2 - prefetch: false, slow
</Link>
</div>
<div>
<Link href="/without-loading/1?timeout=1000">
To Random Number - prefetch: auto, slow
</Link>
</div>
</>
)
}

0 comments on commit 1413c39

Please sign in to comment.