Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset cache integration for nextjs 13.4 #101

Closed
khanghoang1702 opened this issue Sep 26, 2023 · 5 comments
Closed

reset cache integration for nextjs 13.4 #101

khanghoang1702 opened this issue Sep 26, 2023 · 5 comments

Comments

@khanghoang1702
Copy link

Recently, I have a landing page that get all the app contents in the server side of nextjs with one day revalidate config. and I would like an API in nextjs server that can reset the cache and fetch new data whenever I request and the app also reflect the new data. here my integration in the nextjs app. I wonder do I apply the best practices for the case
`
const client = new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(),
link: formatAppContentsResponse.concat(httpLink),
});

await client.resetStore();
await client.query({
query: ExampleDocument,
context: {
fetchOptions: {
next: { revalidate: 1 },
},
},
fetchPolicy: 'no-cache',
});`

@phryneas
Copy link
Member

I'm not exactly sure what you ask here.... where is this code running?

In a server component? In a client component?

@khanghoang1702
Copy link
Author

khanghoang1702 commented Sep 27, 2023

Thank your response, I use this in the server component (I mean App API route) to get the new content of the app after I changed. I also call the same in the app page.tsx get the data contents (which set up the time to revalidate is 1 day). The case here I change the data content, So I force the app revalidate data and reflect it on the app. so I apply this method in the App API route

export async function GET(request: NextRequest) {
  try {
    const token = request.nextUrl.searchParams.get("secret");
    if (token === serverConfig.basic_token) {
      await client.resetStore();
      await client.query({
        query: AppContentsDocument,
        context: {
          fetchOptions: {
            next: { revalidate: 1 },
          },
        },
        fetchPolicy: "no-cache",
      });
      return NextResponse.json({ message: "Revalidated", success: true });
    }
  } catch (err) {
    return NextResponse.json({ message: err, success: false });
  }
}

@phryneas
Copy link
Member

phryneas commented Sep 27, 2023

Please make sure that in a request handler like this, you create a new instance of Apollo Client for every single incoming request - otherwise things like query deduplication could mix up queries of multiple users that are incoming at the same time. Calling resetStore is not enough to prevent things like that.

You could have 20 requests in the same millisecond, and all of them call resetStore at the same time and then start their requests - they would still influence each other. You can't work with a global client here.

That said, if you're not using React SSR, (and this is just a response handler), you also don't need to use the NextSSRApolloClient/NextSSRInMemoryCache. Just do a new ApolloClient(...) inside of that ResponseHandler.

@phryneas
Copy link
Member

I'm doing some housekeeping so I'm closing some older issues that haven't seen activity in a while.
If this is still relevant, please feel free to reopen the issue.

Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants