Skip to content

Commit 3741e69

Browse files
Merge pull request #269175 from thomasgauvin/patch-33
Add deployment validation configuration fix for deploy-nextjs-hybrid.md
2 parents 45b0c4a + 28ab1dd commit 3741e69

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ This Client Component fetches the API with a `useEffect` React hook to render th
253253
254254
:::image type="content" source="media/deploy-nextjs/nextjs-13-home-display.png" alt-text="Screenshot showing the display the output from the API route.":::
255255
256-
257256
## Configure the runtime version for Next.js
258257
259258
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.
@@ -318,6 +317,60 @@ You will also need to configure the `build` command in the `package.json` file i
318317
}
319318
```
320319
320+
## Configure your Next.js routing and middleware for deployment to Azure Static Web Apps
321+
322+
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.
323+
324+
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:
325+
326+
1. Exclude routes starting with `.swa` in your `middleware.ts` (or `.js`) file in your middleware configuration.
327+
328+
```js
329+
export const config = {
330+
matcher: [
331+
/*
332+
* Match all request paths except for the ones starting with:
333+
* - .swa (Azure Static Web Apps)
334+
*/
335+
'/((?!.swa).*)',
336+
],
337+
}
338+
```
339+
340+
1. Configure your redirects in `next.config.js` to exclude routes starting with `.swa`
341+
342+
```js
343+
module.exports = {
344+
async redirects() {
345+
return [
346+
{
347+
source: '/((?!.swa).*)<YOUR MATCHING RULE>',
348+
destination: '<YOUR REDIRECT RULE>',
349+
permanent: false,
350+
},
351+
]
352+
},
353+
};
354+
```
355+
356+
1. Configure your rewrites in `next.config.js` to exclude routes starting with `.swa`
357+
358+
```js
359+
module.exports = {
360+
async rewrites() {
361+
return {
362+
beforeFiles: [
363+
{
364+
source: '/((?!.swa).*)<YOUR MATCHING RULE>',
365+
destination: '<YOUR REWRITE RULE>',
366+
}
367+
]
368+
}
369+
},
370+
};
371+
```
372+
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.
373+
321374
## Enable logging for Next.js
322375
323376
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:

0 commit comments

Comments
 (0)