I have getServerSideProps
in a page, eg index.tsx
. (Not in a component like these problems)
...and it is export
ed (This is not like this problem)
Here's the basic code structure
index.tsx:
import processRequest from '../lib/processRequest';
const Index = ({ otherStuffPassedFrom_appjs, data }) => {
console.log(data) // undefined
return (
<div>
{data}
</div>
)
}
export async function getServerSideProps(context) {
const data = await processRequest(...stuff extracted from the context);
console.log(data); // data exists here
return { props : {data} };
}
The weirdest thing is that if I put a breakpoint on the return { props : {data} };
line, and then run manually from there, the data
shows up in the client's console.log()
.
processRequest
is a complex function with many async
await
calls to APIs and a firestore DB and I'm not able to share the details. processRequest
must be run on multiple pages, which is why I put in a lib
file.
I have checked that await
s are applied to all async
function calls in processRequest
and descendant function calls.
Grateful for any clues on how to debug or what more to try out.
UPDATE:
I found the conflicting code, I think. I also had
_app.tsx:
import { useRouter } from "../node_modules/next/router";
function MyApp({ Component, pageProps }) {
const router = useRouter();
if (route contained particular query string) {
router.replace(router.basePath); // remove the query string
}
...
return (
<Component {...{ ...pageProps }} />
);
}
processRequest
does stuff particularly when this query string is present, so the effect of the code above is to process the query string, and then remove it.
Both the client and server console.log
s were happening twice, the first on the server had data
, but the second was undefined
.
When I removed the router.replace
the client was reporting the console.log on the first call (undefined
on the second, which was after the query string was removed, as expected).
Incidentally, it calls the client console.log
6 times if I put the router.replace
in index.js instead: 2 times with data
and 4 undefined
.
So is this a bug with _app
and router.replace
?