接下来的i18n和布局等静态文件返回404

问题描述 投票:0回答:1

亲爱的 Stack Overflow 社区,

我目前正在学习 Next.js 并已成功建立了一个新项目。现在,我将其与 i18n 集成,虽然翻译工作正常,但我遇到了静态文件问题 (404)。

我遵循官方文档进行集成,重点关注应用程序路由器和中间件部分:

为了避免在 URL 中显示区域设置(默认路径结构为 (/[locale]/rest-of-the-path),我对

localePrefix
matcher
进行了一些调整。您可以看到我所做的更改如下:

navigation.ts

    import { createSharedPathnamesNavigation } from "next-intl/navigation";
    
    export const locales = ["pl-pl", "en-us"] as const;
    export const localePrefix = "never"; // <--- I changed that
    
    export const { Link, redirect, usePathname, useRouter } =
      createSharedPathnamesNavigation({ locales, localePrefix });

middleware.ts

import createMiddleware from "next-intl/middleware";
import { locales, localePrefix } from "./navigation";

export default createMiddleware({
  // A list of all locales that are supported
  locales,
  localePrefix,
  // Used when no locale matches
  defaultLocale: "pl-pl",
});

export const config = {
  // Match only internationalized pathnames
  matcher: ["/", "/:path*", "/(pl-pl|en-us)/:path*"],
};

我的主要问题是加载静态文件时出现 404

我怀疑问题可能与 middleware.ts 中的匹配器有关。尽管尝试了不同的配置,我仍然面临同样的问题。谁能指出我正确的方向或建议我还可以尝试什么?如何修复这些 404?

next.js i18next next-router next-intl
1个回答
0
投票

看起来

matcher
错了,我排除了静态文件,所有的工作都像魅力一样。

export const config = {
  matcher: ["/((?!api|_next|_vercel|.*\\..*).*)"],
};
© www.soinside.com 2019 - 2024. All rights reserved.