c8r / x0

Document & develop React components without breaking a sweat

Home Page:https://compositor.io/x0/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to execute getInitialProps in node only

cdock1029 opened this issue · comments

Or.. How do we only fetch data in a node context / use node-only data fetching dependencies?

Since use-case is static builds, I want to fetch data only during build.
In fact, I'm trying to build a bundle-less site, so my expectation is client-side context should never come into play with this static function.

Using firebase-admin, it shows errors related to missing node libraries.

Tried to copy something similar from a next.js discussion vercel/next.js#219 (comment)
not surprisingly it didn't work.

Thanks for this library by the way!

If you use the --static flag, it will output HTML without the JS bundle and getInitialProps should work as expected

I was having issues with getting firebase-admin (a node-only package) to build, even using --static flag.

My solution was from this stack-overflow answer:

In webpack.config.js add the following:

const nodeExternals = require('webpack-node-externals')
module.exports = {
  target: 'node', // in order to ignore built-in modules like path, fs, etc.
  externals: [nodeExternals()], // in order to ignore all modules in node_modules folder
}