How to self-host Gatsby v4 with Server Side Rendering (SSR) capabilities

Issue

I’m trying to set up a site to host static content, using GatsbyJS. Some of my pages use SSR. When I run it using gatsby serve from the project root, i’m able to view these pages. I’m not sure how I can deploy and host this application with SSR capabilities. according to this page, gatsby serve is to be used only to test the production build, which infers that there may be a different strategy to host actual production.

Our goal is to deploy to a virtual private server (vps) or Azure App Service, where we have more or less full control of our environment.

I was able to host the static site using this script on Azure App Service (win-node16):

const express = require('express'); 
const gatsbyExpress = require('gatsby-plugin-express'); 
const app = express();
const port = process.env.PORT || 8080; 
const dev = process.env.NODE_ENV !== "production";

// serve static files before gatsby
Express app.use(express.static('public/')); 
app.use(gatsbyExpress('config/gatsby-express.json', 
  {   
      publicDir: 'public/',
      redirectSlashes: true, 
  }));

app.listen(port, function() {
   console.log(`App started on port ${port}`); 
});

this seems serve the static pages properly, but all pages with SSR returns 404. I surmise it may be due to the fact that those pages did not have html stubs generated. as I’m new to Express, i’m also not sure if this is the right approach

if anyone has advice on hosting that would be appreciated.

Solution

unfortunately this plugin gatsby-plugin-express is 2 or 3 years old and therefor doesn’t support server-side rendering.

You can see this question How to properly run gatsby with SSR in a production environment for more options.

TL;DR: There is no option for self-hosting it unless you will write it yourself.

Answered By – krotovic

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published