Skip to content

Commit ab3af6f

Browse files
Merge pull request #111285 from christiannwamba/chnwamba-marmalade-next-nuxt
Tutorial: Deploy Next.js / Nuxt.js to Static App
2 parents 54ab378 + 20bfd82 commit ab3af6f

16 files changed

+370
-288
lines changed

articles/static-apps/deploy-nextjs.md

Lines changed: 195 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: "Tutorial: Deploy server-rendered Next.js websites on App Service Static Apps"
3-
description: #Required; article description that is displayed in search results.
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."
44
services: #Required for articles that deal with a service; service slug assigned to your service by ACOM.
55
author: christiannwamba
66
ms.service: azure-functions
@@ -9,170 +9,221 @@ ms.date: 05/08/2020
99
ms.author: chnwamba
1010
---
1111

12-
<!--
13-
Refer to the following guide for more details:
14-
https://review.docs.microsoft.com/en-us/help/contribute/contribute-how-to-mvc-tutorial?branch=master
15-
-->
1612

17-
<!---Recommended: Removal all the comments in this template before you sign-off or merge to master.--->
13+
# Deploy server-rendered Next.js websites on Azure Static Web Apps
1814

19-
# Tutorial: Deploy server-rendered Next.js websites on App Service Static Apps
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
2016

21-
<!---Required:
22-
Starts with "Tutorial: "
23-
Make the first word following "Tutorial:" a verb.
24-
--->
17+
## Prerequisites
2518

