Search code examples
gatsby

When deploying a Gatsby site, should I use "gatsby serve" as the deploy script?


I am a little confused about how I serve the static build in the /public folder when I am deploying. I am thinking about using 'gatsby serve' but it mentions this is to be used before deployment so I am confused. Any help is appreciated!


Solution

  • gatsby serve serves the production build of your site in a development environment (locally or on a staged server). But the command in charge of building your site for a automated deploy (CI/CD) is gatsby build, which is the one that creates a /public folder that needs to be stored in your server.

    In plain words: gatsby serve allows you to see your built site locally (by default at localhost in the port 9000, localhost:9000) but the command that needs to be triggered to build your site in a deployment is gatsby build, because you will be storing the /public folder in the /www or /public of your hosting server.

    From Gatsby docs:

    gatsby serve

    Serves the production build of your site for testing prior to deployment. Should be run from the root of your project.

    Spot the for testing prior to deployment

    gatsby build

    Compiles your site for production so it can be deployed. Should be run from the root of your project.

    To serve your site in your server, you just need to place the /public folder (created by gatsby build) in the public folder of your hosting server.