Skip to content

Commit

Permalink
Test cached form action with revalidate
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Oct 22, 2024
1 parent 475bdb1 commit ac2aa81
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
13 changes: 11 additions & 2 deletions packages/next/src/server/app-render/action-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ export function createServerModuleMap({
{},
{
get: (_, id: string) => {
const workerEntry =
const workers =
serverActionsManifest[
process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node'
][id].workers[normalizeWorkerPageName(pageName)]
][id].workers
let workerEntry = workers[normalizeWorkerPageName(pageName)]

if (workerEntry === undefined) {
// Workaround due to faulty singleton.
for (let key in workers) {
workerEntry = workers[key]
break
}
}

if (typeof workerEntry === 'string') {
return { id: workerEntry, name: id, chunks: [] }
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e/app-dir/use-cache/app/form/page.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<form action={refresh}>
<button id="refresh">Refresh</button>
<p id="t">{Date.now()}</p>
</form>
)
}
27 changes: 26 additions & 1 deletion test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
@@ -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.'
Expand Down Expand Up @@ -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?a`)
const time1 = await browser.waitForElementByCss('#t').text()

await browser.loadPage(new URL(`/form?b`, 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);
})
})

0 comments on commit ac2aa81

Please sign in to comment.