fix: trim away trailing slash
This commit is contained in:
@@ -4,6 +4,7 @@ import { findLang } from "@/constants/languages"
|
||||
import { notFound } from "@/server/errors/next"
|
||||
|
||||
import { resolve as resolveEntry } from "@/utils/entry"
|
||||
import { removeTrailingSlash } from "@/utils/url"
|
||||
|
||||
import { getDefaultRequestHeaders } from "./utils"
|
||||
|
||||
@@ -15,7 +16,9 @@ export const middleware: NextMiddleware = async (request) => {
|
||||
const { nextUrl } = request
|
||||
const lang = findLang(nextUrl.pathname)!
|
||||
|
||||
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
|
||||
const pathWithoutTrailingSlash = removeTrailingSlash(nextUrl.pathname)
|
||||
|
||||
const pathNameWithoutLang = pathWithoutTrailingSlash.replace(`/${lang}`, "")
|
||||
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
|
||||
|
||||
const { contentType, uid } = await resolveEntry(pathNameWithoutLang, lang)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { findLang } from "@/constants/languages"
|
||||
|
||||
import { removeTrailingSlash } from "@/utils/url"
|
||||
|
||||
import type { NextRequest } from "next/server"
|
||||
|
||||
export function getDefaultRequestHeaders(request: NextRequest) {
|
||||
@@ -9,9 +11,11 @@ export function getDefaultRequestHeaders(request: NextRequest) {
|
||||
headers.set("x-lang", lang)
|
||||
headers.set(
|
||||
"x-pathname",
|
||||
request.nextUrl.pathname.replace(`/${lang}`, "").replace(`/webview`, "")
|
||||
removeTrailingSlash(
|
||||
request.nextUrl.pathname.replace(`/${lang}`, "").replace(`/webview`, "")
|
||||
)
|
||||
)
|
||||
headers.set("x-url", request.nextUrl.href)
|
||||
headers.set("x-url", removeTrailingSlash(request.nextUrl.href))
|
||||
|
||||
return headers
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ jiti("./env/client")
|
||||
const nextConfig = {
|
||||
poweredByHeader: false,
|
||||
eslint: { ignoreDuringBuilds: true },
|
||||
trailingSlash: false,
|
||||
experimental: {
|
||||
serverActions: {
|
||||
allowedOrigins: [
|
||||
|
||||
@@ -1,3 +1,11 @@
|
||||
export function removeMultipleSlashes(str: string) {
|
||||
return str.replaceAll(/\/\/+/g, "/")
|
||||
}
|
||||
|
||||
export function removeTrailingSlash(pathname: string) {
|
||||
if (pathname.endsWith("/")) {
|
||||
// Remove the trailing slash
|
||||
return pathname.slice(0, -1)
|
||||
}
|
||||
return pathname
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user