11import { removeEdgesAndNodes } from '@bigcommerce/catalyst-client' ;
2+ import { unstable_cache } from 'next/cache' ;
23import { setRequestLocale } from 'next-intl/server' ;
34import { cache } from 'react' ;
45
@@ -45,34 +46,43 @@ interface PageLink {
4546 href : string ;
4647}
4748
48- const getWebPageChildren = cache ( async ( id : string ) : Promise < PageLink [ ] > => {
49- const { data } = await client . fetch ( {
50- document : WebPageChildrenQuery ,
51- variables : { id : decodeURIComponent ( id ) } ,
52- fetchOptions : { next : { revalidate } } ,
53- } ) ;
49+ const getCachedWebPageChildren = unstable_cache (
50+ async ( locale : string , id : string ) : Promise < PageLink [ ] > => {
51+ const { data } = await client . fetch ( {
52+ document : WebPageChildrenQuery ,
53+ variables : { id : decodeURIComponent ( id ) } ,
54+ locale,
55+ fetchOptions : { cache : 'no-store' } ,
56+ } ) ;
5457
55- if ( ! data . node ) {
56- return [ ] ;
57- }
58+ if ( ! data . node ) {
59+ return [ ] ;
60+ }
5861
59- if ( ! ( 'children' in data . node ) ) {
60- return [ ] ;
61- }
62+ if ( ! ( 'children' in data . node ) ) {
63+ return [ ] ;
64+ }
6265
63- const { children } = data . node ;
66+ const { children } = data . node ;
6467
65- return removeEdgesAndNodes ( children ) . reduce ( ( acc : PageLink [ ] , child ) => {
66- if ( 'path' in child ) {
67- return [ ...acc , { label : child . name , href : child . path } ] ;
68- }
68+ return removeEdgesAndNodes ( children ) . reduce ( ( acc : PageLink [ ] , child ) => {
69+ if ( 'path' in child ) {
70+ return [ ...acc , { label : child . name , href : child . path } ] ;
71+ }
6972
70- if ( 'link' in child ) {
71- return [ ...acc , { label : child . name , href : child . link } ] ;
72- }
73+ if ( 'link' in child ) {
74+ return [ ...acc , { label : child . name , href : child . link } ] ;
75+ }
76+
77+ return acc ;
78+ } , [ ] ) ;
79+ } ,
80+ [ 'get-webpage-children' ] ,
81+ { revalidate } ,
82+ ) ;
7383
74- return acc ;
75- } , [ ] ) ;
84+ const getWebPageChildren = cache ( async ( locale : string , id : string ) : Promise < PageLink [ ] > => {
85+ return getCachedWebPageChildren ( locale , id ) ;
7686} ) ;
7787
7888export default async function WebPageLayout ( { params, children } : Props ) {
@@ -82,7 +92,7 @@ export default async function WebPageLayout({ params, children }: Props) {
8292
8393 return (
8494 < StickySidebarLayout
85- sidebar = { < SidebarMenu links = { getWebPageChildren ( id ) } /> }
95+ sidebar = { < SidebarMenu links = { getWebPageChildren ( locale , id ) } /> }
8696 sidebarSize = "small"
8797 >
8898 { children }
0 commit comments