diff --git a/packages/next/src/server/lib/incremental-cache/index.ts b/packages/next/src/server/lib/incremental-cache/index.ts index 81df6ff31065a..3e5e518b302d1 100644 --- a/packages/next/src/server/lib/incremental-cache/index.ts +++ b/packages/next/src/server/lib/incremental-cache/index.ts @@ -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) diff --git a/test/integration/root-catchall-cache/app/[[...slug]]/page.js b/test/integration/root-catchall-cache/app/[[...slug]]/page.js new file mode 100644 index 0000000000000..cc06fead52960 --- /dev/null +++ b/test/integration/root-catchall-cache/app/[[...slug]]/page.js @@ -0,0 +1,6 @@ +export const revalidate = 2 +export const dynamic = 'error' + +export default function Page() { + return
{Math.random()}
+} diff --git a/test/integration/root-catchall-cache/app/layout.js b/test/integration/root-catchall-cache/app/layout.js new file mode 100644 index 0000000000000..8525f5f8c0b2a --- /dev/null +++ b/test/integration/root-catchall-cache/app/layout.js @@ -0,0 +1,12 @@ +export const metadata = { + title: 'Next.js', + description: 'Generated by Next.js', +} + +export default function RootLayout({ children }) { + return ( + + {children} + + ) +} diff --git a/test/integration/root-catchall-cache/next.config.js b/test/integration/root-catchall-cache/next.config.js new file mode 100644 index 0000000000000..89a39aed7bdb3 --- /dev/null +++ b/test/integration/root-catchall-cache/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + assetPrefix: '/', +} diff --git a/test/integration/root-catchall-cache/test/index.test.js b/test/integration/root-catchall-cache/test/index.test.js new file mode 100644 index 0000000000000..68cc8629b2ca6 --- /dev/null +++ b/test/integration/root-catchall-cache/test/index.test.js @@ -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() + } + ) +})