MiguelSlv
MiguelSlv

Reputation: 15193

Pages with `getServerSideProps` can not be exported

I think i am making some kind of confusion here.

According to the documentation, if i want Server Side Rendering (SSR) for the page i export the async function:

getServerSideProps

But if i do that i can't build the project for running either locally or now Zeit now. If i try build or deploy i get:

Error for page /_error: pages with getServerSideProps can not be exported. See more info here: https://err.sh/next.js/gssp-export

The link provided by the error says i can't export. But I used the example from the documentation below:

import React from "react"

export async function getServerSideProps() {
        return { props: {  } }
}

function Page({ data }) {
    // Render data...
}

export default Page

Do i have to change some configuration somewhere?

How to prevent from building this static page?

Upvotes: 9

Views: 21108

Answers (3)

Miguel
Miguel

Reputation: 11

I was struggling with that for a couple hours.

Here you can see the documentation that talks about this, but I will explain what I did also: https://nextjs.org/docs/messages/gssp-export

So in my case, the error was that I was trying to run this command when building the app: next build && next export. The command next export can only be used when you have static pages in all your project. This can also happen if you have in your next.config.js file the property output: "export" set.

So the solution for me was removing next export from my build command and that's it. Basically, don't try to build as static pages your app when you use getServerSideProps because that requires SSR so it is no longer a static app.

In the documentation they also say that if you have pages using getStaticProps or no lifecycle, these will remain being static.

Hopefully this will save time to someone :)

Upvotes: 1

MisterrPink
MisterrPink

Reputation: 91

getStaticProps() is the way to go under the following conditions:

  • The data required to render the page is available at build time ahead of a user’s request
  • The data comes from a headless CMS
  • The data can be publicly cached (not user-specific)
  • The page must be pre-rendered (for SEO) and be very fast — getStaticProps generates HTML and JSON files, both of which can be cached by a CDN for performance

Refer here for when to use getStaticProps vs getServerSideProps

Upvotes: 5

MiguelSlv
MiguelSlv

Reputation: 15193

Won´t work on page _error.js, by design decision, as posted here by nextjs maintenance staff.

One possibility is to use getInitialProps, instead.

Upvotes: 4

Related Questions