question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is throwing an unnecessary error when getStaticProps & getServerSideProps are undefined

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
felipenmouracommented, Oct 11, 2020

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.

2reactions
Timercommented, Mar 24, 2020

This behavior will not / should not work by design:

export let getStaticProps
export let getServerSideProps

if(process.env.SSG) {
  getStaticProps = loadData
} else { 
  getServerSideProps = loadData
}

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.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found