#NextJS GetStaticProps & Local Api

11 messages · Page 1 of 1 (latest)

abstract grove
#

This may be an immensely stupid question but is it possible that the LocalApi does not work with GetStaticProps and GetStaticPaths?

When using yarn build, I get the Error:

Error occurred prerendering page "/en/articles/dave-en". Read more: https://nextjs.org/docs/messages/prerender-error
TypeError: Cannot read property 'config' of undefined
    at find (/Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/payload/dist/collections/operations/find.js:20:27)
    at findLocal (/Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/payload/dist/collections/operations/local/find.js:21:31)
    at Payload.find (/Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/payload/dist/index.js:42:20)
    at getStaticProps (/Users/dave/WebProjects/snafu.zone/dave-dmg/.next/server/pages/articles/[...slug].js:91:57)
    at renderToHTML (/Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/next/dist/server/render.js:386:26)
    at async /Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/next/dist/export/worker.js:304:36
    at async Span.traceAsyncFn (/Users/dave/WebProjects/snafu.zone/dave-dmg/node_modules/next/dist/trace/trace.js:79:20)```
twin sandal
#

It's not stupid at all!

#

I got blasted by this when I tried

#

and the answer is fairly unfortunate but i know exactly what it is as I had to go deeeeeep down this rabbit hole early on

#

NextJS uses separate threads for getStaticPaths and getStaticProps... meaning Payload is not initialized in the separate threads. So you'd have to re-initialize it yourself if you want to use it in those contexts. This is not an issue for getServerSideProps though

#

also, you can pass payload.init the local: true property so that initialization is much quicker. It basically makes it so that payload.init can be run outside of the context of mounting HTTP routes like REST and GraphQL, and exposes local API only

#

now the only thing here is, that this might not be necessary. You may see better performance just using REST or GraphQL in these cases

abstract grove
#

Thanks for the answer, then I'll just switch to REST for StaticProps and Paths and use the local api for other stuff (because the local api is one of the main selling points for me - apart from code-based config). But it's good to know and maybe helpful if anybody else stumbles over the question

twin sandal
#

I totally agree

#

there may be a way to run getStaticProps / getStaticPaths in the same thread nowadays

#

this was a while ago when i discovered this