Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is throwing an unnecessary error when getStaticProps & getServerSideProps are undefined #11309

Closed
aralroca opened this issue Mar 24, 2020 · 7 comments
Milestone

Comments

@aralroca
Copy link
Contributor

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.

@aralroca aralroca changed the title Is throwing an unecessary error Is throwing an unecessary error when getStaticProps & getServerSideProps are undefined Mar 24, 2020
@aralroca aralroca changed the title Is throwing an unecessary error when getStaticProps & getServerSideProps are undefined Is throwing an unnecessary error when getStaticProps & getServerSideProps are undefined Mar 24, 2020
@Timer
Copy link
Member

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

@aralroca
Copy link
Contributor Author

aralroca commented Mar 24, 2020

Ok @Timer ! But why this restriction instead of just ignore when they aren't functions?

@timneutkens
Copy link
Member

Because getStaticProps en getServerSideProps are tree shaken from browser bundles and what you're trying to do (dynamic assignment) makes it impossible to tree shake it.

@aralroca
Copy link
Contributor Author

Ok. In that case, it makes sense to me! Thanks to clarifying this @timneutkens

@Timer Timer added this to the 9.3.2 milestone Mar 24, 2020
@Timer Timer modified the milestones: 9.3.2, 9.3.3, 9.3.4 Mar 27, 2020
@Timer Timer modified the milestones: 9.3.4, 9.3.5 Apr 3, 2020
@Timer Timer modified the milestones: 9.3.5, 9.3.6 Apr 15, 2020
@Timer Timer modified the milestones: 9.3.6, 9.3.x Apr 28, 2020
@felipenmoura
Copy link

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.

@timneutkens
Copy link
Member

Going to close this issue as it's using getStaticProps/getServerSideProps in an unsupported way.

@felipenmoura there are some feature requests for a behavior like that but getServerSideProps and getStaticProps are two different methods intentionally, they enable completely different behaviors.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants