prevent apollo caching on the server

This commit is contained in:
keyan 2022-11-24 13:22:58 -06:00
parent 5746160893
commit de863021f9
4 changed files with 21 additions and 10 deletions

View File

@ -13,7 +13,7 @@ import { getPrice } from '../components/price'
export default async function getSSRApolloClient (req, me = null) { export default async function getSSRApolloClient (req, me = null) {
const session = req && await getSession({ req }) const session = req && await getSession({ req })
return new ApolloClient({ const client = new ApolloClient({
ssrMode: true, ssrMode: true,
link: new SchemaLink({ link: new SchemaLink({
schema: makeExecutableSchema({ schema: makeExecutableSchema({
@ -31,6 +31,8 @@ export default async function getSSRApolloClient (req, me = null) {
}), }),
cache: new InMemoryCache() cache: new InMemoryCache()
}) })
await client.clearStore()
return client
} }
export function getGetServerSideProps (query, variables = null, notFoundFunc, requireVar) { export function getGetServerSideProps (query, variables = null, notFoundFunc, requireVar) {

View File

@ -19,8 +19,19 @@ function isFirstPage (cursor, existingThings) {
} }
export default function getApolloClient () { export default function getApolloClient () {
global.apolloClient ||= new ApolloClient({ if (typeof window === 'undefined') {
link: new HttpLink({ uri: '/api/graphql' }), 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({ cache: new InMemoryCache({
typePolicies: { typePolicies: {
Query: { Query: {
@ -201,6 +212,4 @@ export default function getApolloClient () {
} }
} }
}) })
return global.apolloClient
} }

View File

@ -62,7 +62,7 @@ function MyApp ({ Component, pageProps: { session, ...props } }) {
ssr data ssr data
*/ */
const { apollo, data, me, price } = props const { apollo, data, me, price } = props
if (typeof window !== 'undefined' && apollo && data) { if (apollo && data) {
client.writeQuery({ client.writeQuery({
query: gql`${apollo.query}`, query: gql`${apollo.query}`,
data: data, data: data,

View File

@ -22,12 +22,12 @@ async function work () {
cache: new InMemoryCache(), cache: new InMemoryCache(),
defaultOptions: { defaultOptions: {
watchQuery: { watchQuery: {
fetchPolicy: 'network-only', fetchPolicy: 'no-cache',
nextFetchPolicy: 'network-only' nextFetchPolicy: 'no-cache'
}, },
query: { query: {
fetchPolicy: 'network-only', fetchPolicy: 'no-cache',
nextFetchPolicy: 'network-only' nextFetchPolicy: 'no-cache'
} }
} }
}) })