Next.js Discord

Discord Forum

How to handle statusCode in NextJs and ApolloClient

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I have this particular problem in which whenever the user enters an invalid url it's redirected to the homepage instead of showing the 404 error page. And since we're using Apollo-Server and ApolloClient the status code is always a 200 OK.

Here is my code in the ApolloClient conf:

export default withApollo(({ctx, initialState}) => { const cache = new InMemoryCache({ typePolicies: { AttributesVideoContent: { keyFields: ["path"] } } }).restore(initialState || {}); const httpLink = new HttpLink({ uri: process.env.NEXT_PUBLIC_APOLLO_SERVER_URL, }); const ssrMode = true; return new ApolloClient({ ssrMode, link: from([ onError(({graphQLErrors, networkError}) => { if(graphQLErrors){ graphQLErrors.forEach((graphError) => { const {extensions, locations, message, path} = graphError; console.error(${extensions?.code}: , message); }); } if(networkError) console.log([Network error]: ${networkError}`);
}),
httpLink
]),
cache,
});

}, {
render: ({Page, props}) => {
return (
<ApolloProvider client={props.apollo}>
<AppWrapper {...props} Page={Page} />
</ApolloProvider>
)
}
});``

0 Replies