11/* eslint-disable turbo/no-undeclared-env-vars */
22import { beforeEach , describe , expect , it } from 'vitest' ;
33
4+ import { getCookieSuffix } from '../keys' ;
45import { CLERK_NETLIFY_CACHE_BUST_PARAM , handleNetlifyCacheHeaders } from '../netlifyCacheHandler' ;
56
67const mockDevPublishableKey = 'pk_test_YW55LW9mZabcZS1wYWdlX3BhZ2VfcG9pbnRlci1pZF90ZXN0XzE' ;
@@ -14,86 +15,98 @@ describe('handleNetlifyCacheHeaders', () => {
1415 } ) ;
1516
1617 describe ( 'Netlify-Vary header' , ( ) => {
17- it ( 'should set Netlify-Vary header when on Netlify' , ( ) => {
18+ it ( 'should set Netlify-Vary header with unsuffixed and suffixed cookie names when on Netlify' , async ( ) => {
1819 process . env . NETLIFY = 'true' ;
1920
2021 const headers = new Headers ( ) ;
21- handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
22+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
2223
23- expect ( headers . get ( 'Netlify-Vary' ) ) . toBe ( 'cookie=__client_uat,cookie=__session' ) ;
24+ const suffix = await getCookieSuffix ( mockProdPublishableKey ) ;
25+ expect ( headers . get ( 'Netlify-Vary' ) ) . toBe (
26+ `cookie=__client_uat,cookie=__session,cookie=__client_uat_${ suffix } ,cookie=__session_${ suffix } ` ,
27+ ) ;
2428 } ) ;
2529
26- it ( 'should not set Netlify-Vary header when not on Netlify' , ( ) => {
30+ it ( 'should not set Netlify-Vary header when not on Netlify' , async ( ) => {
2731 const headers = new Headers ( ) ;
28- handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
32+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
2933
3034 expect ( headers . get ( 'Netlify-Vary' ) ) . toBeNull ( ) ;
3135 } ) ;
3236
33- it ( 'should detect Netlify via NETLIFY_FUNCTIONS_TOKEN' , ( ) => {
37+ it ( 'should detect Netlify via NETLIFY_FUNCTIONS_TOKEN' , async ( ) => {
3438 process . env . NETLIFY_FUNCTIONS_TOKEN = 'some-token' ;
3539
3640 const headers = new Headers ( ) ;
37- handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
41+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
3842
39- expect ( headers . get ( 'Netlify-Vary' ) ) . toBe ( 'cookie=__client_uat,cookie=__session ' ) ;
43+ expect ( headers . get ( 'Netlify-Vary' ) ) . toContain ( 'cookie=__client_uat' ) ;
4044 } ) ;
4145
42- it ( 'should detect Netlify via URL ending with netlify.app' , ( ) => {
46+ it ( 'should detect Netlify via URL ending with netlify.app' , async ( ) => {
4347 process . env . URL = 'https://example.netlify.app' ;
4448
4549 const headers = new Headers ( ) ;
46- handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
50+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
51+
52+ expect ( headers . get ( 'Netlify-Vary' ) ) . toContain ( 'cookie=__client_uat' ) ;
53+ } ) ;
54+
55+ it ( 'should fall back to unsuffixed cookies only when publishableKey is empty' , async ( ) => {
56+ process . env . NETLIFY = 'true' ;
57+
58+ const headers = new Headers ( ) ;
59+ await handleNetlifyCacheHeaders ( { headers, publishableKey : '' } ) ;
4760
4861 expect ( headers . get ( 'Netlify-Vary' ) ) . toBe ( 'cookie=__client_uat,cookie=__session' ) ;
4962 } ) ;
5063 } ) ;
5164
5265 describe ( 'cache bust parameter (dev instances)' , ( ) => {
53- it ( 'should add cache bust parameter when on Netlify and in development with Location header' , ( ) => {
66+ it ( 'should add cache bust parameter when on Netlify and in development with Location header' , async ( ) => {
5467 process . env . NETLIFY = 'true' ;
5568
5669 const headers = new Headers ( { Location : 'https://example.netlify.app' } ) ;
57- handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
70+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
5871
5972 const locationUrl = new URL ( headers . get ( 'Location' ) || '' ) ;
6073 expect ( locationUrl . searchParams . has ( CLERK_NETLIFY_CACHE_BUST_PARAM ) ) . toBe ( true ) ;
6174 } ) ;
6275
63- it ( 'should not add cache bust parameter for production instances' , ( ) => {
76+ it ( 'should not add cache bust parameter for production instances' , async ( ) => {
6477 process . env . NETLIFY = 'true' ;
6578
6679 const headers = new Headers ( { Location : 'https://example.netlify.app' } ) ;
67- handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
80+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockProdPublishableKey } ) ;
6881
6982 const locationUrl = new URL ( headers . get ( 'Location' ) || '' ) ;
7083 expect ( locationUrl . searchParams . has ( CLERK_NETLIFY_CACHE_BUST_PARAM ) ) . toBe ( false ) ;
7184 } ) ;
7285
73- it ( 'should not modify the Location header if it has the handshake param' , ( ) => {
86+ it ( 'should not modify the Location header if it has the handshake param' , async ( ) => {
7487 process . env . NETLIFY = 'true' ;
7588
7689 const locationValue = 'https://example.netlify.app/redirect?__clerk_handshake=' ;
7790 const headers = new Headers ( { Location : locationValue } ) ;
78- handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
91+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
7992
8093 expect ( headers . get ( 'Location' ) ) . toBe ( locationValue ) ;
8194 } ) ;
8295
83- it ( 'should not modify the Location header if not on Netlify' , ( ) => {
96+ it ( 'should not modify the Location header if not on Netlify' , async ( ) => {
8497 const headers = new Headers ( { Location : 'https://example.com' } ) ;
85- handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
98+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
8699
87100 expect ( headers . get ( 'Location' ) ) . toBe ( 'https://example.com' ) ;
88101 } ) ;
89102
90- it ( 'should ignore the URL environment variable if it is not a string' , ( ) => {
103+ it ( 'should ignore the URL environment variable if it is not a string' , async ( ) => {
91104 // @ts -expect-error - Random object
92105 process . env . URL = { } ;
93106 process . env . NETLIFY = 'true' ;
94107
95108 const headers = new Headers ( { Location : 'https://example.netlify.app' } ) ;
96- handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
109+ await handleNetlifyCacheHeaders ( { headers, publishableKey : mockDevPublishableKey } ) ;
97110
98111 const locationUrl = new URL ( headers . get ( 'Location' ) || '' ) ;
99112 expect ( locationUrl . searchParams . has ( CLERK_NETLIFY_CACHE_BUST_PARAM ) ) . toBe ( true ) ;
0 commit comments