feat: add trailingSlash middleware

This commit is contained in:
Arvid Norlin
2025-05-12 12:01:47 +02:00
parent 2cdd086ef3
commit c22cd62d21
3 changed files with 35 additions and 4 deletions

View File

@@ -0,0 +1,24 @@
import { type NextMiddleware, NextResponse } from "next/server"
import { getPublicNextURL } from "@/server/utils"
import { getDefaultRequestHeaders } from "./utils"
import type { MiddlewareMatcher } from "@/types/middleware"
export const middleware: NextMiddleware = async (request) => {
const headers = getDefaultRequestHeaders(request)
const newUrl = new URL(
request.nextUrl.pathname.slice(0, -1),
getPublicNextURL(request)
)
return NextResponse.redirect(newUrl, {
headers,
})
}
export const matcher: MiddlewareMatcher = (request) => {
return request.nextUrl.pathname.endsWith("/")
}