在 docker 中无法 运行 具有国际化 i18n 的 nextjs 应用程序
Cant run nextjs app with internationalization i18n in docker
该应用程序是用打字稿编写的。
构建成功,但是当我在 localhost:3000 中启动程序时,出现此错误
TypeError: The URL is not configured with i18n
at URL.get locale [as locale] (/app/.next/server/pages/_middleware.js:1:46515)
在我的 dockerfile 中,我复制了所有需要的文件
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
COPY next.config.js /app/next.config.js
COPY .next /app/.next
COPY public /app/public
next.config.js
包含:
module.exports = {
reactStrictMode: true,
i18n: {
locales: ["en", "fr"],
defaultLocale: "en",
},
};
在next.config.js
中我尝试添加rewrites
函数但仍然出现相同的错误。
async rewrites() {
return [
{
source: "/",
destination: "/",
locale: false, // Use `locale: false` so that the prefix matches the desired locale correctly
},
{
source: "/fr/test",
destination: "/fr/test",
locale: false, // Use `locale: false` so that the prefix matches the desired locale correctly
},
];
},
_middleware.ts 包含:
import { NextRequest, NextResponse } from "next/server";
const PUBLIC_FILE = /\.(.*)$/;
export function middleware(request: NextRequest) {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes("/api/") &&
request.nextUrl.locale === "default";
return shouldHandleLocale
? NextResponse.redirect(`/en${request.nextUrl.href}`)
: undefined;
}
中间件似乎无法正常工作。
任何有用的提示或想法。
谢谢。
我们需要复制所有 docker 图像中的 next.config
deps
、builder
、runner
这个 docker 文件是 nextjs
的样本
FROM node:16-alpine as deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile
FROM node:16-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
FROM node:16-alpine AS runner
RUN addgroup group && adduser -system -group user
USER user
WORKDIR /app
ENV NODE_ENV production
COPY --from=builder /app/public ./public
COPY --from=builder --chown=user:group /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/next.config.js ./next.config.js
EXPOSE 3000
ENV PORT 3000
CMD ["node_modules/.bin/next", "start"]
了。 next.config.js
像这样:
module.exports = {
reactStrictMode: true,
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
swcMinify: false,
future: {
webpack5: true,
},
};
该应用程序是用打字稿编写的。 构建成功,但是当我在 localhost:3000 中启动程序时,出现此错误
TypeError: The URL is not configured with i18n
at URL.get locale [as locale] (/app/.next/server/pages/_middleware.js:1:46515)
在我的 dockerfile 中,我复制了所有需要的文件
COPY package.json /app/package.json
COPY package-lock.json /app/package-lock.json
COPY next.config.js /app/next.config.js
COPY .next /app/.next
COPY public /app/public
next.config.js
包含:
module.exports = {
reactStrictMode: true,
i18n: {
locales: ["en", "fr"],
defaultLocale: "en",
},
};
在next.config.js
中我尝试添加rewrites
函数但仍然出现相同的错误。
async rewrites() {
return [
{
source: "/",
destination: "/",
locale: false, // Use `locale: false` so that the prefix matches the desired locale correctly
},
{
source: "/fr/test",
destination: "/fr/test",
locale: false, // Use `locale: false` so that the prefix matches the desired locale correctly
},
];
},
_middleware.ts 包含:
import { NextRequest, NextResponse } from "next/server";
const PUBLIC_FILE = /\.(.*)$/;
export function middleware(request: NextRequest) {
const shouldHandleLocale =
!PUBLIC_FILE.test(request.nextUrl.pathname) &&
!request.nextUrl.pathname.includes("/api/") &&
request.nextUrl.locale === "default";
return shouldHandleLocale
? NextResponse.redirect(`/en${request.nextUrl.href}`)
: undefined;
}
中间件似乎无法正常工作。 任何有用的提示或想法。 谢谢。
我们需要复制所有 docker 图像中的 next.config
deps
、builder
、runner
这个 docker 文件是 nextjs
FROM node:16-alpine as deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile
FROM node:16-alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN npm run build && npm install --production --ignore-scripts --prefer-offline
FROM node:16-alpine AS runner
RUN addgroup group && adduser -system -group user
USER user
WORKDIR /app
ENV NODE_ENV production
COPY --from=builder /app/public ./public
COPY --from=builder --chown=user:group /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/next.config.js ./next.config.js
EXPOSE 3000
ENV PORT 3000
CMD ["node_modules/.bin/next", "start"]
了。 next.config.js
像这样:
module.exports = {
reactStrictMode: true,
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
},
swcMinify: false,
future: {
webpack5: true,
},
};