Deploy a Nuxt site

In this guide you will learn how to deploy a Nuxt site with Amplify Hosting.

Getting started

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

Create a new Nuxt site:

1# Using YARN
2yarn create nuxt-app amplify-nuxt
3
4# Using npm
5npx create-nuxt-app amplify-nuxt

For the Deployment target, choose Static (Static/JAMStack hosting).

Next, change into the new directory:

1cd amplify-nuxt

Creating the Git repository

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

Nuxt Hosting with Amplify Console - Creating the repo

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

1git init
2git remote add origin git@github.com:username/project-name.git # or your git repository location
3git add .
4git commit -m 'initial commit'
5git push origin main

Deploying the site to Amplify Console Hosting

To use Amplify Hosting, visit the Amplify Console and click GET STARTED under Deploy.

Nuxt Hosting with Amplify Console - Console view

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

Nuxt Hosting with Amplify Console - Choosing your Git provider

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

Nuxt Hosting with Amplify Console - Choosing your Git repo and branch

In the App build and test settings view, click Edit and do the following:

  1. Set the build command to: yarn run generate
  2. Set the baseDirectory location (see build settings below)
  3. Click Save
  4. Click Next

For Nuxt 2, set baseDirectory: dist:

1version: 1
2frontend:
3 phases:
4 preBuild:
5 commands:
6 - yarn install
7 build:
8 commands:
9 - yarn generate
10 artifacts:
11 # IMPORTANT - Please verify your build output directory
12 baseDirectory: dist
13 files:
14 - '**/*'
15 cache:
16 paths:
17 - node_modules/**/*

For Nuxt 3, set baseDirectory: '.output/public':

1version: 1
2frontend:
3 phases:
4 preBuild:
5 commands:
6 - yarn install
7 build:
8 commands:
9 - yarn generate
10 artifacts:
11 # IMPORTANT - Please verify your build output directory
12 baseDirectory: '.output/public'
13 files:
14 - '**/*'
15 cache:
16 paths:
17 - node_modules/**/*

Below is an example of editing the build settings for a Nuxt v2 application: Nuxt Hosting with Amplify Console - Configuring the build settings

Finally, click Save and deploy.

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

Nuxt 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. .json, .webp, etc.) in the regular expression.