Skip to content

Commit 7119063

Browse files
feat: Generate static redirects for simple Next.js redirects
This change introduces the ability to generate static Netlify redirects for a subset of Next.js redirects defined in `next.config.js`. This offloads simple redirects to Netlify's edge, reducing function invocations and improving performance. The new `setRedirectsConfig` function handles simple redirects, including those with placeholders and splats, by converting them to the Netlify redirect format. Complex redirects that use `has`, `missing`, or regex-based sources will continue to be handled by the serverless function at runtime. Unit tests have been added to verify the redirect generation logic. An E2E test has also been added to ensure that simple redirects are handled by the edge, while complex redirects are correctly passed to the serverless function. The E2E test uses the `debug-x-nf-function-type` header to differentiate between edge-handled and function-handled responses. The E2E test has been refactored to have separate, descriptively named tests for each redirect case to improve readability and maintainability.
1 parent 4019074 commit 7119063

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

src/build/redirects.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NetlifyPluginOptions } from '@netlify/build'
22
import type { RoutesManifest } from 'next/dist/build/index.js'
3-
import { beforeEach, describe, expect, test, vi, type TestContext } from 'vitest'
3+
import { beforeEach, describe, expect, test, type TestContext, vi } from 'vitest'
44

55
import { PluginContext } from './plugin-context.js'
66
import { setRedirectsConfig } from './redirects.js'

src/build/redirects.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import type { PluginContext } from './plugin-context.js'
44

55
// These are the characters that are not allowed in a simple redirect source.
66
// They are all special characters in a regular expression.
7+
// eslint-disable-next-line unicorn/better-regex, no-useless-escape
78
const DISALLOWED_SOURCE_CHARACTERS = /[()\[\]{}?+|]/
89
const SPLAT_REGEX = /\/:(\w+)\*$/
910

1011
/**
1112
* Adds redirects from the Next.js routes manifest to the Netlify config.
1213
*/
1314
export const setRedirectsConfig = async (ctx: PluginContext): Promise<void> => {
14-
const {
15-
redirects,
16-
basePath,
17-
} = await ctx.getRoutesManifest()
15+
const { redirects, basePath } = await ctx.getRoutesManifest()
1816

1917
for (const redirect of redirects) {
2018
// We can only handle simple redirects that don't have complex conditions.
@@ -32,7 +30,7 @@ export const setRedirectsConfig = async (ctx: PluginContext): Promise<void> => {
3230

3331
const splatMatch = from.match(SPLAT_REGEX)
3432
if (splatMatch) {
35-
const param = splatMatch[1]
33+
const [, param] = splatMatch
3634
from = from.replace(SPLAT_REGEX, '/*')
3735
to = to.replace(`/:${param}`, '/:splat')
3836
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { clearStaleEdgeHandlers, createEdgeHandlers } from './build/functions/ed
1818
import { clearStaleServerHandlers, createServerHandler } from './build/functions/server.js'
1919
import { setImageConfig } from './build/image-cdn.js'
2020
import { PluginContext } from './build/plugin-context.js'
21+
import { setRedirectsConfig } from './build/redirects.js'
2122
import {
2223
verifyAdvancedAPIRoutes,
2324
verifyNetlifyFormsWorkaround,
@@ -99,6 +100,7 @@ export const onBuild = async (options: NetlifyPluginOptions) => {
99100
createEdgeHandlers(ctx),
100101
setHeadersConfig(ctx),
101102
setImageConfig(ctx),
103+
setRedirectsConfig(ctx),
102104
])
103105
})
104106
}

0 commit comments

Comments
 (0)