Commit f0b35f40 authored by Malte Ubl's avatar Malte Ubl

[Experimental] Implement option to opt out of revalidation after setting a…

[Experimental] Implement option to opt out of revalidation after setting a cookie in a server action
parent 8e11bf4a
...@@ -29,8 +29,12 @@ export type { ResponseCookies } ...@@ -29,8 +29,12 @@ export type { ResponseCookies }
export type ReadonlyRequestCookies = Omit< export type ReadonlyRequestCookies = Omit<
RequestCookies, RequestCookies,
'set' | 'clear' | 'delete' 'set' | 'clear' | 'delete'
> & > & {
Pick<ResponseCookies, 'set' | 'delete'> set(name: string, value: string, cookie?: Partial<NextCookieOptions>): this
set(options: NextCookieOptions): this
delete(name: string): this
delete(options: ResponseCookie): this
}
export class RequestCookiesAdapter { export class RequestCookiesAdapter {
public static seal(cookies: RequestCookies): ReadonlyRequestCookies { public static seal(cookies: RequestCookies): ReadonlyRequestCookies {
...@@ -64,9 +68,13 @@ export function getModifiedCookieValues( ...@@ -64,9 +68,13 @@ export function getModifiedCookieValues(
return modified return modified
} }
type NextCookieOptions = ResponseCookie & {
revalidate?: boolean
}
type SetCookieArgs = type SetCookieArgs =
| [key: string, value: string, cookie?: Partial<ResponseCookie>] | [key: string, value: string, cookie?: Partial<NextCookieOptions>]
| [options: ResponseCookie] | [options: NextCookieOptions]
export function appendMutableCookies( export function appendMutableCookies(
headers: Headers, headers: Headers,
...@@ -112,10 +120,10 @@ export class MutableRequestCookiesAdapter { ...@@ -112,10 +120,10 @@ export class MutableRequestCookiesAdapter {
let modifiedValues: ResponseCookie[] = [] let modifiedValues: ResponseCookie[] = []
const modifiedCookies = new Set<string>() const modifiedCookies = new Set<string>()
const updateResponseCookies = () => { const updateResponseCookies = (shouldRevalidate = true) => {
// TODO-APP: change method of getting workStore // TODO-APP: change method of getting workStore
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
if (workStore) { if (workStore && shouldRevalidate) {
workStore.pathWasRevalidated = true workStore.pathWasRevalidated = true
} }
...@@ -159,11 +167,37 @@ export class MutableRequestCookiesAdapter { ...@@ -159,11 +167,37 @@ export class MutableRequestCookiesAdapter {
modifiedCookies.add( modifiedCookies.add(
typeof args[0] === 'string' ? args[0] : args[0].name typeof args[0] === 'string' ? args[0] : args[0].name
) )
// Extract revalidate option and clean args for the underlying set call
let shouldRevalidate = true
let cleanArgs: SetCookieArgs
if (
args.length === 3 &&
args[2] &&
typeof args[2] === 'object' &&
'revalidate' in args[2]
) {
shouldRevalidate = args[2].revalidate !== false
const { revalidate, ...cookieOptions } = args[2]
cleanArgs = [args[0], args[1], cookieOptions] as SetCookieArgs
} else if (
args.length === 1 &&
typeof args[0] === 'object' &&
'revalidate' in args[0]
) {
shouldRevalidate = args[0].revalidate !== false
const { revalidate, ...cookieOptions } = args[0]
cleanArgs = [cookieOptions as ResponseCookie] as SetCookieArgs
} else {
cleanArgs = args
}
try { try {
target.set(...args) target.set(...cleanArgs)
return wrappedCookies return wrappedCookies
} finally { } finally {
updateResponseCookies() updateResponseCookies(shouldRevalidate)
} }
} }
...@@ -192,7 +226,30 @@ export function createCookiesWithMutableAccessCheck( ...@@ -192,7 +226,30 @@ export function createCookiesWithMutableAccessCheck(
case 'set': case 'set':
return function (...args: SetCookieArgs) { return function (...args: SetCookieArgs) {
ensureCookiesAreStillMutable(requestStore, 'cookies().set') ensureCookiesAreStillMutable(requestStore, 'cookies().set')
target.set(...args)
// Extract revalidate option and clean args for the underlying set call
let cleanArgs: SetCookieArgs
if (
args.length === 3 &&
args[2] &&
typeof args[2] === 'object' &&
'revalidate' in args[2]
) {
const { revalidate, ...cookieOptions } = args[2]
cleanArgs = [args[0], args[1], cookieOptions] as SetCookieArgs
} else if (
args.length === 1 &&
typeof args[0] === 'object' &&
'revalidate' in args[0]
) {
const { revalidate, ...cookieOptions } = args[0]
cleanArgs = [cookieOptions as ResponseCookie] as SetCookieArgs
} else {
cleanArgs = args
}
target.set(...cleanArgs)
return wrappedCookies return wrappedCookies
} }
......
/**
* @jest-environment @edge-runtime/jest-environment
*/
import { NextResponse } from 'next/dist/server/web/spec-extension/response'
it('should not revalidate when revalidate: false is set', async () => {
const response = new NextResponse()
// Test setting cookie with revalidate: false
response.cookies.set('test', 'value', { revalidate: false })
// The cookie should still be set correctly
expect(response.cookies.get('test')?.value).toBe('value')
expect(response.cookies.get('test')).toEqual({
name: 'test',
value: 'value',
path: '/',
})
// Check that the Set-Cookie header is present
expect(response.headers.get('set-cookie')).toContain('test=value')
})
it('should accept revalidate option with other cookie options', async () => {
const response = new NextResponse()
// Test setting cookie with multiple options including revalidate: false
response.cookies.set('test', 'value', {
path: '/custom',
maxAge: 3600,
revalidate: false,
})
expect(response.cookies.get('test')).toEqual({
name: 'test',
value: 'value',
path: '/custom',
maxAge: 3600,
})
expect(response.headers.get('set-cookie')).toContain(
'test=value; Path=/custom; Max-Age=3600'
)
})
it('should work with single-argument cookie options including revalidate', async () => {
const response = new NextResponse()
// Test setting cookie with single options object including revalidate: false
response.cookies.set({
name: 'test',
value: 'value',
path: '/single',
revalidate: false,
})
expect(response.cookies.get('test')).toEqual({
name: 'test',
value: 'value',
path: '/single',
})
expect(response.headers.get('set-cookie')).toContain(
'test=value; Path=/single'
)
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment