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-web-apps/deploy-nextjs-hybrid.md
+38-29Lines changed: 38 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ services: static-web-apps
5
5
author: aaronpowell
6
6
ms.service: static-web-apps
7
7
ms.topic: tutorial
8
-
ms.date: 10/12/2022
8
+
ms.date: 04/25/2024
9
9
ms.author: aapowell
10
10
ms.custom: devx-track-js
11
11
---
@@ -20,10 +20,12 @@ In this tutorial, you learn to deploy a [Next.js](https://nextjs.org) website to
20
20
21
21
## Prerequisites
22
22
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. |
1. Open the project in Visual Studio Code or your preferred code editor.
120
122
123
+
## Set up server side rendering
124
+
125
+
[!INCLUDE [Server side rendering](../../includes/static-web-apps/static-web-apps-nextjs-backends.md)]
126
+
121
127
## Add Server-Rendered data with a Server Component
122
128
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.
124
130
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.
126
132
127
133
```ts
128
134
export default functionHome() {
@@ -134,7 +140,7 @@ To add server-rendered data in your Next.js project using the App Router, edit a
134
140
```
135
141
136
142
1. Import `unstable_noStore` from `next/cache` and call it within the `Home` component to ensure the route is dynamically rendered.
137
-
143
+
138
144
```ts
139
145
import { unstable_noStore as noStore } from 'next/cache';
140
146
@@ -151,7 +157,7 @@ To add server-rendered data in your Next.js project using the App Router, edit a
151
157
>[!NOTE]
152
158
>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).
153
159
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.
155
161
156
162
```ts
157
163
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
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>.
168
173
</div>
169
174
</main>
170
175
);
@@ -173,11 +178,12 @@ To add server-rendered data in your Next.js project using the App Router, edit a
173
178
174
179
## Adding an API route
175
180
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).
177
182
178
183
Begin by adding an API route.
179
184
180
185
1. Create a new file at `app/api/currentTime/route.tsx`. This file holds the Route Handler for the new API endpoint.
186
+
181
187
1. Add a handler function to return data from the API.
182
188
183
189
```ts
@@ -195,6 +201,7 @@ Begin by adding an API route.
195
201
```
196
202
197
203
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
+
198
205
1. Add a client component that fetches the API in this file.
199
206
200
207
```ts
@@ -239,22 +246,21 @@ This Client Component fetches the API with a `useEffect` React hook to render th
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>.
244
250
</div>
245
251
<CurrentTimeFromAPI />
246
252
</main>
247
253
);
248
254
}
249
255
```
250
-
256
+
251
257
1. The result from the API route is displayed on the page.
252
258
253
259
:::image type="content" source="media/deploy-nextjs/nextjs-13-home-display.png" alt-text="Screenshot showing the display the output from the API route.":::
254
260
255
261
## Configure the runtime version for Next.js
256
262
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.
258
264
259
265
```json
260
266
{
@@ -267,7 +273,7 @@ Certain Next.js versions require specific Node.js versions. To configure a speci
267
273
268
274
## Set environment variables for Next.js
269
275
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.
271
277
272
278
```yml
273
279
...
@@ -292,18 +298,20 @@ Next.js uses environment variables at build time and at request time, to support
292
298
293
299
## Enable standalone feature
294
300
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.
296
304
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`:
298
306
299
-
In order to enable the `standalone` feature, add the following additional property to your `next.config.js`:
300
307
```js
301
308
module.exports ={
302
309
output:"standalone",
303
310
}
304
311
```
305
312
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
+
307
315
```json
308
316
{
309
317
...
@@ -316,9 +324,9 @@ You will also need to configure the `build` command in the `package.json` file i
316
324
}
317
325
```
318
326
319
-
## Configure your Next.js routing and middleware for deployment to Azure Static Web Apps
327
+
## Configure routing and middleware for deployment
320
328
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.
322
330
323
331
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:
324
332
@@ -336,7 +344,7 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
336
344
}
337
345
```
338
346
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`.
340
348
341
349
```js
342
350
module.exports = {
@@ -352,7 +360,7 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
352
360
};
353
361
```
354
362
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`.
356
364
357
365
```js
358
366
module.exports = {
@@ -368,11 +376,12 @@ Static Web Apps validates that your Next.js site is successfully deployed by add
368
376
},
369
377
};
370
378
```
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.
372
381
373
382
## Enable logging for Next.js
374
383
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:
376
385
377
386
* [Example preload script for Application Insights + Next.js](https://medium.com/microsoftazure/enabling-the-node-js-application-insights-sdk-in-next-js-746762d92507)
Copy file name to clipboardExpand all lines: articles/static-web-apps/nextjs.md
+5-6Lines changed: 5 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ services: static-web-apps
5
5
author: aaronpowell
6
6
ms.service: static-web-apps
7
7
ms.topic: how-to
8
-
ms.date: 10/12/2022
8
+
ms.date: 04/25/2024
9
9
ms.author: aapowell
10
10
ms.custom: devx-track-js
11
11
---
@@ -14,13 +14,10 @@ ms.custom: devx-track-js
14
14
15
15
Next.js support on Azure Static Web Apps can be categorized as two deployment models:
16
16
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)
19
18
20
19
-**Static**: Static Next.js sites, which use the [Static HTML Export](https://nextjs.org/docs/advanced-features/static-html-export) option of Next.js.
21
20
22
-
23
-
24
21
## Hybrid Next.js applications (preview)
25
22
26
23
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
31
28
32
29
Key features that are available in the preview are:
33
30
34
-
35
31
-[App Router](https://nextjs.org/docs/app) and [Pages Router](https://nextjs.org/docs/pages)
36
32
-[React Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components)
> 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.
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.
0 commit comments