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

actions: make cookies.set revalidate #49582

Merged
merged 1 commit into from May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,6 +1,7 @@
import type { RequestCookies } from '../cookies'
import type { BaseNextResponse } from '../../../base-http'
import type { ServerResponse } from 'http'
import { StaticGenerationStore } from '../../../../client/components/static-generation-async-storage'

import { ResponseCookies } from '../cookies'
import { ReflectAdapter } from './reflect'
Expand Down Expand Up @@ -61,6 +62,14 @@ export class MutableRequestCookiesAdapter {
let modifiedValues: ResponseCookie[] = []
const modifiedCookies = new Set<string>()
const updateResponseCookies = () => {
// TODO-APP: change method of getting staticGenerationAsyncStore
const staticGenerationAsyncStore = (fetch as any)
.__nextGetStaticStore?.()
?.getStore() as undefined | StaticGenerationStore
if (staticGenerationAsyncStore) {
staticGenerationAsyncStore.pathWasRevalidated = true
}

Comment on lines +65 to +72
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need to make sure actionAsyncStorage.getStore().isAction === true here as cookies().set() can happen inside Route Handlers too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this property doesn't do anything outside of server actions

const allCookies = responseCookes.getAll()
modifiedValues = allCookies.filter((c) => modifiedCookies.has(c.name))
if (res) {
Expand Down
15 changes: 15 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Expand Up @@ -430,6 +430,21 @@ createNextDescribe(
: 'failure'
}, 'success')
})

it('should revalidate when cookies.set is called', async () => {
const browser = await next.browser('/revalidate')
const randomNumber = await browser.elementByCss('#random-cookie').text()

await browser.elementByCss('#set-cookie').click()

await check(async () => {
const newRandomNumber = await browser
.elementByCss('#random-cookie')
.text()

return newRandomNumber !== randomNumber ? 'success' : 'failure'
}, 'success')
})
})
}
)
19 changes: 19 additions & 0 deletions test/e2e/app-dir/actions/app/revalidate/page.js
Expand Up @@ -5,6 +5,8 @@ import {
} from 'next/cache'
import { redirect } from 'next/navigation'

import { cookies } from 'next/headers'

export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?page',
Expand Down Expand Up @@ -49,6 +51,23 @@ export default async function Page() {
revalidate (tags: thankyounext, justputit):{' '}
<span id="justputit">{data2}</span>
</p>
<p>
random cookie:{' '}
<span id="random-cookie">
{JSON.stringify(cookies().get('random'))}
</span>
</p>
<form>
<button
id="set-cookie"
formAction={async () => {
'use server'
cookies().set('random', `${Math.random()}`)
}}
>
set cookie
</button>
</form>
{/* <p>revalidate 10 (tags: thankyounext): {JSON.stringify(cachedData)}</p> */}
<form>
<button
Expand Down