26-
Introductory paragraph.
19+
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/).
20+
- A GitHub account. [Create an account for free](https://github.com/join).
21+
- [Node.js](https://nodejs.org) installed.
2722

28-
<!---Required:
29-
Lead with a light intro that describes, in customer-friendly language,
30-
what the customer will learn, or do, or accomplish. Answer the
31-
fundamental "why would I want to do this?" question.
32-
--->
23+
## Set up a Next.js app
3324

34-
In this tutorial, you learn how to:
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.
3526

36-
> [!div class="checklist"]
37-
> * All tutorials include a list summarizing the steps to completion
38-
> * Each of these bullet points align to a key H2
39-
> * Use these green checkboxes in a tutorial
40-
<!---Required:
41-
The outline of the tutorial should be included in the beginning and at
42-
the end of every tutorial. These will align to the **procedural** H2
43-
headings for the activity. You do not need to include all H2 headings.
44-
Leave out the prerequisites, clean-up resources and next steps--->
27+
To begin, create a new repository under your GitHub account from a template repository.
4528

46-
If you don't have a <service> subscription, create a free trial account...
47-
<!--- Required, if a free trial account exists
48-
Because tutorials are intended to help new customers use the product or
49-
service to complete a top task, include a link to a free trial before the
50-
first H2, if one exists. You can find listed examples in
51-
[Write tutorials](contribute-how-to-mvc-tutorial.md)
52-
--->
29+
1. Navigate to <http://github.com/staticwebdev/nextjs-starter/generate>
30+
1. Name the repository **nextjs-starter**
31+
1. Next, clone the new repo to your machine. Make sure to replace `<YOUR_GITHUB_ACCOUNT_NAME>` with your account name.
5332

54-
<!---Avoid notes, tips, and important boxes. Readers tend to skip over
55-
them. Better to put that info directly into the article text.--->
33+
```bash
34+
git clone http://github.com/<YOUR_GITHUB_ACCOUNT_NAME>/nextjs-starter
35+
```
5636

57-
## Prerequisites
37+
1. Navigate to the newly cloned Next.js app:
5838

59-
- First prerequisite
60-
- Second prerequisite
61-
- Third prerequisite
62-
<!---If you need them, make Prerequisites your first H2 in a tutorial. If
63-
there's something a customer needs to take care of before they start (for
64-
example, creating a VM) it's OK to link to that content before they
65-
begin.--->
66-
67-
## Sign in to <service/product/tool name>
68-
69-
Sign in to the <service> portal.
70-
<!---If you need to sign in to the portal to do the tutorial, this H2 and
71-
link are required.--->
72-
73-
## Procedure 1
74-
75-
<!---Required:
76-
Tutorials are prescriptive and guide the customer through an end-to-end
77-
procedure. Make sure to use specific naming for setting up accounts and
78-
configuring technology.
79-
Don't link off to other content - include whatever the customer needs to
80-
complete the scenario in the article. For example, if the customer needs
81-
to set permissions, include the permissions they need to set, and the
82-
specific settings in the tutorial procedure. Don't send the customer to
83-
another article to read about it.
84-
In a break from tradition, do not link to reference topics in the
85-
procedural part of the tutorial when using cmdlets or code. Provide customers what they need to know in the tutorial to successfully complete
86-
the tutorial.
87-
For portal-based procedures, minimize bullets and numbering.
88-
For the CLI or PowerShell based procedures, don't use bullets or
89-
numbering.
90-
--->
91-
92-
Include a sentence or two to explain only what is needed to complete the
93-
procedure.
94-
95-
1. Step 1 of the procedure
96-
1. Step 2 of the procedure
97-
1. Step 3 of the procedure
98-
99-
<!---Use screenshots but be judicious to maintain a reasonable length.
100-
Make sure screenshots align to the
101-
[current standards](https://review.docs.microsoft.com/help/contribute/contribute-how-to-create-screenshot?branch=master).
102-
If users access your product/service via a web browser the first
103-
screenshot should always include the full browser window in Chrome or
104-
Safari. This is to show users that the portal is browser-based - OS
105-
and browser agnostic.--->
106-
1. Step 4 of the procedure
107-
108-
## Procedure 2
109-
110-
Include a sentence or two to explain only what is needed to complete the procedure.
111-
112-
1. Step 1 of the procedure
113-
1. Step 2 of the procedure
114-
1. Step 3 of the procedure
115-
116-
## Procedure 3
117-
118-
Include a sentence or two to explain only what is needed to complete the
119-
procedure.
120-
<!---Code requires specific formatting. Here are a few useful examples of
121-
commonly used code blocks. Make sure to use the interactive functionality
122-
where possible.
123-
124-
For the CLI or PowerShell based procedures, don't use bullets or
125-
numbering.
126-
--->
127-
128-
Here is an example of a code block for Java:
129-
130-
```java
131-
cluster = Cluster.build(new File("src/remote.yaml")).create();
132-
...
133-
client = cluster.connect();
134-
```
39+
```bash
40+
cd nextjs-starter
41+
```
13542

136-
or a code block for Azure CLI:
43+
1. Install dependencies:
13744

138-
```azurecli-interactive
139-
az vm create --resource-group myResourceGroup --name myVM --image win2016datacenter --admin-username azureuser --admin-password myPassword12
140-
```
45+
```bash
46+
npm install
47+
```
48+
49+
1. Start Next.js app in development:
50+
51+
```bash
52+
npm run dev
53+
```
54+
55+
Navigate to <http://localhost:3000> to open the app, where you should see the following website open in your preferred browser:
56+
57+
:::image type="content" source="media/deploy-nextjs/start-nextjs-app.png" alt-text="Start Next.js app":::
58+
59+
When you click on a framework/library, you should see a details page about the selected item:
60+
61+
:::image type="content" source="media/deploy-nextjs/start-nextjs-details.png" alt-text="Details page":::
62+
63+
## Generate a static website from Next.js build
64+
65+
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.
66+
67+
1. To configure static routes, create file named _next.config.js_ at the root of your project and add the following code..
68+
69+
```javascript
70+
module.exports = {
71+
exportTrailingSlash: true,
72+
exportPathMap: function() {
73+
return {
74+
'/': { page: '/' }
75+
};
76+
}
77+
};
78+
```
79+
80+
This configuration maps `/` to the Next.js page that is served for the `/` route, and that is the _pages/index.js_ page file.
81+
82+
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.
83+
84+
```json
85+
"scripts": {
86+
"dev": "next dev",
87+
"build": "next build && next export",
88+
},
89+
```
90+
91+
Now with this command in place, Static Web Apps will run the `build` script every time you push a commit.
92+
93+
1. Generate a static site:
94+
95+
```bash
96+
npm run build
97+
```
98+
99+
The static site is generated and copied into an _out_ folder at the root of your working directory.
100+
101+
> [!NOTE]
102+
> This folder is listed in the _.gitignore_ file because it should be generated by CI/CD when you deploy.
103+
104+
## Push your static website to GitHub
105+
106+
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 commands sync your changes to GitHub.
107+
108+
1. Stage all changed files
109+
```bash
110+
git add .
111+
```
112+
1. Commit all changes
113+
```bash
114+
git commit -m "Update build config"
115+
```
116+
117+
1. Push your changes to GitHub.
118+
119+
```bash
120+
git push origin master
121+
```
141122
142-
or a code block for Azure PowerShell:
123+
## Deploy your static website
143124
144-
```azurepowershell-interactive
145-
New-AzureRmContainerGroup -ResourceGroupName myResourceGroup -Name mycontainer -Image microsoft/iis:nanoserver -OsType Windows -IpAddressType Public
125+
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.
126+
127+
### Create a static app
128+
129+
1. Navigate to the [Azure portal](https://portal.azure.com).
130+
1. Click **Create a Resource** then search for **Static Web Apps** and select the matching result.
131+
132+
133+
1. Select a subscription from the *Subscription* drop-down list or use the default value.
134+
1. Click the **New** link below the *Resource group* drop-down. In *New resource group name*, type **mystaticsite** and click **OK**
135+
1. Provide a globally unique name for your app in the **Name** text box. Valid characters include `a-z`, `A-Z`, `0-9`, and `-`. This value is used as the URL prefix for your static app in the format of `https://<APP_NAME>.azurestaticapps.net`.
136+
1. In the *Region* drop-down, choose a region closest to you.
137+
1. Select **Free** from the SKU drop-down.
138+
139+
140+
:::image type="content" source="media/deploy-nextjs/create-static-web-app.png" alt-text="Create Static Web App":::
141+
142+
### Add a GitHub repository
143+
144+
The new Static Web Apps account needs access to the repository with your Next.js app so it can automatically deploy commits.
145+
146+
1. Click the **Sign in with GitHub button**
147+
1. Select the **Organization** under which you created the repo for your Next.js project, which may be your GitHub username.
148+
1. Find and select the name of the repository you created earlier.
149+
1. Choose **master** as the branch from the *Branch* drop-down.
150+
151+
:::image type="content" source="media/deploy-nextjs/connect-github.png" alt-text="Connect GitHub":::
152+
153+
### Configure the build process
154+
155+
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 source folder and the build destination folder that you need to configure manually.
156+
157+
1. Click on the **Build** tab to configure the static output folder.
158+
159+
:::image type="content" source="media/deploy-nextjs/build-tab.png" alt-text="Build tab":::
160+
161+
2. Type **out** in the *App artifact location* text box.
162+
163+
### Review and create
164+
165+
1. Click the **Review + Create** button to verify the details are all correct.
166+
1. Click **Create** to start the creation of the resource and also provision a GitHub Action for deployment.
167+
1. Once the deployment is completed, click **Go to resource**
168+
1. On the _Overview_ window, click the *URL* link to open your deployed application.
169+
170+
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.
171+
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.
172+
173+
You can check the status of the Actions workflows by navigating to the Actions for your repository:
174+
175+
```url
176+
https://github.com/<YOUR_GITHUB_USERNAME>/nextjs-starter/actions
146177
```
147178
179+
### Sync changes
180+
181+
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.
182+
183+
Return to the terminal and run the following command `git pull origin master`.
184+
185+
## Configure dynamic routes
186+
187+
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.
188+
189+
:::image type="content" source="media/deploy-nextjs/404-in-production.png" alt-text="404 on dynamic routes":::
190+
191+
The reason for this error is because Next.js only generated the home page based on the application configuration.
192+
193+
## Generate static pages from dynamic routes
148194
149-
## Clean up resources
195+
1. Update the _next.config.js_ file so that Next.js uses a list of all available data to generate static pages for each framework/library:
150196
151-
If you're not going to continue to use this application, delete
152-
<resources> with the following steps:
197+
```javascript
198+
const data = require('./utils/projectsData');
153199
154-
1. From the left-hand menu...
155-
2. ...click Delete, type...and then click Delete
200+
module.exports = {
201+
exportTrailingSlash: true,
202+
exportPathMap: async function () {
203+
const { projects } = data;
204+
const paths = {
205+
'/': { page: '/' },
206+
};
207+
208+
projects.forEach((project) => {
209+
paths[`/project/${project.slug}`] = {
210+
page: '/project/[path]',
211+
query: { path: project.slug },
212+
};
213+
});
214+
215+
return paths;
216+
},
217+
};
218+
```
156219
157-
<!---Required:
158-
To avoid any costs associated with following the tutorial procedure, a
159-
Clean up resources (H2) should come just before Next steps (H2)
160-
--->
220+
> [!NOTE]
221+
> 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.
161222
162-
## Next steps
223+
2. 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.
163224
164-
<!-- Uncomment this block and add the appropriate link
225+
226+
:::image type="content" source="media/deploy-nextjs/404-in-production-fixed.png" alt-text="404 on dynamic routes fixed":::
165227
166228
> [!div class="nextstepaction"]
167-
> [Next steps button](contribute-get-started-mvc.md)
168-
169-
-->
170-
171-
<!--- Required:
172-
Tutorials should always have a Next steps H2 that points to the next
173-
logical tutorial in a series, or, if there are no other tutorials, to
174-
some other cool thing the customer can do. A single link in the blue box
175-
format should direct the customer to the next article - and you can
176-
shorten the title in the boxes if the original one doesn't fit.
177-
Do not use a "More info section" or a "Resources section" or a "See also
178-
section". --->
229+
> [Set up a custom domain](custom-domain.md)

0 commit comments

Comments
 (0)