From de863021f9c96ba407d3f124f9855c5d20707fe7 Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 24 Nov 2022 13:22:58 -0600 Subject: [PATCH] prevent apollo caching on the server --- api/ssrApollo.js | 4 +++- lib/apollo.js | 17 +++++++++++++---- pages/_app.js | 2 +- worker/index.js | 8 ++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/api/ssrApollo.js b/api/ssrApollo.js index 3931c76d..92f50701 100644 --- a/api/ssrApollo.js +++ b/api/ssrApollo.js @@ -13,7 +13,7 @@ import { getPrice } from '../components/price' export default async function getSSRApolloClient (req, me = null) { const session = req && await getSession({ req }) - return new ApolloClient({ + const client = new ApolloClient({ ssrMode: true, link: new SchemaLink({ schema: makeExecutableSchema({ @@ -31,6 +31,8 @@ export default async function getSSRApolloClient (req, me = null) { }), cache: new InMemoryCache() }) + await client.clearStore() + return client } export function getGetServerSideProps (query, variables = null, notFoundFunc, requireVar) { diff --git a/lib/apollo.js b/lib/apollo.js index 291e3d2f..9d22b3e7 100644 --- a/lib/apollo.js +++ b/lib/apollo.js @@ -19,8 +19,19 @@ function isFirstPage (cursor, existingThings) { } export default function getApolloClient () { - global.apolloClient ||= new ApolloClient({ - link: new HttpLink({ uri: '/api/graphql' }), + if (typeof window === 'undefined') { + const client = getClient(`${process.env.SELF_URL}/api/graphql`) + client.clearStore() + return client + } else { + global.apolloClient ||= getClient('/api/graphql') + return global.apolloClient + } +} + +function getClient (uri) { + return new ApolloClient({ + link: new HttpLink({ uri }), cache: new InMemoryCache({ typePolicies: { Query: { @@ -201,6 +212,4 @@ export default function getApolloClient () { } } }) - - return global.apolloClient } diff --git a/pages/_app.js b/pages/_app.js index e9a3c6d7..dee988af 100644 --- a/pages/_app.js +++ b/pages/_app.js @@ -62,7 +62,7 @@ function MyApp ({ Component, pageProps: { session, ...props } }) { ssr data */ const { apollo, data, me, price } = props - if (typeof window !== 'undefined' && apollo && data) { + if (apollo && data) { client.writeQuery({ query: gql`${apollo.query}`, data: data, diff --git a/worker/index.js b/worker/index.js index 3399acd5..238448ad 100644 --- a/worker/index.js +++ b/worker/index.js @@ -22,12 +22,12 @@ async function work () { cache: new InMemoryCache(), defaultOptions: { watchQuery: { - fetchPolicy: 'network-only', - nextFetchPolicy: 'network-only' + fetchPolicy: 'no-cache', + nextFetchPolicy: 'no-cache' }, query: { - fetchPolicy: 'network-only', - nextFetchPolicy: 'network-only' + fetchPolicy: 'no-cache', + nextFetchPolicy: 'no-cache' } } })