My setup
I'm using getInitialProps on the _app.js file to preload data on the server side:
import App from "next/app";
// The problematic import
import nodeFetchCache from "node-fetch-cache";
function MyApp({ Component, pageProps, customProps }) {
return <Component customProps={customProps} {...pageProps} />;
}
MyApp.getInitialProps = async (appContext) => {
const appProps = await App.getInitialProps(appContext);
// When I remove the top import and use a classic "fetch" here, it works fine
const res = await nodeFetchCache(
`${process.env.NEXT_PUBLIC_API_URL}/api/node/last_items`
);
const items = await res.json();
return {
...appProps,
customProps: {
footerArticles: items.data.articles,
footerVideos: items.data.videos,
},
};
};
export default MyApp;
My problem
This code yields an error:
Unhandled Runtime Error TypeError: The "original" argument must be of type Function
Call Stack promisify node_modules/util/util.js (614:0) eval node_modules/@npmcli/move-file/index.js (25:0) Object../node_modules/@npmcli/move-file/index.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (716:1) Object.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval node_modules/cacache/lib/entry-index.js (16:17) Object../node_modules/cacache/lib/entry-index.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (1598:1) Object.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval node_modules/cacache/ls.js (3:14) Object../node_modules/cacache/ls.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (1686:1) Object.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval node_modules/cacache/index.js (3:11) Object../node_modules/cacache/index.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (1543:1) Object.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval webpack-internal:///./node_modules/node-fetch-cache/src/classes/caching/file_system_cache.js (5:65) Module../node_modules/node-fetch-cache/src/classes/caching/file_system_cache.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (4513:1) Module.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval webpack-internal:///./node_modules/node-fetch-cache/src/index.js (13:95) Module../node_modules/node-fetch-cache/src/index.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (4557:1) Module.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval webpack-internal:///./pages/_app.js (10:74) Module../pages/_app.js file:///Users/dev/app/.next/static/chunks/pages/_app.js (40:1) Module.options.factory /_next/static/chunks/webpack.js (685:31) webpack_require file:///Users/dev/app/.next/static/chunks/webpack.js (37:33) fn /_next/static/chunks/webpack.js (354:21) eval node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?page=%2F_app&absolutePagePath=private-next-pages%2F_app! (5:15) eval node_modules/next/dist/client/route-loader.js (207:48)
From what other devs told me, it seems like Next is trying to import server side code on the client. Even when I remove the await nodeFetchCache(...) call, the only fact that I import 'node-fetch-cache' at the top gives me an error.
My question
Is what I'm trying to achieve even possible ? Isn't getInitialProps supposed to run only on the server side? And isn't code/tree shaking supposed to eliminate unused imports from the client side bundle?
I'm a bit lost here. Any help would be very much appreciated.