Files
oh-my-opencode/packages/web/middleware.ts
T
YeonGyu-Kim 7e0406f4ec fix(web): UX/a11y polish + middleware metadata route fix
- footer.tsx: Discord link → canonical discord.gg/PUwSMR9XNk
  (was outdated invite that 404'd).
- [locale]/layout.tsx: hooks count 40 → 54 (matches AGENTS.md).
- lib/stats.ts:88: drop '!' non-null assertion; use '??' fallback.
  Removes the only '!' in packages/web (project rule).
- nav-header.tsx: hamburger Button size='icon' → explicit h-11 w-11;
  mobile drawer links min-h-11 px-3 rounded-md. WCAG 2.5.5 tap
  target 44x44 minimum.
- install-command.tsx: copy button h-8 w-8 → h-11 w-11 (was 32px,
  below WCAG). Icon stays 16x16; padding fills to 44.
- middleware.ts: matcher excludes opengraph-image, twitter-image,
  icon, apple-icon, manifest.webmanifest, robots.txt, sitemap.xml.
  Previously next-intl redirected /opengraph-image → /en/opengraph-image
  breaking OG previews for crawlers that hit the unlocalized path.
2026-05-20 14:26:09 +09:00

77 lines
2.6 KiB
TypeScript

import { NextResponse, type NextRequest } from "next/server"
import createMiddleware from "next-intl/middleware"
import { locales, type Locale } from "./i18n/config"
import { routing } from "./i18n/routing"
const handleI18nRouting = createMiddleware(routing)
const oldHosts = new Set(["ohmyopencode.org", "www.ohmyopencode.org"])
const primaryHost = "ohmyopenagent.com"
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"
}
export default function middleware(request: NextRequest): NextResponse {
const forwardedHost = request.headers.get("x-forwarded-host")
const requestHost = request.headers.get("host")
const hostname = (forwardedHost ?? requestHost ?? request.nextUrl.hostname).split(":")[0]
const redirectUrl = request.nextUrl.clone()
let shouldRedirect = false
if (hostname && oldHosts.has(hostname)) {
redirectUrl.protocol = "https"
redirectUrl.host = primaryHost
shouldRedirect = true
}
const installationDocsPath = getInstallationDocsPath(request.nextUrl.pathname)
if (installationDocsPath) {
redirectUrl.pathname = installationDocsPath
redirectUrl.search = ""
redirectUrl.hash = "installation"
shouldRedirect = true
}
if (shouldRedirect) {
return NextResponse.redirect(redirectUrl, 308)
}
return handleI18nRouting(request)
}
export const config = {
matcher: [
"/",
/*
* 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|.*\\..*).+)",
"/installation.md",
"/:locale(en|ko|ja|zh)/installation.md",
"/docs/installation.md",
"/:locale(en|ko|ja|zh)/docs/installation.md",
],
}