Skip to content

Commit

Permalink
actions: make cookies.set revalidate (#49582)
Browse files Browse the repository at this point in the history
This PR adds revalidation for the current page you're on when you call
cookies.set.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

link NEXT-1097
  • Loading branch information
feedthejim committed May 10, 2023
1 parent 958e34e commit 824c7fd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
}

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 824c7fd

Please sign in to comment.