1+ import { unstable_cache } from 'next/cache' ;
12import { getLocale , getTranslations } from 'next-intl/server' ;
23import { cache } from 'react' ;
34
@@ -47,34 +48,64 @@ const getCartCount = cache(async (cartId: string, customerAccessToken?: string)
4748 return response . data . site . cart ?. lineItems . totalQuantity ?? null ;
4849} ) ;
4950
50- const getHeaderLinks = cache ( async ( customerAccessToken ?: string , currencyCode ?: CurrencyCode ) => {
51- const { data : response } = await client . fetch ( {
52- document : GetLinksAndSectionsQuery ,
53- customerAccessToken,
54- variables : { currencyCode } ,
55- // Since this query is needed on every page, it's a good idea not to validate the customer access token.
56- // The 'cache' function also caches errors, so we might get caught in a redirect loop if the cache saves an invalid token error response.
57- validateCustomerAccessToken : false ,
58- fetchOptions : customerAccessToken ? { cache : 'no-store' } : { next : { revalidate } } ,
59- } ) ;
60-
61- return readFragment ( HeaderLinksFragment , response ) . site ;
62- } ) ;
63-
64- const getHeaderData = cache ( async ( ) => {
65- const { data : response } = await client . fetch ( {
66- document : LayoutQuery ,
67- fetchOptions : { next : { revalidate } } ,
68- } ) ;
51+ const getCachedHeaderLinks = unstable_cache (
52+ async ( locale : string , currencyCode ?: CurrencyCode ) => {
53+ const { data : response } = await client . fetch ( {
54+ document : GetLinksAndSectionsQuery ,
55+ variables : { currencyCode } ,
56+ locale,
57+ validateCustomerAccessToken : false ,
58+ fetchOptions : { cache : 'no-store' } ,
59+ } ) ;
60+
61+ return readFragment ( HeaderLinksFragment , response ) . site ;
62+ } ,
63+ [ 'header-links' ] ,
64+ { revalidate } ,
65+ ) ;
66+
67+ const getHeaderLinks = cache (
68+ async ( locale : string , customerAccessToken ?: string , currencyCode ?: CurrencyCode ) => {
69+ if ( customerAccessToken ) {
70+ const { data : response } = await client . fetch ( {
71+ document : GetLinksAndSectionsQuery ,
72+ customerAccessToken,
73+ variables : { currencyCode } ,
74+ locale,
75+ validateCustomerAccessToken : false ,
76+ fetchOptions : { cache : 'no-store' } ,
77+ } ) ;
78+
79+ return readFragment ( HeaderLinksFragment , response ) . site ;
80+ }
6981
70- return readFragment ( HeaderFragment , response ) . site ;
82+ return getCachedHeaderLinks ( locale , currencyCode ) ;
83+ } ,
84+ ) ;
85+
86+ const getCachedHeaderData = unstable_cache (
87+ async ( locale : string ) => {
88+ const { data : response } = await client . fetch ( {
89+ document : LayoutQuery ,
90+ locale,
91+ fetchOptions : { cache : 'no-store' } ,
92+ } ) ;
93+
94+ return readFragment ( HeaderFragment , response ) . site ;
95+ } ,
96+ [ 'header-data' ] ,
97+ { revalidate } ,
98+ ) ;
99+
100+ const getHeaderData = cache ( async ( locale : string ) => {
101+ return getCachedHeaderData ( locale ) ;
71102} ) ;
72103
73104export const Header = async ( ) => {
74105 const t = await getTranslations ( 'Components.Header' ) ;
75106 const locale = await getLocale ( ) ;
76107
77- const data = await getHeaderData ( ) ;
108+ const data = await getHeaderData ( locale ) ;
78109
79110 const logo = data . settings ? logoTransformer ( data . settings ) : '' ;
80111
@@ -101,7 +132,8 @@ export const Header = async () => {
101132 ] ) ;
102133 // const customerAccessToken = await getSessionCustomerAccessToken();
103134 // const currencyCode = await getPreferredCurrencyCode();
104- const categoryTree = ( await getHeaderLinks ( customerAccessToken , currencyCode ) ) . categoryTree ;
135+ const headerLinks = await getHeaderLinks ( locale , customerAccessToken , currencyCode ) ;
136+ const categoryTree = headerLinks . categoryTree ;
105137
106138 /** To prevent the navigation menu from overflowing, we limit the number of categories to 6.
107139 To show a full list of categories, modify the `slice` method to remove the limit.
@@ -128,8 +160,8 @@ export const Header = async () => {
128160 getSessionCustomerAccessToken ( ) ,
129161 getPreferredCurrencyCode ( ) ,
130162 ] ) ;
131- const giftCertificateSettings = ( await getHeaderLinks ( customerAccessToken , currencyCode ) )
132- . settings ?. giftCertificates ;
163+ const headerLinksData = await getHeaderLinks ( locale , customerAccessToken , currencyCode ) ;
164+ const giftCertificateSettings = headerLinksData . settings ?. giftCertificates ;
133165
134166 return giftCertificateSettings ?. isEnabled ?? false ;
135167 } ) ;
0 commit comments