NextJS Error: Your `getServerSideProps` function did not return an object
From Next.js 10 getServerSideProps
supports returning a redirect
object for this exact purpose - which also solves the error you're getting by explicitly returning an object.
export const getServerSideProps: GetServerSideProps = async ({ req, res }) => { try { const cookie = req.headers.cookie if (!cookie) throw new Error('Missing auth token cookie') await Axios.get('/auth/me', { headers: { cookie } }) return { props: {} } } catch (err) { // Handle error return { redirect: { destination: '/login', statusCode: 307 } } }}
The catch doesn't return. You can do
res.writeHead(307, { Location: '/login' }).end()return { props: {ok: false, reason: "some error description for your own consumption, not for client side"}
Similarly, inside the try block, maybe you also want to res.writeHead(200) and push some data to the server?
I don't know very well why, on success, you return the data as a function return value, and on failure, you send the error through HTTP. I think this might be your point of failure, and that both sides should either return HTTP, or a function return value, or both, but not one on one and another one on the other.