Skip to content

Commit

Permalink
ensure router cache updates reference the latest cache values
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Jun 9, 2024
1 parent 3c99b8c commit e00e47d
Show file tree
Hide file tree
Showing 23 changed files with 168 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function navigateReducer_noPPR(
updatedCanonicalUrl.split('#', 1)[0]

let currentTree = state.tree
const currentCache = state.cache
let currentCache = state.cache
let scrollableSegments: FlightSegmentPath[] = []
for (const flightDataPath of flightData) {
const flightSegmentPath = flightDataPath.slice(
Expand Down Expand Up @@ -258,6 +258,9 @@ function navigateReducer_noPPR(
mutable.cache = cache
} else if (applied) {
mutable.cache = cache
// If we applied the cache, we update the "current cache" value so any other
// segments in the FlightDataPath will be able to reference the updated cache.
currentCache = cache
}

currentTree = newTree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
fastForwardTo,
getPathname,
} from './test-utils'
import path from 'path'

describe('app dir client cache semantics (default semantics)', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
files: path.join(__dirname, 'fixture', 'regular'),
})

if (isNextDev) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { nextTestSetup } from 'e2e-utils'
import { browserConfigWithFixedTime, fastForwardTo } from './test-utils'
import { findAllTelemetryEvents } from 'next-test-utils'
import path from 'path'

describe('app dir client cache semantics (experimental staleTimes)', () => {
describe('dynamic: 0', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
files: path.join(__dirname, 'fixture', 'regular'),
nextConfig: {
experimental: { staleTimes: { dynamic: 0 } },
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
fastForwardTo,
getPathname,
} from './test-utils'
import path from 'path'

// This preserves existing tests for the 30s/5min heuristic (previous router defaults)
describe('app dir client cache semantics (30s/5min)', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
files: path.join(__dirname, 'fixture', 'regular'),
nextConfig: {
experimental: { staleTimes: { dynamic: 30, static: 180 } },
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils'
import { BrowserInterface } from 'next-webdriver'
import {
browserConfigWithFixedTime,
createRequestsListener,
fastForwardTo,
getPathname,
} from './test-utils'
import path from 'path'

describe('app dir client cache with parallel routes', () => {
const { next } = nextTestSetup({
files: path.join(__dirname, 'fixtures', 'parallel-routes'),
})

describe('prefetch={true}', () => {
let browser: BrowserInterface

beforeEach(async () => {
browser = (await next.browser(
'/',
browserConfigWithFixedTime
)) as BrowserInterface
})

it('should prefetch the full page', async () => {
const { getRequests, clearRequests } =
await createRequestsListener(browser)
await check(() => {
return getRequests().some(
([url, didPartialPrefetch]) =>
getPathname(url) === '/0' && !didPartialPrefetch
)
? 'success'
: 'fail'
}, 'success')

clearRequests()

await browser
.elementByCss('[href="/0"]')
.click()
.waitForElementByCss('#random-number')

expect(getRequests().every(([url]) => getPathname(url) !== '/0')).toEqual(
true
)
})

it('should re-use the cache for the full page, only for 5 mins', async () => {
const randomNumber = await browser
.elementByCss('[href="/0"]')
.click()
.waitForElementByCss('#random-number')
.text()

await browser.elementByCss('[href="/"]').click()

const number = await browser
.elementByCss('[href="/0"]')
.click()
.waitForElementByCss('#random-number')
.text()

expect(number).toBe(randomNumber)

await browser.eval(fastForwardTo, 5 * 60 * 1000)

await browser.elementByCss('[href="/"]').click()

const newNumber = await browser
.elementByCss('[href="/0"]')
.click()
.waitForElementByCss('#random-number')
.text()

expect(newNumber).not.toBe(randomNumber)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Page({ params }) {
return (
<div>
Catchall <pre>{JSON.stringify(params)}</pre>{' '}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>Root Breadcrumb</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Link from 'next/link'

export default async function Page() {
const randomNumber = await new Promise((resolve) => {
setTimeout(() => {
resolve(Math.random())
}, 1000)
})

return (
<>
<div>
<Link href="/">Back to Home</Link>
</div>
<div id="random-number">{randomNumber}</div>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function Root({ children, breadcrumbs }) {
return (
<html>
<head></head>
<body>
<div>{breadcrumbs}</div>
<div id="root-layout">Root Layout</div>
<div>{children}</div>
</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Link from 'next/link'

export default function Page() {
return (
<div>
<Link href="/0" prefetch={true}>
To Dynamic Page
</Link>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use client'

export function ClientComponent() {
return <div>Client Component</div>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Link from 'next/link'
import { ClientComponent } from './client'

export default async function Page({ searchParams: { timeout } }) {
const randomNumber = await new Promise((resolve) => {
Expand All @@ -16,6 +17,7 @@ export default async function Page({ searchParams: { timeout } }) {
<Link href="/"> Back to Home </Link>
</div>
<div id="random-number">{randomNumber}</div>
<ClientComponent />
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Page({ params }) {
return (
<div>
Catchall <pre>{JSON.stringify(params)}</pre>{' '}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page({ params }) {
return <div>Root Breadcrumb</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function Page({ children, breadcrumbs }) {
return (
<>
<div>{breadcrumbs}</div>
<div>{children}</div>
</>
)
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-client-cache/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = { productionBrowserSourceMaps: true }

0 comments on commit e00e47d

Please sign in to comment.