CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/next/middleware.ts
Views: 791
1
import type { NextRequest } from "next/server";
2
import { NextResponse } from "next/server";
3
4
import { LOCALE } from "locales/misc";
5
6
export function middleware(request: NextRequest) {
7
// check the incoming request's URL path for known locales and rewrites the URL to a `/lang/[locale]`.
8
// Otherwise, the `/[owner]` route will be used!
9
for (const locale of LOCALE) {
10
if (request.nextUrl.pathname.startsWith(`/${locale}`)) {
11
return NextResponse.rewrite(new URL(`/lang/${locale}`, request.url));
12
}
13
14
// This normalizes /lang/[locale] and /lang/[locale]/foo to /[locale] and /[locale]/foo
15
if (request.nextUrl.pathname.startsWith(`/lang/${locale}`)) {
16
return NextResponse.redirect(
17
new URL(
18
request.nextUrl.pathname.replace(`/lang/${locale}`, `/${locale}`),
19
request.url,
20
),
21
);
22
}
23
}
24
}
25
26