@@ -6,93 +6,50 @@ import {
66} from '@apollo/experimental-nextjs-app-support/ssr' ;
77import { createErrorLink } from '../utils' ;
88import { PUBLIC_GATEWAY_URL } from '@/config/app' ;
9+ import Cookies from 'js-cookie' ;
910
10- const SEVEN_DAYS = 7 * 24 * 60 * 60 * 1000 ; // 7 days in milliseconds
11- const WOO_SESSION_KEY = 'woo-session' ;
11+ export const WOO_SESSION_KEY = 'woo-session' ;
1212
1313/**
14- * Woo Session setter operation.
15- * If we have a session token in localStorage, add it to the GraphQL request as a Session header.
16- */
17- export const wooSessionSetter = new ApolloLink ( ( operation , forward ) => {
18- /**
19- * If session data exist in local storage, set value as session header.
20- * Here we also delete the session if it is older than 7 days
21- */
22- const localStorageValue = localStorage . getItem ( WOO_SESSION_KEY ) ;
23- const sessionData = localStorageValue ? JSON . parse ( localStorageValue ) : null ;
24-
25- if ( sessionData && sessionData . token && sessionData . createdTime ) {
26- const { token, createdTime } = sessionData ;
27-
28- // Check if the token is older than 7 days
29- if ( Date . now ( ) - createdTime > SEVEN_DAYS ) {
30- // If it is, delete it
31- localStorage . removeItem ( WOO_SESSION_KEY ) ;
32- localStorage . setItem ( 'woocommerce-cart' , JSON . stringify ( { } ) ) ;
33- } else {
34- // If it's not, use the token
35- operation . setContext ( ( ) => ( {
36- headers : {
37- 'woocommerce-session' : `Session ${ token } ` ,
38- } ,
39- } ) ) ;
40- }
41- }
42-
43- return forward ( operation ) ;
44- } ) ;
45-
46- /**
47- * Woo Session updater operation.
48- *
49- * This catches the incoming session token and stores it in localStorage, for future GraphQL requests.
14+ * Apollo Link to update the WooCommerce session token.
15+ * This link captures the session token from the response headers and stores it in cookies.
5016 */
5117export const wooSessionUpdater = new ApolloLink ( ( operation , forward ) =>
5218 forward ( operation ) . map ( ( response ) => {
53- /**
54- * Check for session header and update session in local storage accordingly.
55- */
5619 const context = operation . getContext ( ) ;
57- const {
58- response : { headers } ,
59- } = context ;
60-
61- const session = headers . get ( 'woocommerce-session' ) ;
20+ const session = context . response ?. headers . get ( 'woocommerce-session' ) ;
6221
6322 if ( session ) {
64- if ( 'false' === session ) {
65- // Remove session data if session destroyed.
66- localStorage . removeItem ( WOO_SESSION_KEY ) ;
67- // Update session new data if changed.
68- } else if ( ! localStorage . getItem ( WOO_SESSION_KEY ) ) {
69- localStorage . setItem (
70- WOO_SESSION_KEY ,
71- JSON . stringify ( { token : session , createdTime : Date . now ( ) } ) ,
72- ) ;
23+ if ( session === 'false' ) {
24+ // Remove session data if the session is destroyed
25+ Cookies . remove ( WOO_SESSION_KEY ) ;
26+ } else if ( ! Cookies . get ( WOO_SESSION_KEY ) ) {
27+ // Store new session token if it doesn't exist
28+ Cookies . set ( WOO_SESSION_KEY , session , { expires : 7 } ) ;
7329 }
7430 }
7531
7632 return response ;
7733 } ) ,
7834) ;
7935
36+ // Create an HTTP link for Apollo Client
8037const httpLink = new HttpLink ( {
8138 uri : PUBLIC_GATEWAY_URL ,
8239} ) ;
8340
41+ /**
42+ * Function to create an Apollo Client instance with SSR support.
43+ */
8444export const makeClient = ( ) => {
8545 return new NextSSRApolloClient ( {
8646 ssrMode : true ,
8747 connectToDevTools : true ,
8848 cache : new NextSSRInMemoryCache ( ) ,
8949 link : from ( [
90- wooSessionSetter ,
9150 wooSessionUpdater ,
9251 createErrorLink ( ) ,
93- new SSRMultipartLink ( {
94- stripDefer : true ,
95- } ) ,
52+ new SSRMultipartLink ( { stripDefer : true } ) ,
9653 httpLink ,
9754 ] ) ,
9855 } ) ;
0 commit comments