Skip to content

Commit 5a5b639

Browse files
Merge pull request #273446 from craigshoemaker/swa/nexjs-backend
[Static Web Apps] Update: Add Nextjs backend include
2 parents abdef14 + 72bda56 commit 5a5b639

File tree

4 files changed

+75
-41
lines changed

4 files changed

+75
-41
lines changed

articles/static-web-apps/deploy-nextjs-hybrid.md

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services: static-web-apps
55
author: aaronpowell
66
ms.service: static-web-apps
77
ms.topic: tutorial
8-
ms.date: 10/12/2022
8+
ms.date: 04/25/2024
99
ms.author: aapowell
1010
ms.custom: devx-track-js
1111
---
@@ -20,10 +20,12 @@ In this tutorial, you learn to deploy a [Next.js](https://nextjs.org) website to
2020
2121
## Prerequisites
2222

23-
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/).
24-
- A GitHub account. [Create an account for free](https://github.com/join).
25-
- [Node.js](https://nodejs.org) installed.
26-
- [Next.js CLI](https://nextjs.org/docs/getting-started) installed. Refer to the [Next.js Getting Started guide](https://nextjs.org/docs/getting-started) for details.
23+
| Resource | Description |
24+
|---|---|
25+
| [Azure account](https://azure.microsoft.com/free/) | If you don't have an Azure account with an active subscription, you can [create one for free](https://azure.microsoft.com/free/). |
26+
| [GitHub account](https://github.com/join) | If you don't have a GitHub account, you can [create an account for free](https://github.com/join). |
27+
| [Node.js](https://nodejs.org) | Install the latest version of Node.js. |
28+
| [Next.js CLI](https://nextjs.org/docs/getting-started) | Install the latest version of the Next.js CLI. See the [Next.js Getting Started guide](https://nextjs.org/docs/getting-started) for details. |
2729

2830
[!INCLUDE [Unsupported Next.js features](../../includes/static-web-apps-nextjs-unsupported.md)]
2931

@@ -110,19 +112,23 @@ Once GitHub Actions workflow is complete, you can select the URL link to open th
110112

111113
## Set up your Next.js project locally to make changes
112114

113-
1. Clone the new repo to your machine. Make sure to replace <YOUR_GITHUB_ACCOUNT_NAME> with your account name.
115+
1. Clone the new repo to your machine. Make sure to replace <GITHUB_ACCOUNT_NAME> with your account name.
114116

115117
```bash
116-
git clone http://github.com/<YOUR_GITHUB_ACCOUNT_NAME>/my-first-static-web-app
118+
git clone http://github.com/<GITHUB_ACCOUNT_NAME>/my-first-static-web-app
117119
```
118120

119121
1. Open the project in Visual Studio Code or your preferred code editor.
120122

123+
## Set up server side rendering
124+
125+
[!INCLUDE [Server side rendering](../../includes/static-web-apps/static-web-apps-nextjs-backends.md)]
126+
121127
## Add Server-Rendered data with a Server Component
122128

123-
To add server-rendered data in your Next.js project using the App Router, edit a Next.js component to add a server-side operations to render data in the component. By default, Next.js components are [Server Components](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) that can be server-rendered.
129+
To add server-rendered data in your Next.js project using the App Router, edit a Next.js component to add a server-side operation to render data in the component. By default, Next.js components are [Server Components](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) that can be server-rendered.
124130

125-
1. Open the `app/page.tsx` file and add an operation that sets the value of a variable, which is computed server-side. Examples include fetching data or other server operations.
131+
1. Open the `app/page.tsx` file and add an operation that sets the value of a server-side computed variable. Examples include fetching data or other server operations.
126132

127133
```ts
128134
export default function Home() {
@@ -134,7 +140,7 @@ To add server-rendered data in your Next.js project using the App Router, edit a
134140
```
135141

136142
1. Import `unstable_noStore` from `next/cache` and call it within the `Home` component to ensure the route is dynamically rendered.
137-
143+
138144
```ts
139145
import { unstable_noStore as noStore } from 'next/cache';
140146
@@ -151,7 +157,7 @@ To add server-rendered data in your Next.js project using the App Router, edit a
151157
>[!NOTE]
152158
>This example forces dynamic rendering of this component to demonstrate server-rendering of the server's current time. The App Router model of Next.js recommends caching individual data requests to optimize the performance of your Next.js app. Read more on [data fetching and caching in Next.js](https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating).
153159
154-
1. Update the `Home` component in _app/pages.tsx_ to render the server-side data.
160+
1. Update the `Home` component in _app/pages.tsx_ to render the server-side data.
155161
156162
```ts
157163
import { unstable_noStore as noStore } from 'next/cache';
@@ -163,8 +169,7 @@ To add server-rendered data in your Next.js project using the App Router, edit a
163169
return(
164170
<main className="flex min-h-screen flex-col items-center justify-between p-24">
165171
<div>
166-
This is a Next.js application hosted on Azure Static Web Apps with
167-
hybrid rendering. The time on the server is <strong>{timeOnServer}</strong>.
172+
This is a Next.js application hosted on Azure Static Web Apps with hybrid rendering. The time on the server is <strong>{timeOnServer}</strong>.
168173
</div>
169174
</main>
170175
);
@@ -173,11 +178,12 @@ To add server-rendered data in your Next.js project using the App Router, edit a
173178
174179
## Adding an API route
175180
176-
In addition to Server Components, Next.js provides [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) you can use to create API routes to your Next.js application. These APIs can be fetched in [Client Components](https://nextjs.org/docs/app/building-your-application/rendering/client-components).
181+
In addition to Server Components, Next.js provides [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers) you can use to create API routes to your Next.js application. You can fetch these APIs in [Client Components](https://nextjs.org/docs/app/building-your-application/rendering/client-components).
177182
178183
Begin by adding an API route.
179184
180185
1. Create a new file at `app/api/currentTime/route.tsx`. This file holds the Route Handler for the new API endpoint.
186+
181187
1. Add a handler function to return data from the API.
182188
183189
```ts
@@ -195,6 +201,7 @@ Begin by adding an API route.
195201
```
196202
197203
1. Create a new file at `app/components/CurrentTimeFromAPI.tsx`. This component creates a container for the Client Component that fetches the API from the browser.
204+
198205
1. Add a client component that fetches the API in this file.
199206
200207
```ts
@@ -239,22 +246,21 @@ This Client Component fetches the API with a `useEffect` React hook to render th
239246
return(
240247
<main className="flex min-h-screen flex-col items-center justify-between p-24">
241248
<div>
242-
This is a Next.js application hosted on Azure Static Web Apps with
243-
hybrid rendering. The time on the server is <strong>{timeOnServer}</strong>.
249+
This is a Next.js application hosted on Azure Static Web Apps with hybrid rendering. The time on the server is <strong>{timeOnServer}</strong>.
244250
</div>
245251
<CurrentTimeFromAPI />
246252
</main>
247253
);
248254
}
249255
```
250-
256+
251257
1. The result from the API route is displayed on the page.
252258
253259
:::image type="content" source="media/deploy-nextjs/nextjs-13-home-display.png" alt-text="Screenshot showing the display the output from the API route.":::
254260
255261
## Configure the runtime version for Next.js
256262
257-
Certain Next.js versions require specific Node.js versions. To configure a specific Node version, you can set the 'engines' property of your `package.json` file to designate a version.
263+
Certain Next.js versions require specific Node.js versions. To configure a specific Node version, you can set the `engines` property of your `package.json` file to designate a version.
258264
259265
```json
260266
{
@@ -267,7 +273,7 @@ Certain Next.js versions require specific Node.js versions. To configure a speci
267273
268274
## Set environment variables for Next.js
269275
270-
Next.js uses environment variables at build time and at request time, to support both static page generation and dynamic page generation with server-side rendering. Therefore, set environment variables both within the build and deploy task, and in the _Environment variables_ of your Azure Static Web Apps resource.
276+
Next.js uses environment variables at build time and at request time, to support both static page generation and dynamic page generation with server-side rendering. Therefore, set environment variables both within the build and deploy task, and in the _Environment variables_ of your Azure Static Web Apps resource.
271277
272278
```yml
273279
...
@@ -292,18 +298,20 @@ Next.js uses environment variables at build time and at request time, to support
292298
293299
## Enable standalone feature
294300
295-
When your application size exceeds 250Mb, the Next.js [Output File Tracing](https://nextjs.org/docs/advanced-features/output-file-tracing) feature helps optimize the app size and enhance performance.
301+
When your application size exceeds 250 MB, the Next.js [Output File Tracing](https://nextjs.org/docs/advanced-features/output-file-tracing) feature helps optimize the app size and enhance performance.
302+
303+
Output File Tracing creates a compressed version of the whole application with necessary package dependencies. This package is built into a folder named _.next/standalone_. With this package, your app can deploy on its own without _node_modules_ dependencies.
296304
297-
Output File Tracing creates a compressed version of the whole application with necessary package dependencies built into a folder named *.next/standalone*. This folder is meant to deploy on its own without additional *node_modules* dependencies.
305+
In order to enable the `standalone` feature, add the following property to your `next.config.js`:
298306
299-
In order to enable the `standalone` feature, add the following additional property to your `next.config.js`:
300307
```js
301308
module.exports ={
302309
output:"standalone",
303310
}
304311
```
305312
306-
You will also need to configure the `build` command in the `package.json` file in order to copy static files to your standalone output.
313+
Next, configure the `build` command in the `package.json` file in order to copy static files to your standalone output.
314+
307315
```json
308316
{
309317
...
@@ -316,9 +324,9 @@ You will also need to configure the `build` command in the `package.json` file i
316324
}
317325
```
318326
319-
## Configure your Next.js routing and middleware for deployment to Azure Static Web Apps
327+
## Configure routing and middleware for deployment
320328
321-
Your Next.js project can be configured to have custom handling of routes with redirects, rewrites, and middleware. These handlers are commonly used for authentication, personalization, routing, and internationalization. Custom handling affects the default routing of your Next.js site and the configuration must be compatible with hosting on Static Web Apps.
329+
You can configure your Next.js project handle of routes with custom redirects, rewrites, and middleware. These handlers are commonly used for authentication, personalization, routing, and internationalization. Custom handling affects the default routing of your Next.js site and the configuration must be compatible with hosting on Static Web Apps.
322330
323331
Static Web Apps validates that your Next.js site is successfully deployed by adding a page to your site at build time. The page is named `public/.swa/health.html`, and Static Web Apps verifies the successful startup and deployment of your site by navigating to `/.swa/health.html` and verifying a successful response. Middleware and custom routing, which includes redirects and rewrites, can affect the access of the `/.swa/health.html` path, which can prevent Static Web Apps' deployment validation. To configure middleware and routing for a successful deployment to Static Web Apps, follow these steps:
324332
@@ -336,7 +344,7 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
336344
}
337345
```
338346
339-
1. Configure your redirects in `next.config.js` to exclude routes starting with `.swa`
347+
1. Configure your redirects in `next.config.js` to exclude routes starting with `.swa`.
340348
341349
```js
342350
module.exports = {
@@ -352,7 +360,7 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
352360
};
353361
```
354362
355-
1. Configure your rewrites in `next.config.js` to exclude routes starting with `.swa`
363+
1. Configure your rewrite rules in `next.config.js` to exclude routes starting with `.swa`.
356364
357365
```js
358366
module.exports = {
@@ -368,11 +376,12 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
368376
},
369377
};
370378
```
371-
These code snippets exclude paths that start with `.swa` from being handled by your custom routing or middleware. These rules ensure that the paths resolve as expected during deployment validation.
379+
380+
These code snippets exclude paths that start with `.swa` to stop your custom routing or middleware from processing these requests. These rules ensure that the paths resolve as expected during deployment validation.
372381
373382
## Enable logging for Next.js
374383
375-
Following best practices for Next.js server API troubleshooting, add logging to the API to catch these errors. Logging on Azure uses **Application Insights**. In order to preload this SDK, you need to create a custom start up script. To learn more:
384+
Following best practices for Next.js server API troubleshooting, add logging to the API to catch these errors. Logging on Azure uses **Application Insights**. In order to preload this SDK, you need to create a custom startup script. To learn more:
376385
377386
* [Example preload script for Application Insights + Next.js](https://medium.com/microsoftazure/enabling-the-node-js-application-insights-sdk-in-next-js-746762d92507)
378387
* [GitHub issue](https://github.com/microsoft/ApplicationInsights-node.js/issues/808)

articles/static-web-apps/nextjs.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services: static-web-apps
55
author: aaronpowell
66
ms.service: static-web-apps
77
ms.topic: how-to
8-
ms.date: 10/12/2022
8+
ms.date: 04/25/2024
99
ms.author: aapowell
1010
ms.custom: devx-track-js
1111
---
@@ -14,13 +14,10 @@ ms.custom: devx-track-js
1414

1515
Next.js support on Azure Static Web Apps can be categorized as two deployment models:
1616

17-
- **Hybrid**: Hybrid Next.js sites, which includes support for all Next.js features such as the [App Router](https://nextjs.org/docs/app), the [Pages Router](https://nextjs.org/docs/pages) and [React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components)
18-
17+
- **Hybrid**: Hybrid Next.js sites, which include support for all Next.js features such as the [App Router](https://nextjs.org/docs/app), the [Pages Router](https://nextjs.org/docs/pages) and [React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components)
1918

2019
- **Static**: Static Next.js sites, which use the [Static HTML Export](https://nextjs.org/docs/advanced-features/static-html-export) option of Next.js.
2120

22-
23-
2421
## Hybrid Next.js applications (preview)
2522

2623
Static Web Apps supports deploying hybrid Next.js websites. This enables support for all Next.js features, such as the [App Router](https://nextjs.org/docs/app) and [React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components).
@@ -31,7 +28,6 @@ With hybrid Next.js applications, pages and components can be dynamically render
3128

3229
Key features that are available in the preview are:
3330

34-
3531
- [App Router](https://nextjs.org/docs/app) and [Pages Router](https://nextjs.org/docs/pages)
3632
- [React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components)
3733
- [Hybrid rendering](https://nextjs.org/docs/app/building-your-application/rendering/server-components#server-rendering-strategies)
@@ -45,6 +41,9 @@ Follow the [deploy hybrid Next.js applications](deploy-nextjs-hybrid.md) tutoria
4541

4642
[!INCLUDE [Unsupported Next.js features](../../includes/static-web-apps-nextjs-unsupported.md)]
4743

44+
## Server side rendering
45+
46+
[!INCLUDE [Server side rendering](../../includes/static-web-apps/static-web-apps-nextjs-backends.md)]
4847

4948
## Static HTML export
5049

includes/static-web-apps-nextjs-unsupported.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
author: craigshoemaker
33
ms.service: static-web-apps
44
ms.topic: include
5-
ms.date: 10/26/2022
5+
ms.date: 06/12/2024
66
ms.author: cshoe
77
---
88

99
### Unsupported features in preview
1010

1111
The following features of Static Web Apps are unsupported for Next.js with hybrid rendering:
1212

13-
- Linked APIs using Azure Functions, Azure App Service, Azure Container Apps, or Azure API Management.
14-
- SWA CLI local emulation and deployment.
15-
- Partial support for `staticwebapp.config.json` file.
13+
- **Select Azure services**: Linked APIs using Azure Functions, Azure App Service, Azure Container Apps, or Azure API Management.
14+
- **SWA CLI features**: SWA CLI local emulation and deployment.
15+
- **Partial features support**: The following properties in `staticwebapp.config.json` file aren't supported:
1616
- Navigation fallback is unsupported.
1717
- Route rewrites to routes within the Next.js application must be configured within `next.config.js`.
1818
- The configuration within the `staticwebapp.config.json` file takes precedence over the configuration within `next.config.js`.
1919
- Configuration for the Next.js site should be handled using `next.config.js` for full feature compatibility.
20-
- `skip_app_build` and `skip_api_build` can't be used within the `Azure/static-web-apps-deploy@v1` deployment image.
21-
- Incremental static regeneration (ISR) doesn't support caching images.
20+
- **Build skipping**: The `skip_app_build` and `skip_api_build` features aren't supported in the `Azure/static-web-apps-deploy@v1` deployment image.
21+
- **Incremental static regeneration (ISR)**: Image caching isn't supported.
2222

2323
> [!NOTE]
2424
> The maximum app size for the hybrid Next.js application is 250 MB. Use [standalone](../articles/static-web-apps/deploy-nextjs-hybrid.md#enable-standalone-feature) feature by Next.js for optimized app sizes. If this is not sufficient, consider using [Static HTML exported Next.js](../articles/static-web-apps/deploy-nextjs-static-export.md) if your app size requirement is more than 250 MB.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
author: craigshoemaker
3+
ms.service: static-web-apps
4+
ms.topic: include
5+
ms.date: 06/11/2024
6+
ms.author: cshoe
7+
---
8+
9+
A managed backed is automatically available for every hybrid Next.js deployment in all plans. However, you can fine- tune performance and take more control of the backend by assigning a custom backend to your site. If you switch between a managed backend to a linked backend, your site experiences no downtime.
10+
11+
The following steps show you how to associate a custom backend to your Standard plan and above static web apps.
12+
13+
> [!NOTE]
14+
> Linked backends are only available for sites using the Standard plan or above.
15+
16+
1. Go to your static web app in the Azure portal.
17+
18+
1. Select **Settings** and then **APIs** from the side menu.
19+
20+
1. Select **Configure linked backend**.
21+
22+
1. Either create a new App Service Plan or select an existing App Service Plan.
23+
24+
Your selected App Service Plan must use at least an **S1** SKU.
25+
26+
1. Click **Link**.

0 commit comments

Comments
 (0)