middleware.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import createMiddleware from "next-intl/middleware";
  2. import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
  3. import { locales } from "./i18n";
  4. function stackMiddleware(functions: Function[], index = 0): Function {
  5. const current = functions[index];
  6. if (current) {
  7. const next = stackMiddleware(functions, index + 1);
  8. return current(next);
  9. }
  10. return () => NextResponse.next();
  11. }
  12. function localMiddleware() {
  13. return async (request: NextRequest, _next: NextFetchEvent) => {
  14. const handleI18nRouting = createMiddleware({
  15. locales: locales,
  16. defaultLocale: locales.at(2)!,
  17. });
  18. return handleI18nRouting(request);
  19. };
  20. }
  21. const whiteRouters = ["/"];
  22. function authMiddleware(next: Function) {
  23. return async (request: NextRequest, res: NextResponse) => {
  24. return next(request, res);
  25. };
  26. }
  27. export default stackMiddleware([authMiddleware, localMiddleware]);
  28. // 创建一个正则表达式对象
  29. export const config = {
  30. // todo use this to match the path
  31. matcher: ["/", "/(zh|en|es|br)/:path*"],
  32. };