I have a Next.js page (in the old pages
directory, not the newer app
directory) with this code:
export const getStaticProps = async (...args) => {
throw new Error('error inside');
}
throw new Error('error outside');
const SomePage = () => (<div/>);
export default SomePage;
When I try to build my app, I get the following failure message, as expected:
✓ Linting and checking validity of types Collecting page data .Error: error outside at (.next/server/pages/somePath/someFile.js:1:1516)
Build error occurred
However, when I remove the line that causes that error:
throw new Error('error outside');
the build passes ... even though I still have:
throw new Error('error inside');
My question is, how can my build be completing successfully if my getStaticProps
throws an error? I know the file is being included as part of the build, because 'error outside'
is getting thrown.
I thought that Next ran getStaticProps
as part of the build process. At https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-props it says:
getStaticProps always runs during next build
... but if that's true, why isn't my build failing, due to my thrown error?
P.S. The backstory here is that I have routes which have no data when I build them, but work fine in development mode, so I'm trying to debug what's going wrong. However, if I can't even throw a helpful error message or log anything from inside getStaticProps
, I don't know how to debug it.
P.P.S. I also tried logging in getStaticProps
, but never saw the result in the console where I'm building the app. It really seems like getStaticProps
isn't being run at all, but that makes no sense.
P.P.P.S. I tried removing getStaticProps
completely and got:
Error: getStaticPaths was added without a getStaticProps in /somePath. Without getStaticProps, getStaticPaths does nothing
So again, more evidence that getStaticProps
is getting used ... and yet somehow (despite having an error inside it) the build passes.
Next.js will use the paths
returned from getStaticPaths
to pre-render the pages. If getStaticPaths
returns an empty array, Next.js will not pre-render any pages at build time. As a result, getStaticProps
will not be called.