Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions examples/magento-graphcms/lib/graphql/graphqlSsrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { MeshApolloLink, getBuiltMesh } from '@graphcommerce/graphql-mesh'
import { storefrontConfig, storefrontConfigDefault } from '@graphcommerce/next-ui'
import type { GetStaticPropsContext } from 'next'
import fs from 'fs'
import { i18nSsrLoader } from '../i18n/I18nProvider'

function client(context: GetStaticPropsContext, fetchPolicy: FetchPolicy = 'no-cache') {
Expand Down Expand Up @@ -53,7 +54,7 @@ export function graphqlSharedClient(context: GetStaticPropsContext) {
}

const ssrClient: {
[locale: string]: ApolloClient
[locale: string]: { instancedAt: number; client: ApolloClient }
} = {}

export function graphqlSsrClient(context: GetStaticPropsContext) {
Expand All @@ -62,8 +63,24 @@ export function graphqlSsrClient(context: GetStaticPropsContext) {

if (context.preview || context.draftMode) return client(context, 'no-cache')

const dir = './tmp'

const shouldCheckCache = fs.existsSync(`${dir}/renew-all-pages-query.txt`)

if (shouldCheckCache) {
const instancedAt = Number(fs.readFileSync(`${dir}/renew-all-pages-query.txt`, 'utf8'))

if (ssrClient[locale]?.instancedAt < instancedAt) {
delete ssrClient[locale]
}
}

// Create a client if it doesn't exist for the locale.
if (!ssrClient[locale]) ssrClient[locale] = client(context, 'no-cache')
if (!ssrClient[locale])
ssrClient[locale] = {
instancedAt: new Date().getTime(),
client: client(context, 'no-cache'),
}

return ssrClient[locale]
return ssrClient[locale].client
}
Loading