当使用NextJS的独立服务器时,重新验证选项似乎不起作用。
I got this:
return {
props: {
page,
},
revalidate: 60,
};
我有以下NextJS配置:
{
output: "standalone",
reactStrictMode: true,
swcMinify: true,
i18n: {
locales: ["default", "en", "nl", "fr"],
defaultLocale: "default",
localeDetection: false,
},
trailingSlash: true,
}
我使用以下docker文件创建一个容器:
# Install dependencies only when needed
FROM node:16-alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN
if [ -f yarn.lock ]; then yarn --frozen-lockfile;
elif [ -f package-lock.json ]; then npm ci;
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile;
else echo "Lockfile not found." && exit 1;
fi
# Rebuild the source code only when needed
FROM node:16-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn build
# If using npm comment out above and use below instead
# RUN npm run build
# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
ENV PORT 3000
CMD ["node", "server.js"]
我自己尝试了很多东西,但没有任何效果,在互联网上,我不断遇到你必须在NextJS服务器上运行,但如果我是正确的,那就是你在独立运行时所做的。
深入问题,发现了更多错误。发现next/image组件的默认加载器使用了squoosh,因为它可以快速安装并且适合于开发环境。但是对于使用output: "standalone"的生产环境,您必须安装sharp.
所以我运行
yarn add sharp
这个错误不再发生了。但我仍然没有得到内容的更新。因为我不能让这个工作,我需要继续这个项目,我签出了按需重新验证。点击这里阅读更多内容:https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration
我现在做的是:
- 在我的NextJS解决方案中创建一个API端点来重新验证
- 使用webhook从我的CMS调用api端点
那对我起作用了。我现在可以在我的CMS中更新一些东西,webhook被触发,然后我得到了一个新的新的构建页面!
在我看来这是一个更好的解决方案,因为:
- 它只在真正需要时重新验证页面
- 更改/重新验证后,下一个访问者直接看到新的更新版本
所以我很高兴它没有工作,并与一点额外的努力,我得到了一个更好的解决方案!
希望这能帮助到一些人。