You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Tutorial: Deploy static-rendered Nuxt.js websites on Azure Static Web Apps"
3
-
description: "Generate and deploy Nuxt.js dynamic sites with Azure Static Web Apps."
2
+
title: "Tutorial: Deploy Nuxt sites with universal rendering on Azure Static Web Apps"
3
+
description: "Generate and deploy Nuxt 3 sites with universal rendering on Azure Static Web Apps."
4
4
services: static-web-apps
5
5
author: craigshoemaker
6
6
ms.service: static-web-apps
7
7
ms.topic: tutorial
8
-
ms.date: 05/08/2020
8
+
ms.date: 09/01/2022
9
9
ms.author: cshoe
10
10
ms.custom: devx-track-js
11
11
---
12
12
13
-
# Deploy static-rendered Nuxt.js websites on Azure Static Web Apps
13
+
# Deploy Nuxt 3 sites with universal rendering on Azure Static Web Apps
14
14
15
-
In this tutorial, you learn to deploy a [Nuxt.js](https://nuxtjs.org) generated static website to [Azure Static Web Apps](overview.md). To begin, you learn to set up, configure, and deploy a Nuxt.js app. During this process, you also learn to deal with common challenges often faced when generating static pages with Nuxt.js
15
+
In this tutorial, you learn to deploy a [Nuxt 3](https://v3.nuxtjs.org/) application to [Azure Static Web Apps](overview.md). Nuxt 3 supports [universal (client-side and server-side) rendering](https://v3.nuxtjs.org/guide/concepts/rendering/#universal-rendering), including server and API routes. Without extra configuration, you can deploy Nuxt 3 apps with universal rendering to Azure Static Web Apps. When the app is built in the Static Web Apps GitHub Action or Azure Pipelines task, Nuxt 3 automatically converts it into static assets and an Azure Functions app that are compatible with Azure Static Web Apps.
16
16
17
17
## Prerequisites
18
18
19
19
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/).
20
20
- A GitHub account. [Create an account for free](https://github.com/join).
21
-
-[Node.js](https://nodejs.org) installed.
21
+
-[Node.js](https://nodejs.org)16 or later installed.
22
22
23
-
## Set up a Nuxt.js app
23
+
## Set up a Nuxt 3 app
24
24
25
-
You can set up a new Nuxt.js project using `create-nuxt-app`. Instead of a new project, in this tutorial you begin by cloning an existing repository. This repository is set up to demonstrate how to deploy a dynamic Nuxt.js app as a static site.
25
+
You can set up a new Nuxt project using `npx nuxi init nuxt-app`. Instead of using a new project, this tutorial uses an existing repositoryset up to demonstrate how to deploy a Nuxt 3 site with universal rendering on Azure Static Web Apps.
26
26
27
-
1. Create a new repository under your GitHub account from a template repository.
28
-
1. Navigate to [http://github.com/staticwebdev/nuxtjs-starter/generate](https://github.com/login?return_to=/staticwebdev/nuxtjs-starter/generate)
29
-
1. Name the repository **nuxtjs-starter**.
27
+
1. Navigate to [http://github.com/staticwebdev/nuxt-3-starter/generate](https://github.com/login?return_to=/staticwebdev/nuxt-3-starter/generate).
28
+
1. Name the repository **nuxt-3-starter**.
30
29
1. Next, clone the new repo to your machine. Make sure to replace <YOUR_GITHUB_ACCOUNT_NAME> with your account name.
@@ -48,46 +47,16 @@ You can set up a new Nuxt.js project using `create-nuxt-app`. Instead of a new p
48
47
1. Start Nuxt.js app in development:
49
48
50
49
```bash
51
-
npm run dev
50
+
npm run dev -- -o
52
51
```
53
52
54
-
Navigate to `http://localhost:3000` to open the app, where you should see the following website open in your preferred browser:
53
+
Navigate to `http://localhost:3000` to open the app, where you should see the following website open in your preferred browser. Select the buttons to invoke server and API routes.
When you build a Nuxt.js site using `npm run build`, the app is built as a traditional web app, not a static site. To generate a static site, use the following application configuration.
65
-
66
-
1. Update the _package.json_'s build script to only generate a static site using the `nuxt generate` command:
67
-
68
-
```json
69
-
"scripts": {
70
-
"dev": "nuxt dev",
71
-
"build": "nuxt generate"
72
-
},
73
-
```
74
-
75
-
Now with this command in place, Static Web Apps will run the `build` script every time you push a commit.
76
-
77
-
1. Generate a static site:
78
-
79
-
```bash
80
-
npm run build
81
-
```
82
-
83
-
Nuxt.js will generate the static site and copy it into a _dist_ folder at the root of your working directory.
84
-
85
-
> [!NOTE]
86
-
> This folder is listed in the _.gitignore_ file because it should be generated by CI/CD when you deploy.
87
-
88
-
## Deploy your static website
89
-
90
-
The following steps show how to link the app you just pushed to GitHub to Azure Static Web Apps. Once in Azure, you can deploy the application to a production environment.
59
+
The following steps show how to create an Azure Static Web Apps resource and configure it to deploy your app from GitHub.
91
60
92
61
### Create an Azure Static Web Apps resource
93
62
@@ -102,7 +71,7 @@ The following steps show how to link the app you just pushed to GitHub to Azure
102
71
| --- | --- |
103
72
| _Subscription_ | Your Azure subscription name. |
104
73
| _Resource group_ |**my-nuxtjs-group**|
105
-
| _Name_ | **my-nuxtjs-app** |
74
+
| _Name_ |**my-nuxt3-app**|
106
75
| _Plan type_ |**Free**|
107
76
| _Region for Azure Functions API and staging environments_ | Select a region closest to you. |
108
77
| _Source_ |**GitHub**|
@@ -119,76 +88,39 @@ The following steps show how to link the app you just pushed to GitHub to Azure
119
88
120
89
1. In the _Build Details_ section, select**Custom** from the _Build Presets_ drop-down and keep the default values.
121
90
122
-
1. In the _App location_, enter **./** in the box.
123
-
1. Leave the _Api location_ box empty.
124
-
1. In the _Output location_ box, enter **dist**.
91
+
1. In the _App location_, enter **/**in the box.
92
+
1. In the _Api location_, enter **.output/server**in the box.
93
+
1. In the _Output location_, enter **.output/public**in the box.
125
94
126
95
### Review and create
127
96
128
-
1. Select the **Review + Create** button to verify the details are all correct.
97
+
1. Select **Review + Create** to verify the details are all correct.
129
98
130
-
1. Select **Create** to start the creation of the App Service Static Web App and provision a GitHub Actions for deployment.
99
+
1. Select **Create** to start the creation of the static web app and provision a GitHub Actions for deployment.
131
100
132
-
1. Once the deployment completes click, **Go to resource**.
101
+
1. Once the deployment completes, select**Go to resource**.
133
102
134
-
1. On the _Overview_ window, click the *URL* link to open your deployed application.
103
+
1. On the _Overview_ window, selectthe*URL* link to open your deployed application.
135
104
136
-
If the website does note immediately load, then the background GitHub Actions workflow is still running. Once the workflow is complete you can then click refresh the browser to view your web app.
105
+
If the website does not immediately load, then the background GitHub Actions workflow is still running. Once the workflow is complete you can then refresh the browser to view your web app.
137
106
138
107
You can check the status of the Actions workflows by navigating to the Actions for your repository:
When you created the app, Azure Static Web Apps created a GitHub Actions workflow file in your repository. You need to bring this file down to your local repository so your git history is synchronized.
113
+
### Synchronize changes
147
114
148
-
Return to the terminal and run the following command `git pull origin main`.
115
+
When you created the app, Azure Static Web Apps created a GitHub Actions workflow file in your repository. Return to the terminal and run the following commandto pull the commit containing the new file.
149
116
150
-
## Configure dynamic routes
151
-
152
-
Navigate to the newly-deployed site and click on one of the framework or library logos. Instead of getting a details page, you get a 404 error page.
153
-
154
-
:::image type="content" source="media/deploy-nuxtjs/404-in-production.png" alt-text="404 on dynamic routes":::
155
-
156
-
The reason for this is, Nuxt.js generated the static site, it only did so for the home page. Nuxt.js can generate equivalent static `.html` files for every `.vue` pages file, but there's an exception.
157
-
158
-
If the page is a dynamic page, for example `_id.vue`, it won't have enough information to generate a static HTML from such dynamic page. You'll have to explicitly provide the possible paths for the dynamic routes.
159
-
160
-
## Generate static pages from dynamic routes
161
-
162
-
1. Update the _nuxt.config.js_ file so that Nuxt.js uses a list of all available data to generate static pages for each framework/library:
163
-
164
-
```javascript
165
-
import { projects } from "./utils/projectsData";
166
-
167
-
export default {
168
-
mode: "universal",
169
-
170
-
//...truncated
171
-
172
-
generate: {
173
-
async routes() {
174
-
const paths = [];
175
-
176
-
projects.forEach(project => {
177
-
paths.push(`/project/${project.slug}`);
178
-
});
179
-
180
-
return paths;
181
-
}
182
-
}
183
-
};
184
-
```
185
-
186
-
> [!NOTE]
187
-
>`routes` is an async function, so you can make a request to an API in this functionand use the returned list to generate the paths.
117
+
```bash
118
+
git pull
119
+
```
188
120
189
-
2. Push the new changes to your GitHub repository and waitfor a few minutes while GitHubActions builds your site again. After the build is complete, the 404 error disappears.
121
+
Make changes to the app by updating the code and pushing it to GitHub. GitHub Actions automatically builds and deploys the app.
190
122
191
-
:::image type="content" source="media/deploy-nuxtjs/404-in-production-fixed.png" alt-text="404 on dynamic routes fixed":::
123
+
For more information, see the Azure Static Web Apps Nuxt 3 deployment preset [documentation](https://v3.nuxtjs.org/guide/deploy/providers/azure/).
0 commit comments