1

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".
Adam D
  • 1,962
  • 2
  • 21
  • 37

2 Answers2

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • But with things like vite and create-react-app the code is built in node, and then a bunch of static assets are built which can be served staticly. Those tools don't complain about `window` not being available during build. There's no similar functionality with Gatsby? – Adam D Feb 17 '23 at 16:31
  • 1
    I've not used vite. The application generated by create-react-app is a basic client-side-only React application. Any static assets generated are just things like images and stylesheets. It doesn't generate static pages *which is the **point** of Gatsby* and the thing that requires the components be rendered by Node and not just by the browser. – Quentin Feb 17 '23 at 16:36
  • Oh, I see, I wasn't aware that when Gatsby talked about serving static pages they do the initial rendering ahead of time into HTML, kind of like [Astro](https://astro.build/) does. – Adam D Feb 17 '23 at 17:18
  • Re edit: "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?" — The answer is still no. If you don't want to render pages ahead of time, don't use a tool designed to do that. – Quentin Feb 17 '23 at 17:20
  • Thanks. I wasn't fully understanding what Gatsby was designed to do I guess. – Adam D Feb 17 '23 at 17:44
0

I use a modified version of gatsby-plugin-no-javascript to build to a static site with gatsby react scripts removed.

Tony Gutierrez
  • 753
  • 1
  • 6
  • 16