feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions

View File

@@ -44,7 +44,11 @@ export const middleware = auth(async (request) => {
const isLoggedIn = !!request.auth
if (isLoggedIn) {
return NextResponse.next()
const headers = new Headers(request.headers)
headers.set("x-continue", "1")
return NextResponse.next({
headers,
})
}
if (!env.PUBLIC_URL) {
@@ -61,6 +65,7 @@ export const middleware = auth(async (request) => {
"set-cookie",
`redirectTo=${encodeURIComponent(nextUrlClone.href)}; Path=/; HttpOnly; SameSite=Lax`
)
const loginUrl = login[lang]
return NextResponse.redirect(new URL(loginUrl, nextUrlClone), {
headers,

View File

@@ -1,8 +1,9 @@
import { NextResponse } from "next/server"
import { findLang } from "@/constants/languages"
import { notFound } from "@/server/errors/next"
import { getContentTypeByPathName, PageTypeEnum } from "@/utils/contentType"
import { resolve as resolveEntry } from "@/utils/entry"
import type { NextMiddleware } from "next/server"
@@ -15,37 +16,65 @@ export const middleware: NextMiddleware = async (request) => {
const pathNameWithoutLang = nextUrl.pathname.replace(`/${lang}`, "")
const searchParams = new URLSearchParams(request.nextUrl.searchParams)
const contentType = await getContentTypeByPathName(pathNameWithoutLang, lang)
const { contentType, uid } = await resolveEntry(pathNameWithoutLang, lang)
if (!contentType || !uid) {
throw notFound()
}
const isCurrent = contentType ? contentType.indexOf("current") >= 0 : false
if (request.nextUrl.pathname.includes("/preview")) {
if (isCurrent) {
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
return NextResponse.rewrite(
new URL(`/${lang}/preview-current?${searchParams.toString()}`, nextUrl)
)
}
if (request.nextUrl.pathname.includes("preview")) {
searchParams.set("uri", pathNameWithoutLang.replace("/preview", ""))
return NextResponse.rewrite(
new URL(`/${lang}/preview-current?${searchParams.toString()}`, nextUrl)
new URL(
`/${lang}/preview/${contentType}/${uid}?${searchParams.toString()}`,
nextUrl
),
{
request: {
headers: new Headers({
"x-uid": uid,
}),
},
}
)
}
searchParams.set("uri", pathNameWithoutLang)
switch (contentType) {
case PageTypeEnum.CurrentBlocksPage:
return NextResponse.rewrite(
new URL(
`/${lang}/current-content-page?${searchParams.toString()}`,
nextUrl
)
if (isCurrent) {
return NextResponse.rewrite(
new URL(
`/${lang}/current-content-page?${searchParams.toString()}`,
nextUrl
)
case PageTypeEnum.LoyaltyPage:
return NextResponse.rewrite(
new URL(`/${lang}/loyalty-page?${searchParams.toString()}`, nextUrl)
)
// case PageTypeEnum.ContentPage:
// return NextResponse.rewrite(
// new URL(`/${lang}/content-page?${searchParams.toString()}`, nextUrl)
// )
default:
return NextResponse.next()
)
}
return NextResponse.rewrite(
new URL(
`/${lang}/${contentType}/${uid}?${searchParams.toString()}`,
nextUrl
),
{
request: {
headers: new Headers({
"x-uid": uid,
}),
},
}
)
}
export const matcher: MiddlewareMatcher = (request) => {
return true
// Do not process paths with file extension.
// Only looking for dot might be too brute force/give false positives.
// It works right now but adjust accordingly when new use cases/data emerges.
const lastPathnameSegment = request.nextUrl.pathname.split("/").pop()
return lastPathnameSegment?.indexOf(".") === -1
}

View File

@@ -1,15 +0,0 @@
import { NextResponse } from "next/server"
import { findLang } from "@/constants/languages"
import type { NextMiddleware } from "next/server"
import type { MiddlewareMatcher } from "@/types/middleware"
export const middleware: NextMiddleware = () => {
return new NextResponse("Not found", { status: 404 })
}
export const matcher: MiddlewareMatcher = (request) => {
return !findLang(request.nextUrl.pathname)
}