Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change server actions cache default to no-store #60170

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ export async function handleAction({
)
}

// When running actions the default is no-store, you can still `cache: 'force-cache'`
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
staticGenerationStore.fetchCache = 'default-no-store'

const originDomain =
typeof req.headers['origin'] === 'string'
? new URL(req.headers['origin']).host
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,5 +1114,28 @@ createNextDescribe(
})
})
})

it('should use no-store as default for server action', async () => {
const browser = await next.browser('/no-caching-in-actions')
await browser
.waitForElementByCss('#trigger-fetch')
.click()
.waitForElementByCss('#fetched-data')

const getNumber = async () =>
JSON.parse(await browser.elementByCss('#fetched-data').text())

const firstNumber = await getNumber()

await browser.waitForElementByCss('#trigger-fetch').click()

await check(async () => {
const newNumber = await getNumber()
// Expect that the number changes on each click
expect(newNumber).not.toBe(firstNumber)

return 'success'
}, 'success')
})
}
)
9 changes: 9 additions & 0 deletions test/e2e/app-dir/actions/app/no-caching-in-actions/actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use server'

export async function getNumber() {
const res = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?no-caching-actions'
)

return res.text()
}
27 changes: 27 additions & 0 deletions test/e2e/app-dir/actions/app/no-caching-in-actions/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client'
import { useState, useTransition } from 'react'
import { getNumber } from './actions'

export default function Page() {
const [isPending, startTransition] = useTransition()
const [result, setResult] = useState(null)
return (
<>
<h1>No Caching in Actions: {isPending ? 'pending' : 'not pending'}</h1>
{result !== null ? (
<pre id="fetched-data">{JSON.stringify(result)}</pre>
) : null}
<button
id="trigger-fetch"
onClick={() =>
startTransition(async () => {
const result = await getNumber()
setResult(result)
})
}
>
Trigger fetch
</button>
</>
)
}
Loading