ApolloClient used in SSR cannot set cookies ?
Unanswered
Daggertooth pike conger posted this in #help-forum
Daggertooth pike congerOP
Hello, i'm using appolloClient on my app and i use it on my server component also, with a particuliar config.
Problem is, i have an errorLink that is triggered when the original fetch sends an error. It works nice, the error is catched, but then inside my handleError i'm doing some logic that set client cookies with the cookies() from 'next/headers', and there a wild error appears : 'Cookies can only be modified in a server action or route handler'. But everything is used server side, every where when i console.log i got it on my Next console, i added "use server" everywhere it doesn't change anything.
Second problem is, we just implemented AppolloClient, but previously we did it with fetch() and a fetchWrapper to catch the error and then re-set the cookies and it worked well ? I cannot figure out why it doesn't work here. I also tried to put my logic in a route handler but still doesn't works.
Here is the handleError() :
and the main logic is in the renewAuthTokensFromServer(), where i do set the new cookies.
Thank you !
export function createApolloClientServer() {
return new ApolloClient({
ssrMode: true,
link: authLinkServer.concat(errorLinkServer).concat(httpLinkServer),
cache: new InMemoryCache(),
defaultOptions: {
watchQuery: {
fetchPolicy: "network-only",
errorPolicy: "all",
},
query: {
fetchPolicy: "network-only",
errorPolicy: "all",
},
},
});
}
Problem is, i have an errorLink that is triggered when the original fetch sends an error. It works nice, the error is catched, but then inside my handleError i'm doing some logic that set client cookies with the cookies() from 'next/headers', and there a wild error appears : 'Cookies can only be modified in a server action or route handler'. But everything is used server side, every where when i console.log i got it on my Next console, i added "use server" everywhere it doesn't change anything.
Second problem is, we just implemented AppolloClient, but previously we did it with fetch() and a fetchWrapper to catch the error and then re-set the cookies and it worked well ? I cannot figure out why it doesn't work here. I also tried to put my logic in a route handler but still doesn't works.
Here is the handleError() :
({ graphQLErrors, networkError, operation, forward }: ErrorResponse) => {
if (graphQLErrors) {
if (isAuthError(graphQLErrors)) {
return new Observable((observer) => {
renewAuthTokensFromServer().then(....)
}
and the main logic is in the renewAuthTokensFromServer(), where i do set the new cookies.
Thank you !