简体   繁体   English

下一次构建后的下一个 JS 图像提供 404 个图像

[英]Next JS Images after next build gives 404 images

Default URL structure is http://localhost:5000/_next/image?url="someURL".默认的 URL 结构是 http://localhost:5000/_next/image?url="someURL"。

I want the URL to be like http://localhost:5000/demo/_next/image?url="someURL".我希望 URL 像http://localhost:5000/demo/_next/image?url="someURL"。

which can be achieved by这可以通过

// next.config.js
     images: {
            domains: ['example.domain.com'],
            path: `/demo/_next/image`
        },

In this, the _next/image where the directory is pointing cannot be found.在这里,找不到目录指向的_next/image

After this, all the images are giving 404. I need a solution to this problem in the next js.在此之后,所有的图像都给 404。我需要在下一个 js 中解决这个问题。

Have you tried to create a middleware to rewrite those routes to the correct location?您是否尝试过创建一个中间件来将这些路由重写到正确的位置?

Something like: pages/_middleware.ts with content:类似于: pages/_middleware.ts的内容:

import { NextRequest, NextResponse } from 'next/server';

export function middleware(req: NextRequest): NextResponse {
  if (req.nextUrl.href.includes('/demo/_next/image'))
    return NextResponse.rewrite(
      req.nextUrl.href.replace('/demo/_next/image', '/_next/image'),
    );

  return null;
}

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

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