简体   繁体   English

Next.js 公共图像未在生产版本中显示

[英]Next.js public images not showing in production build

I have a Next.js app I am deploying to Heroku.我有一个 Next.js 应用程序,我正在部署到 Heroku。 When I dev locally I see the images, but when I push to Heroku and check the site, the images have a 404. I have a public folder where I have the images (.png) right in the folder, and the code I reference the image like this当我在本地开发时,我看到了图像,但是当我推送到 Heroku 并检查站点时,图像有一个 404。我有一个公用文件夹,其中我的文件夹中有图像(.png),以及我引用的代码像这样的图像

<Image
    src="/wb_blue_white.png"
    alt="WB Concept"
    width="70"
    height="70"
    className={navStyles.logo}
/>

Both locally and in prod the if I look at the image source they are the same src="/_next/image?url=%2Fwb_blue_white.png&w=256&q=75" but I get a 404 in prod.如果我在本地和产品中查看图像源,它们都是相同的src="/_next/image?url=%2Fwb_blue_white.png&w=256&q=75"但我在产品中得到 404。 What could be causing the image to show up localhost but not in Heroku prod build?什么可能导致图像显示 localhost 但不在 Heroku 产品构建中?

package.json package.json

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start -p $PORT"
},

file structure文件结构

components
pages
public

I was having this same problem.我遇到了同样的问题。 For me the issue was not the components at all.对我来说,问题根本不是组件。 I was hosting my site on Netlify, and I didn't realize that by default Netlify builds the devDependencies.我在 Netlify 上托管我的网站,但我没有意识到默认情况下 Netlify 会构建 devDependencies。 So node packages for development/testing were accidentally getting compiled for production.因此,用于开发/测试的节点包意外地被编译用于生产。

I changed the NODE_ENV (in Netlify) to production and reorganized the packages between dependencies and devDependencies ... and the error went away.我将NODE_ENV (在 Netlify 中)更改为production环境,并重新组织了dependencies项和devDependencies之间的包......并且错误消失了。

I think that the components triggered this issue because the initial request(s) cause them to be generated/optimized server-side.我认为组件触发了这个问题,因为初始请求导致它们在服务器端生成/优化。 So even though the build succeeded.所以即使构建成功。 The remote image request caused dev packages to run in the wrong context.远程图像请求导致开发包在错误的上下文中运行。

I hope this someday helps someone else.我希望有一天这对其他人有所帮助。

Check out next custom server doc and its example repo .查看下一个自定义服务器文档及其示例 repo

Here in this express-looking code that's used to configure the server, app.render() seems to be setting routes for nextjs page , ie /a to pages/a .在这个用于配置服务器的快速代码中, app.render()似乎正在为 nextjs page设置路由,即/apages/a I'm not sure if that's even needed for each path or done for demo purpose.我不确定每条路径是否都需要这样做,或者是否出于演示目的而这样做。 Try fiddling around.试着摆弄一下。

Anyway, if it's anything like basic express server, which I suspect, use() method on express instance will add a "middleware", which 1. takes the request and 2. passes it to next middleware, or sends the response to client.无论如何,如果它类似于我怀疑的基本快递服务器,那么快递实例上的use()方法将添加一个“中间件”,它 1. 接受请求,2. 将其传递给下一个中间件,或将响应发送给客户端。

With server.use(express.static(path.join(__dirname, 'public')));使用server.use(express.static(path.join(__dirname, 'public'))); , where server is the express instance in this case and fortunately(convention actually) in the example repo as well, you can add the middleware that handles static files serve. ,在这种情况下server是 express 实例,幸运的是(实际上是约定)在示例 repo 中,您可以添加处理 static 文件服务的中间件。

I've forgotten the exact way of configuring express, but i'm guessing either:我忘记了配置 express 的确切方法,但我猜要么:

  • right after the express() instantiation, orexpress()实例化之后,或
  • right before the listen()就在listen()之前

should do the trick.应该做的伎俩。 Just put server.use(express.static(path.join(__dirname, 'public'))) in there.只需将server.use(express.static(path.join(__dirname, 'public')))放在那里。

Create a directory for images in the root folder and import, relatively, from there.在根文件夹中为图像创建一个目录,然后从那里相对导入。 Does the trick.行得通。

There are several articles on this, but basically Sharp is required and should be installed.这方面有几篇文章,但基本上Sharp是必需的,应该安装。 The wording in the NextJS docs makes it sound optional, but with the default loader, Sharp is required . NextJS 文档中的措辞使它听起来是可选的,但对于默认加载器, Sharp 是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM