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
Copy file name to clipboardExpand all lines: articles/static-apps/deploy-nextjs.md
+34-38Lines changed: 34 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: "Tutorial: Deploy server-rendered Next.js websites on App Service Static Apps"
3
-
description: "Build with JAMstack: Generate and deploy Next.js dynamic sites with App Service Static App."
2
+
title: "Tutorial: Deploy server-rendered Next.js websites on Azure Static Web Apps"
3
+
description: "Generate and deploy Next.js dynamic sites with Azure Static Web Apps."
4
4
services: #Required for articles that deal with a service; service slug assigned to your service by ACOM.
5
5
author: christiannwamba
6
6
ms.service: azure-functions
@@ -12,17 +12,17 @@ ms.author: chnwamba
12
12
13
13
# Deploy server-rendered Next.js websites on App Service Static Apps
14
14
15
-
In this tutorial, you will learn how to deploy a [Next.js](https://nextjs.org) generated static website to [Azure App Service Static Apps](overview.md). You'll learn how to set up, configure, and deploy a Next.js app. You wil also learn how to deal with the most prominent challenges you might come across when generating static pages with Next.js
15
+
In this tutorial, you learn to deploy a [Next.js](https://nextjs.org) generated static website to [Azure Static Web Apps](overview.md). To begin, you learn to set up, configure, and deploy a Next.js app. During this process, you also learn to deal with common challenges often faced when generating static pages with Next.js
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
21
-[Node.js](https://nodejs.org) installed.
22
22
23
-
## Set up a Next.js App
23
+
## Set up a Next.js app
24
24
25
-
You can set up a new Next.js project using `create-next-app`. Instead of a new project, let's clone a repository that is set up to demonstrate an edge case when you try to deploy a dynamic Next.js app. The repository contains an example of a dynamic site that we want to serve as a static site.
25
+
Rather than using the Next.js CLI to create an app, you can use a starter repository that includes an existing Next.js app. This repository features a Next.js app with dynamic routes, which highlights a common deployment issue. Dynamic routes need an extra deployment configuration which you will learn more about in a moment.
26
26
27
27
1. Clone starter repository:
28
28
@@ -58,9 +58,9 @@ When you click on a framework/library, you should see a details page about the s
58
58
59
59
## Generate a static website from Next.js build
60
60
61
-
When you build a Next.js site using `npm run build`, it only builds the app to be served like a regular web app, not a static site. Here is how to generate a static site from the build:
61
+
When you build a Next.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.
62
62
63
-
1. Configure static routes--create a _next.config.js_ file at the root of your project and add the following route config:
63
+
1. To configure static routes, create file named _next.config.js_ at the root of your project.
64
64
65
65
```javascript
66
66
module.exports = {
@@ -73,9 +73,9 @@ When you build a Next.js site using `npm run build`, it only builds the app to b
73
73
};
74
74
```
75
75
76
-
This configuration will map `/` to the Next.js page that is served for`/`, and that is the _pages/index.js_ page file.
76
+
This configuration maps `/` to the Next.js page that is served forthe `/` route, and that is the _pages/index.js_ page file.
77
77
78
-
1. Update the _package.json_'s build script to also generate a static site after building, using the `next export` command:
78
+
1. Update the _package.json_'s build script to also generate a static site after building, using the `next export` command. The `export` command generates a static site.
79
79
80
80
```json
81
81
"scripts": {
@@ -84,22 +84,22 @@ When you build a Next.js site using `npm run build`, it only builds the app to b
84
84
},
85
85
```
86
86
87
-
App Service Static Apps will run the `build` script every time you push a commit.
87
+
Now with this command in place, Static Web Apps will run the `build` script every time you push a commit.
88
88
89
89
1. Generate a static site:
90
90
91
91
```bash
92
92
npm run build
93
93
```
94
94
95
-
The static site will be generated and copied into an _out_ folder at the root of your working directory.
95
+
The static site is generated and copied into an _out_ folder at the root of your working directory.
96
96
97
97
> [!NOTE]
98
-
> This folder is ignored in _.gitignore_ because it should be generated by CI/CD when you deploy.
98
+
> This folder is listed in the _.gitignore_ file because it should be generated by CI/CD when you deploy.
99
99
100
100
## Push your static website to GitHub
101
101
102
-
App Service Static Apps will deploy your app from a GitHub repository and it will keep doing so for every pushed commit. Set up a repository:
102
+
Azure Static Web Apps deploys your app from a GitHub repository and keeps doing so for every pushed commit to a designated branch. Use the following command to set up a repository.
103
103
104
104
1. Initialize a git repo
105
105
@@ -130,37 +130,37 @@ App Service Static Apps will deploy your app from a GitHub repository and it wil
130
130
131
131
## Deploy your static website
132
132
133
-
The following steps show how to create a new static site app and deploy it to a production environment.
133
+
The following steps show how to deploy the app you just pushed to GitHub to Azure Static Web Apps. Once in Azure you can deploy the application to a production environment.
134
134
135
135
### Create Azure App Service Static App
136
136
137
137
1. Navigate to the [Azure Portal](https://portal.azure.com).
138
-
1. Click **Create a Resource**then search for**Static App** and selectit.
138
+
1. Click **Create a Resource**then search for**Static Web Apps** and selectthe matching result.
139
139
140
140
[Image of Static App]
141
141
142
142
1. Select a subscription from the *Subscription* dropdown list or use the default one.
143
143
1. Click the **New** link below the *Resource group* dropdown. In *New resource group name*, type**nextjsstaticsite** and click **OK**
144
-
1. Provide a globally unique name foryour appin the **Name** text box. Valid characters include `a-z`, `A-Z`, `0-9`, and `-`. This value is used as the URL prefix foryour static appin the format of `https://<APP_NAME>.....`
145
-
1. In the *Region*dropdown, choose a region closest to you.
146
-
1. Select **Basic** from the SKU dropdown.
144
+
1. Provide a globally unique name foryour appin the **Name** text box. Valid characters include `a-z`, `A-Z`, `0-9`, and `-`. This value is used as the URL prefix foryour static appin the format of `https://<APP_NAME>.azurestaticapps.net`.
145
+
1. In the *Region*drop-down, choose a region closest to you.
146
+
1. Select **Free** from the SKU drop-down.
147
147
148
148
[Image of static app form]
149
149
150
150
### Add a GitHub repository
151
151
152
-
Azure App Service Static App needs access to the repository where your Next.js app lives so it can automatically deploy commits:
152
+
The new Static Web Apps account needs access to the repository with your Next.js app so it can automatically deploy commits.
153
153
154
154
1. Click the **Sign in with GitHub button**
155
-
1. Select the **Organization** under which you created the repo for your Next.js project. It can also be your GitHub username.
156
-
1. Find the name of the repository you created earlier and selectit.
157
-
1. Choose **master** as the branch from the *Branch*dropdown.
155
+
1. Select the **Organization** under which you created the repo for your Next.js project, which may be your GitHub username.
156
+
1. Find and selectthe name of the repository you created earlier.
157
+
1. Choose **master** as the branch from the *Branch*drop-down.
158
158
159
159
[Image of new repo form]
160
160
161
161
### Configure the build process
162
162
163
-
There are few things that Azure App Service Static App can assume--things like automatically installing npm modules and running `npm run build`. There are also few you have to be explicit about, like what folder will the static app be copied to after build so the static site can be served from there.
163
+
Azure Static Web Apps is built to automatically carry out common tasks like installing npm modules and running `npm run build` during each deployment. There are, however, a few settings like the application sourcefolder and the build destination folder that you need to configure manually.
164
164
165
165
1. Click on the **Build** tab to configure the static output folder.
166
166
@@ -175,21 +175,21 @@ There are few things that Azure App Service Static App can assume--things like a
175
175
1. Once the deployment is completed, click **Go to resource**
176
176
1. On the resource screen, click the *URL* link to open your deployed application.
177
177
178
-
## How to handle dynamic routes
178
+
## Configure dynamic routes
179
179
180
-
Head to your newlydeployed site and click on one of the frameworks/libraries. Instead of getting a details page, this time, you get a 404 error page:
180
+
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.
181
181
182
182

183
183
184
-
The reason for this error is, when we generated a static site with Next.js, we only did so forthe home page:
184
+
The reason for this error is because Next.jsonly generated the home page based on the application configuration.
185
185
186
186
```javascript
187
187
module.exports = {
188
188
exportTrailingSlash: true,
189
189
exportPathMap: function() {
190
-
return {
191
-
'/': { page: '/' }
192
-
};
190
+
return {
191
+
'/': { page: '/' }
192
+
};
193
193
}
194
194
};
195
195
```
@@ -211,8 +211,8 @@ module.exports = {
211
211
212
212
projects.forEach((project) => {
213
213
paths[`/project/${project.slug}`] = {
214
-
page: '/project/[path]',
215
-
query: { path: project.slug },
214
+
page: '/project/[path]',
215
+
query: { path: project.slug },
216
216
};
217
217
});
218
218
@@ -222,16 +222,12 @@ module.exports = {
222
222
```
223
223
224
224
> [!NOTE]
225
-
> `exportPathMap` is an async function, so you can make a request to an API in this function and use the returned list to generate the paths.
225
+
> The `exportPathMap` function is an async function, so you can make a request to an API in this function and use the returned list to generate the paths.
226
226
227
-
1. Push the new changes to your GitHub repository and wait for a few minutes while GitHub Actions builds your site again. After the build, the 404 error will be gone:
227
+
1. Push the new changes to your GitHub repository and wait for a few minutes while GitHub Actions builds your site again. After the build is complete, the 404 error disappears.
228
228
229
229

230
230
231
-
## Summary
232
-
233
-
In this *tutorial*, you created a Next.js static website with dynamic routes and deployed it to App Service Static Site using GitHub Actions.
234
231
235
-
## Next Steps
236
232
> [!div class="nextstepaction"]
237
-
> [Set up a custom domain in app service static apps](custom-domain.md)
0 commit comments