Deploy a Gatsby site

In this guide you will learn how to deploy a Gatsby site with Amplify hosting.

Getting started

In this step, you will create a new Gatsby site. If you have already created a site, you can jump to the next step.

If you haven't already, install the Gatsby CLI:

1# Using YARN
2yarn global add gatsby-cli
4# Using npm
5npm install --global gatsby-cli

Next, create a new site and change into the new directory:

1gatsby new gatsby-amplify
2cd gatsby-amplify

Creating the Git repository

Next, create a new Git repository and copy the URI of the repo to your clipboard.

Gatsby Hosting with Amplify Console - Creating a new project in Git

Now, initialize the new repository within the root of your project and push the code to Git.

1git init
2git remote add origin # or your git repository location
3git add .
4git commit -m 'initial commit'
5git push origin main

Deploying the site to Amplify Console Hosting

Visit the Amplify Console and click GET STARTED under Deploy.

Gatsby Hosting with Amplify Console - Console view

Next, choose the Git provider that you are using and click Continue:

Gatsby Hosting with Amplify Console - Choosing Git provider

In the next screen, choose your repository and branch and click Next:

Gatsby Hosting with Amplify Console - Choosing repo and branch

In the App build and test settings view, click Next:

Gatsby Hosting with Amplify Console - Configuring build settings

Finally, click Save and deploy.

Once your site has successfully deployed, you should see three green checkmarks:

Gatsby Hosting with Amplify Console - Deployment complete

To view the live site, click on the automatically generated URL given to you by the Amplify Console.

Setting up rewrites for SPAs

Most SPA frameworks support HTML5 history.pushState() to change browser location without triggering a server request. This works for users who begin their journey from the root (or /index.html), but fails for users who navigate directly to any other page. Using regular expressions, the following example sets up a 200 rewrite for all files to index.html, except for the specific file extensions specified in the regular expression.

To set up rewrites, follow the guide on AWS Amplify Hosting's documentation.

Make sure you list the file extensions used in your application (i.e. .manifest, .webp, etc.) in the regular expression.