|
1 | 1 | import { compile, Match, match, PathFunction } from "path-to-regexp";
|
2 | 2 |
|
| 3 | +import { NextConfig } from "../config"; |
3 | 4 | import { InternalEvent, InternalResult } from "../event-mapper";
|
4 | 5 | import { debug } from "../logger";
|
5 | 6 | import {
|
@@ -170,7 +171,40 @@ export function handleRedirects(
|
170 | 171 | event: InternalEvent,
|
171 | 172 | redirects: RedirectDefinition[],
|
172 | 173 | ): InternalResult | undefined {
|
173 |
| - const { internalEvent, __rewrite } = handleRewrites(event, redirects); |
| 174 | + if ( |
| 175 | + NextConfig.trailingSlash && |
| 176 | + !event.rawPath.endsWith("/") && |
| 177 | + !event.headers["x-nextjs-data"] |
| 178 | + ) { |
| 179 | + return { |
| 180 | + type: event.type, |
| 181 | + statusCode: 308, |
| 182 | + headers: { |
| 183 | + Location: event.url + "/", |
| 184 | + }, |
| 185 | + body: "", |
| 186 | + isBase64Encoded: false, |
| 187 | + }; |
| 188 | + // eslint-disable-next-line sonarjs/elseif-without-else |
| 189 | + } else if ( |
| 190 | + !NextConfig.trailingSlash && |
| 191 | + event.rawPath.endsWith("/") && |
| 192 | + event.rawPath !== "/" |
| 193 | + ) { |
| 194 | + return { |
| 195 | + type: event.type, |
| 196 | + statusCode: 308, |
| 197 | + headers: { |
| 198 | + Location: event.url.replace(/\/$/, ""), |
| 199 | + }, |
| 200 | + body: "", |
| 201 | + isBase64Encoded: false, |
| 202 | + }; |
| 203 | + } |
| 204 | + const { internalEvent, __rewrite } = handleRewrites( |
| 205 | + event, |
| 206 | + redirects.filter((r) => !r.internal), |
| 207 | + ); |
174 | 208 | if (__rewrite && !__rewrite.internal) {
|
175 | 209 | return {
|
176 | 210 | type: event.type,
|
|
0 commit comments