From a422518c119126540c1582100d01ffc3dd074b0b Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Sat, 19 Oct 2024 23:15:12 -0400 Subject: [PATCH] Test cached form action with revalidate --- .../src/server/use-cache/use-cache-wrapper.ts | 2 +- test/e2e/app-dir/use-cache/app/form/page.tsx | 18 +++++++++++++ test/e2e/app-dir/use-cache/use-cache.test.ts | 27 ++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 test/e2e/app-dir/use-cache/app/form/page.tsx diff --git a/packages/next/src/server/use-cache/use-cache-wrapper.ts b/packages/next/src/server/use-cache/use-cache-wrapper.ts index 94864d188f7e01..9597aaf1bc0b4d 100644 --- a/packages/next/src/server/use-cache/use-cache-wrapper.ts +++ b/packages/next/src/server/use-cache/use-cache-wrapper.ts @@ -679,7 +679,7 @@ export function cache(kind: string, id: string, fn: any) { moduleMap: isEdgeRuntime ? clientReferenceManifest.edgeRscModuleMapping : clientReferenceManifest.rscModuleMapping, - serverModuleMap: null, + serverModuleMap: getServerModuleMap(), } return createFromReadableStream(stream, { diff --git a/test/e2e/app-dir/use-cache/app/form/page.tsx b/test/e2e/app-dir/use-cache/app/form/page.tsx new file mode 100644 index 00000000000000..e8482a528755cd --- /dev/null +++ b/test/e2e/app-dir/use-cache/app/form/page.tsx @@ -0,0 +1,18 @@ +import { revalidateTag, unstable_cacheTag as cacheTag } from 'next/cache' + +async function refresh() { + 'use server' + revalidateTag('home') +} + +export default async function Page() { + 'use cache' + cacheTag('home') + + return ( +
+ +

{Date.now()}

+
+ ) +} diff --git a/test/e2e/app-dir/use-cache/use-cache.test.ts b/test/e2e/app-dir/use-cache/use-cache.test.ts index d327d1e5e275f1..4fba7fa0b646ab 100644 --- a/test/e2e/app-dir/use-cache/use-cache.test.ts +++ b/test/e2e/app-dir/use-cache/use-cache.test.ts @@ -1,5 +1,5 @@ import { nextTestSetup } from 'e2e-utils' -import { retry } from 'next-test-utils' +import { retry, waitFor } from 'next-test-utils' const GENERIC_RSC_ERROR = 'An error occurred in the Server Components render. The specific message is omitted in production builds to avoid leaking sensitive details. A digest property is included on this error instance which may provide additional details about the nature of the error.' @@ -231,4 +231,29 @@ describe('use-cache', () => { expect(meta.headers['x-next-cache-tags']).toContain('a,c,b') }) } + + it('should be able to revalidate a page using', async () => { + const browser = await next.browser(`/form`) + const time1 = await browser.waitForElementByCss('#t').text() + + await browser.loadPage(new URL(`/form`, next.url).toString()) + + const time2 = await browser.waitForElementByCss('#t').text() + + expect(time1).toBe(time2) + + await browser.elementByCss('#refresh').click() + + await waitFor(500) + + const time3 = await browser.waitForElementByCss('#t').text() + + expect(time3).not.toBe(time2) + + // Reloading again should ideally be the same value but because the Action seeds + // the cache with real params as the argument it has a different cache key. + // await browser.loadPage(new URL(`/form?c`, next.url).toString()) + // const time4 = await browser.waitForElementByCss('#t').text() + // expect(time4).toBe(time3); + }) })