title | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|
Tutorial: Deploy static-rendered Next.js websites on Azure Static Web Apps |
Generate and deploy Next.js dynamic sites with Azure Static Web Apps. |
static-web-apps |
craigshoemaker |
static-web-apps |
tutorial |
03/26/2022 |
cshoe |
devx-track-js |
In this tutorial, you learn to deploy a Next.js generated static website to Azure Static Web Apps. For more information about Next.js specifics, see the starter template's readme.
- An Azure account with an active subscription. Create an account for free.
- A GitHub account. Create an account for free.
- Node.js installed.
Rather than using the Next.js CLI to create your app, you can use a starter repository. The starter repository contains an existing Next.js application that supports dynamic routes.
To begin, create a new repository under your GitHub account from a template repository.
-
Navigate to https://github.com/staticwebdev/nextjs-starter/generate
-
Name the repository nextjs-starter
-
Next, clone the new repo to your machine. Make sure to replace
<YOUR_GITHUB_ACCOUNT_NAME>
with your account name.git clone http://github.com/<YOUR_GITHUB_ACCOUNT_NAME>/nextjs-starter
-
Navigate to the newly cloned Next.js app:
cd nextjs-starter
-
Install dependencies:
npm install
-
Start Next.js app in development:
npm run dev
Navigate to http://localhost:3000
to open the app, where you should see the following website open in your preferred browser:
:::image type="content" source="media/deploy-nextjs/start-nextjs-app.png" alt-text="Start Next.js app":::
When you select a framework or library, you see a details page about the selected item:
:::image type="content" source="media/deploy-nextjs/start-nextjs-details.png" alt-text="Details page":::
The following steps show how to link your app to Azure Static Web Apps. Once in Azure, you can deploy the application to a production environment.
-
Navigate to the Azure portal.
-
Select Create a Resource.
-
Search for Static Web Apps.
-
Select Static Web Apps.
-
Select Create.
-
On the Basics tab, enter the following values.
Property Value Subscription Your Azure subscription name. Resource group my-nextjs-group Name my-nextjs-app Plan type Free Region for Azure Functions API and staging environments Select a region closest to you. Source GitHub -
Select Sign in with GitHub and authenticate with GitHub.
-
Enter the following GitHub values.
Property Value Organization Select the appropriate GitHub organization. Repository Select nextjs-starter. Branch Select main. -
In the Build Details section, select Custom from the Build Presets. Add the following values as for the build configuration.
Property Value App location Enter / in the box. Api location Leave this box empty. Output location Enter out in the box.
-
Select the Review + Create button to verify the details are all correct.
-
Select Create to start the creation of the App Service Static Web App and provision a GitHub Actions for deployment.
-
Once the deployment completes select, Go to resource.
-
On the Overview window, select the URL link to open your deployed application.
If the website doesn't load immediately, then the build is still running. Once the workflow is complete, you can refresh the browser to view your web app.
To check the status of the Actions workflow, navigate to the Actions dashboard for your repository:
https://github.com/<YOUR_GITHUB_USERNAME>/nextjs-starter/actions
Now any changes made to the main
branch start a new build and deployment of your website.
When you created the app, Azure Static Web Apps created a GitHub Actions file in your repository. Synchronize with the server by pulling down the latest to your local repository.
Return to the terminal and run the following command git pull origin main
.
If you're not going to continue to use this application, you can delete the Azure Static Web Apps instance through the following steps:
- Open the Azure portal.
- Search for my-nextjs-group from the top search bar.
- Select on the group name.
- Select on the Delete button.
- Select Yes to confirm the delete action.
[!div class="nextstepaction"] Set up a custom domain