Is throwing an unnecessary error when getStaticProps & getServerSideProps are undefined
See original GitHub issueBug report
Describe the bug
When getStaticProps and getServerSideProps exist both as an export, is throwing an error.
You can not use getStaticProps with getServerSideProps. To use SSG, please remove getServerSideProps
Looks OK, however, IMO it should throw this error only when both are functions. With this, it will be possible to use conditional exports.
export let getStaticProps
export let getServerSideProps
if(process.env.SSG) {
getStaticProps = loadData
} else {
getServerSideProps = loadData
}
To Reproduce
export const getStaticProps = undefined
export const getServerSideProps = undefined
Expected behavior
With this:
export const getStaticProps = undefined
export const getServerSideProps = async ctx => ({ props: { itWorks: false } })
I expect that getServerSideProps is executed because getStaticProps is not defined.
With this:
export const getStaticProps = async ctx => ({ props: { itWorks: false } })
export const getServerSideProps = undefined
I expect that getStaticProps is executed because getServerSideProps is not defined.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Data Fetching: getServerSideProps - Next.js
Fetch data on each request with `getServerSideProps`. ... If an error is thrown inside getServerSideProps , it will show the pages/500.js file.
Read more >Next.js getServerSideProps is always undefined
Make sure that is a Next.js page and located in pages directory as getServerSideProps only allowed in a page.
Read more >How to solve "window is not defined" errors in React and Next.js
The "React" way to solve this issue would be to use the useEffect React hook. Which only runs at the rendering phase, so...
Read more >How To Solve Module Not Found Can't Resolve 'fs' in Next.js
While working on custom sitemap functionality for a Next.js project, I encountered the following error: Module not found: Can't resolve 'fs' ...
Read more >How to Build Your Own Blog with Next.js and MDX
There was a certain error that stuck with me, and refused to go away ... to be loaded within Next.js' getStaticProps or getServerSideProps...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Is there a way to use them both together? I mean…my first thought was to use static props for some info (like version, port, path, envType, etc) and serverside props to api values.
This behavior will not / should not work by design:
You must export a function.
If the above works when you’re not mixing the two, we need to issue a patch release to disallow such behavior.