问题描述
next.config.json
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
};
export default nextConfig;
在执行 npm run build
时发现实际的页面只有文字,样式和逻辑全部丢失
打开开发者工具发现外部文件全部报404
问题解析
根据官方文档1
Additionally, a minimal
server.js
file is also output which can be used instead ofnext start
. This minimal server does not copy thepublic
or.next/static
folders by default as these should ideally be handled by a CDN instead, although these folders can be copied to thestandalone/public
andstandalone/.next/static
folders manually, after whichserver.js
file will serve these automatically.
在使用“standalone”作为选项时,next会将css、js等文件排除在外,官方建议将这些静态资源通过CDN来引入
问题解决2
方法一
手动将.next/static
文件夹的文件复制到.next/standalone/.next/static
中
方法二
修改package.json
中的build命令,变为
"build": "next build && cp -r .next/static .next/standalone/.next/ && cp -r public .next/standalone/"
问题即可解决
注意
在Powershell和cmd中都没有cp命令,这是需要用 git bash解决