11import { PUBLIC_GATEWAY_URL } from '@/config/app' ;
2- import { ApolloClient , HttpLink , from } from '@apollo/client' ;
2+ import { ApolloClient , ApolloLink , HttpLink , from } from '@apollo/client' ;
33import { setContext } from '@apollo/client/link/context' ;
44import {
55 NextSSRApolloClient ,
@@ -22,7 +22,7 @@ const client = new ApolloClient({
2222 link : from ( [ httpLink ] ) ,
2323} ) ;
2424
25- const createSessionLink = ( ) => {
25+ const sessionCreatorLink = ( ) => {
2626 return setContext ( async ( { context : { headers : currentHeaders } = { } } ) => {
2727 const headers = { ...currentHeaders } ;
2828
@@ -48,13 +48,31 @@ const createSessionLink = () => {
4848 } ) ;
4949} ;
5050
51+ const sessionUpdaterLink = new ApolloLink ( ( operation , forward ) =>
52+ forward ( operation ) . map ( ( response ) => {
53+ const context = operation . getContext ( ) ;
54+ const {
55+ response : { headers } ,
56+ } = context ;
57+
58+ const session = headers . get ( 'woocommerce-session' ) ;
59+
60+ if ( session && ! session . includes ( 'Session' ) ) {
61+ Cookies . set ( WOO_SESSION_KEY , session , { expires : 7 } ) ;
62+ }
63+
64+ return response ;
65+ } ) ,
66+ ) ;
67+
5168export const makeClient = ( ) => {
5269 return new NextSSRApolloClient ( {
5370 ssrMode : true ,
5471 connectToDevTools : true ,
5572 cache : new NextSSRInMemoryCache ( ) ,
5673 link : from ( [
57- createSessionLink ( ) ,
74+ sessionCreatorLink ( ) ,
75+ sessionUpdaterLink ,
5876 createErrorLink ( ) ,
5977 new SSRMultipartLink ( { stripDefer : true } ) ,
6078 httpLink ,
0 commit comments