2026-05-08 13:35:00 +09:00
|
|
|
import { NextResponse, type NextRequest } from "next/server"
|
|
|
|
|
import createMiddleware from "next-intl/middleware"
|
2026-05-14 14:18:13 +09:00
|
|
|
import { locales, type Locale } from "./i18n/config"
|
2026-05-08 13:35:00 +09:00
|
|
|
import { routing } from "./i18n/routing"
|
|
|
|
|
|
|
|
|
|
const handleI18nRouting = createMiddleware(routing)
|
2026-05-25 17:14:39 +09:00
|
|
|
const oldHosts = new Set(["www.omo.dev"])
|
|
|
|
|
const primaryHost = "omo.dev"
|
2026-05-14 14:18:13 +09:00
|
|
|
const installationPaths = new Set([
|
|
|
|
|
"installation",
|
|
|
|
|
"installation.md",
|
|
|
|
|
"docs/installation",
|
|
|
|
|
"docs/installation.md",
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
function getLocaleSegment(segment: string | undefined): Locale | null {
|
|
|
|
|
if (!segment) return null
|
|
|
|
|
return locales.find((locale) => locale === segment) ?? null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInstallationDocsPath(pathname: string): string | null {
|
|
|
|
|
const segments = pathname.split("/").filter(Boolean)
|
|
|
|
|
const locale = getLocaleSegment(segments[0])
|
|
|
|
|
const routeSegments = locale ? segments.slice(1) : segments
|
|
|
|
|
|
|
|
|
|
if (!installationPaths.has(routeSegments.join("/"))) return null
|
|
|
|
|
|
|
|
|
|
return locale ? `/${locale}/docs` : "/docs"
|
|
|
|
|
}
|
2026-05-08 13:35:00 +09:00
|
|
|
|
2026-05-20 14:26:09 +09:00
|
|
|
export default function middleware(request: NextRequest): NextResponse {
|
2026-05-08 13:35:00 +09:00
|
|
|
const forwardedHost = request.headers.get("x-forwarded-host")
|
|
|
|
|
const requestHost = request.headers.get("host")
|
|
|
|
|
const hostname = (forwardedHost ?? requestHost ?? request.nextUrl.hostname).split(":")[0]
|
2026-05-14 14:18:13 +09:00
|
|
|
const redirectUrl = request.nextUrl.clone()
|
|
|
|
|
let shouldRedirect = false
|
2026-05-08 13:35:00 +09:00
|
|
|
|
|
|
|
|
if (hostname && oldHosts.has(hostname)) {
|
|
|
|
|
redirectUrl.protocol = "https"
|
|
|
|
|
redirectUrl.host = primaryHost
|
2026-05-14 14:18:13 +09:00
|
|
|
shouldRedirect = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const installationDocsPath = getInstallationDocsPath(request.nextUrl.pathname)
|
|
|
|
|
if (installationDocsPath) {
|
|
|
|
|
redirectUrl.pathname = installationDocsPath
|
|
|
|
|
redirectUrl.search = ""
|
|
|
|
|
redirectUrl.hash = "installation"
|
|
|
|
|
shouldRedirect = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldRedirect) {
|
2026-05-08 13:35:00 +09:00
|
|
|
return NextResponse.redirect(redirectUrl, 308)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handleI18nRouting(request)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const config = {
|
2026-05-14 14:18:13 +09:00
|
|
|
matcher: [
|
|
|
|
|
"/",
|
2026-05-20 14:26:09 +09:00
|
|
|
/*
|
|
|
|
|
* Match everything except:
|
|
|
|
|
* - api routes
|
|
|
|
|
* - Next.js internals (_next, _vercel)
|
|
|
|
|
* - Next.js file-based metadata routes (opengraph-image, twitter-image, icon, apple-icon, manifest, robots, sitemap)
|
|
|
|
|
* These serve images / JSON / XML directly and must NOT be redirected by i18n.
|
|
|
|
|
* - Any path containing a dot (favicon.ico, *.webp, *.png, etc.)
|
|
|
|
|
*/
|
|
|
|
|
"/((?!api|_next|_vercel|opengraph-image|twitter-image|icon|apple-icon|manifest\\.webmanifest|robots\\.txt|sitemap\\.xml|.*\\..*).+)",
|
2026-05-14 14:18:13 +09:00
|
|
|
"/installation.md",
|
|
|
|
|
"/:locale(en|ko|ja|zh)/installation.md",
|
|
|
|
|
"/docs/installation.md",
|
|
|
|
|
"/:locale(en|ko|ja|zh)/docs/installation.md",
|
|
|
|
|
],
|
2026-05-08 13:35:00 +09:00
|
|
|
}
|