Skip to content

Commit

Permalink
Fix revalidateTimings route for catchall index route (#65843)
Browse files Browse the repository at this point in the history
fixes #65842

When setting revalidateTimings correctly use toRoute to convert `/index`
to `/`, like done for get
(https://github.com/vercel/next.js/blob/canary/packages/next/src/server/lib/incremental-cache/index.ts#L213)

See #65842 for reproduction codesandbox.

The bug also exists in 14.2.3 (and below) - before #64370

---------

Co-authored-by: JJ Kasper <[email protected]>
  • Loading branch information
nsams and ijjk authored May 20, 2024
1 parent 663488c commit 8a429e0
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ export class IncrementalCache implements IncrementalCacheType {
// Set the value for the revalidate seconds so if it changes we can
// update the cache with the new value.
if (typeof ctx.revalidate !== 'undefined' && !ctx.fetchCache) {
this.revalidateTimings.set(pathname, ctx.revalidate)
this.revalidateTimings.set(toRoute(pathname), ctx.revalidate)
}

await this.cacheHandler?.set(pathname, data, ctx)
Expand Down
6 changes: 6 additions & 0 deletions test/integration/root-catchall-cache/app/[[...slug]]/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const revalidate = 2
export const dynamic = 'error'

export default function Page() {
return <pre id="random">{Math.random()}</pre>
}
12 changes: 12 additions & 0 deletions test/integration/root-catchall-cache/app/layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const metadata = {
title: 'Next.js',
description: 'Generated by Next.js',
}

export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
3 changes: 3 additions & 0 deletions test/integration/root-catchall-cache/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
assetPrefix: '/',
}
63 changes: 63 additions & 0 deletions test/integration/root-catchall-cache/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-env jest */

import { join } from 'path'
import cheerio from 'cheerio'
import {
killApp,
findPort,
nextBuild,
nextStart,
renderViaHTTP,
waitFor,
} from 'next-test-utils'

const appDir = join(__dirname, '../')
let app
let appPort

const getRandom = async (path) => {
const html = await renderViaHTTP(appPort, path)
const $ = cheerio.load(html)
return $('#random').text()
}

const runTests = () => {
it('should cache / correctly', async () => {
const random = await getRandom('/')

{
//cached response (revalidate is 2 seconds)
await waitFor(1000)
const newRandom = await getRandom('/')
expect(random).toBe(newRandom)
}
{
//stale response, triggers revalidate
await waitFor(1000)
const newRandom = await getRandom('/')
expect(random).toBe(newRandom)
}
{
//new response
await waitFor(100)
const newRandom = await getRandom('/')
expect(random).not.toBe(newRandom)
}
})
}

describe('Root Catch-all Cache', () => {
;(process.env.TURBOPACK_DEV ? describe.skip : describe)(
'production mode',
() => {
beforeAll(async () => {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort)
})
afterAll(() => killApp(app))

runTests()
}
)
})

0 comments on commit 8a429e0

Please sign in to comment.