Search code examples
javascriptgatsbyserver-side-renderingstatic-sitestatic-site-generation

Is it possible to build a static-only (SSG) site with Gatsby, disabling SSR?


In the Gatsby documentation it says that the default build mode is SSG:

SSG is the default rendering mode in Gatsby. While the name has the word “static” in it, it doesn’t at all mean boring or lifeless. It simply means the entire site is pre-rendered into HTML, CSS, and JavaScript at build time, which then get served as static assets to the browser.

But it seems that when you build it, the compontents and libraries have to be SSR friendly, and you need to use workarounds when using client-only libraries.

From the documentation it seems like there are three options for rendering:

What if I am not interested in using SSR and just want to serve the static SSG version of a Gatsby site. Is there an option to build a purely static site, client-side site like Vite or Create React App and not have it complain about server-side rendering errors like this?

failed Building static HTML for pages - 1.639s

 ERROR #95312  HTML.COMPILATION

"window" is not available during server-side rendering. Enable "DEV_SSR" to debug this during "gatsby develop".

Solution

  • Building a static site and doing server-side rendering are very nearly the same thing. The primary difference is when it is done (at build time instead of on demand).

    The code to generate the HTML to be delivered to the client still has to be executed, and it still has to run in an environment where window is not available.

    So no. You still need to do the workaround so that the code which can only run on the client is only run on the client.