Next.js Page's getStaticProps throws an error ... but still builds? - Stack Overflow

I have a Next.js page (in the old pages directory, not the newer app directory) with this code:export

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 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.

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./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.

Share Improve this question edited Mar 24 at 21:20 machineghost asked Mar 24 at 20:54 machineghostmachineghost 35.7k32 gold badges173 silver badges260 bronze badges 3
  • 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. – Duannx Commented Mar 25 at 3:25
  • That was it, mystery solved! Thank you so much ... would you mind putting your comment in the form of an answer, so that I can officially accept it? – machineghost Commented Mar 25 at 15:13
  • You're welcome :) – Duannx Commented Mar 26 at 4:31
Add a comment  | 

1 Answer 1

Reset to default 1

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.

Nextjs getStaticPaths Documentation

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744228835a4564142.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信