next info
(从版本 12.0.8 及更高版本可用)
Operating System:
Platform: linux
Arch: x64
Version: #1 SMP Mon Oct 18 19:27:44 UTC 2021
Binaries:
Node: 14.18.0
npm: 6.14.15
Yarn: 1.22.17
pnpm: 6.24.4
Relevant packages:
next: 12.0.8-canary.18
react: 17.0.2
react-dom: 17.0.2
您使用的 Next.js 版本是什么?
12.0.8-canary.18
您使用的 Node.js 版本是什么?14.18.0
你使用的是什么浏览器?火狐浏览器
描述错误使用中间件,NextResponse.rewrite 无法使用另一个现有路由正确重写路由。这样做会导致 404。
我正在使用 i18n,但https://github.com/vercel/next.js/pull/31174无法修复它。
似乎与https://github.com/vercel/next.js/issues/30749有关
这是我的中间件文件:
import { NextRequest, NextResponse } from "next/server";
import { CONSTANTS } from "src/utils/constants";
const ONE_YEAR_IN_MS = 3600 * 365 * 24 * 1000;
export function middleware(req: NextRequest) {
const expires = new Date(Date.now() + ONE_YEAR_IN_MS);
const initialVariant = req.cookies[CONSTANTS.RH_HOME_PAGE_TEST_COOKIE];
// Redirect direct traffic to our test variant
if (
(!initialVariant ||
initialVariant !== CONSTANTS.RH_HOME_PAGE_VARIANT_1_SLUG) &&
req.nextUrl.pathname === `/${CONSTANTS.RH_HOME_PAGE_VARIANT_1_SLUG}`
) {
return NextResponse.redirect(CONSTANTS.HOME_ROUTE);
}
// Home page AB testing
if (req.nextUrl.pathname === CONSTANTS.HOME_ROUTE) {
let variant = initialVariant;
// Determine the variant via a random split
if (!variant) {
const split: boolean = Math.random() >= 0.5;
variant = split
? CONSTANTS.RH_HOME_PAGE_VARIANT_1_SLUG
: CONSTANTS.RH_HOME_PAGE_ORIGINAL;
}
const res =
variant === CONSTANTS.RH_HOME_PAGE_ORIGINAL
? NextResponse.next()
: NextResponse.redirect(`/${variant}`, 307); // Temporary redirect
if (initialVariant !== variant) {
res.cookie(CONSTANTS.RH_HOME_PAGE_TEST_COOKIE, variant, { expires });
}
return res;
} else {
return NextResponse.next();
}
}
我的下一个配置:
module.exports = {
productionBrowserSourceMaps: true,
outputFileTracing: false,
images: {
domains: [
"images.ctfassets.net",
"assets.ctfassets.net",
"videos.ctfassets.net",
"assets.zappyride.com",
],
},
i18n: {
locales: ["en", "es"],
defaultLocale: "en",
},
env: {},
webpack: (config, options) => {
if (!options.isServer) {
config.resolve.alias["@sentry/node"] = "@sentry/react";
}
// Only add sentry webpack plugin when sentry DSN is defined and the
// app is being deployed (NODE_ENV=production when making an optimized build
// for a deployed environment).
if (
process.env.NEXT_PUBLIC_SENTRY_DSN &&
process.env.NODE_ENV === "production"
) {
config.plugins.push(
new SentryWebpackPlugin({
include: ".next",
ignore: ["node_modules"],
urlPrefix: "~/_next",
release: process.env.VERCEL_GITHUB_COMMIT_SHA,
})
);
}
config.resolve.alias = {
...config.resolve.alias,
"@mui/styled-engine": "@mui/styled-engine-sc",
};
return config;
},
async redirects() {
if (process.env.ENVIRONMENT === "production") {
return [...productionRedirects, ...sharedRedirects];
} else {
return [...sharedRedirects];
}
},
async headers() {
return [
{
source: "/",
headers: [
{
key: "Cache-Control",
value: "s-maxage=1, stale-while-revalidate",
},
...securityHeaders,
],
},
{
source: "/:path*",
headers: [
{
key: "Cache-Control",
value: "s-maxage=1, stale-while-revalidate",
},
...securityHeaders,
],
},
{
source: "/fonts/Averta/(.*)",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, stale-while-revalidate",
},
...securityHeaders,
],
},
{
source: "/fonts/fontface.css",
headers: [
{
key: "Cache-Control",
value: "public, max-age=31536000, stale-while-revalidate",
},
...securityHeaders,
],
},
];
},
};
预期行为
重写应该像在 Vercel 上本地那样进行,而不是 404。