简体   繁体   English

Next.js:_next/webpack-hmr 请求 404

[英]Next.js:_next/webpack-hmr request 404

This issue's demo repo is https://github.com/hh54188/happy-server/tree/issue-demo本期的演示仓库是https://github.com/hh54188/happy-server/tree/issue-demo

I try to integrate Next.js with Hapi.js as a plugin.我尝试用Next.js作为Hapi.js插件集成。 Here is my next.js plugin project folder main structure:这是我的 next.js 插件项目文件夹主要结构:

--plugins
   |--app
        |--pages
            |--app
                |--a.js
        |--handlers
        |--public
             |--dist
        |--index.js
        |--next.config.js

And here is index.js main content, most for route register这里是 index.js 的主要内容,大部分用于路由注册

const nextRenderService = next({
  dir: path.join(__dirname)
});

module.exports = {
  name: "AppService",
  version: "0.0.1",
  register: async function(server, options) {
    await nextRenderService.prepare();

    server.route({
      method: "GET",
      path: `/app/${assetPrefix}/_next/webpack-hmr`,
      handler: nextHandlerWrapper(nextRenderService)
    });

    server.route({
      method: "GET",
      path: "/app/{param*}",
      handler: defaultHandler(nextRenderService)
    });

    server.route({
      method: "GET",
      path: `/app/${assetPrefix}/_next/on-demand-entries-ping`,
      handler: nextHandlerWrapper(nextRenderService)
    });

    server.route({
      method: "GET",
      path: `/app/${assetPrefix}/_next/-/page/{param*}`,
      handler: {
        directory: {
          path: path.join(__dirname, pagesPath),
          listing: true
        }
      }
    });

    server.route({
      method: "GET",
      path: `/app/${assetPrefix}/_next/{param*}`,
      handler: {
        directory: {
          path: path.join(__dirname, distPath),
          listing: true
        }
      }
    });
  }
};

However, when I run the server, and visit http://127.0.0.1:4000/app/a , the page could render success, and most script file could load successful.但是,当我运行服务器并访问http://127.0.0.1:4000/app/a 时,页面可以呈现成功,并且大多数脚本文件可以成功加载。 But the _next/webpack-hmr and the _next/on-demand-entries-ping requests status is 404 .但是_next/webpack-hmr_next/on-demand-entries-ping请求状态是404 And I notice the 404 status is from Next.js, not Hapi.js我注意到 404 状态来自 Next.js,而不是 Hapi.js

So what's wrong with my code ?那么我的代码有什么问题? How can I solve this problem ?我怎么解决这个问题 ?

The assetPrefix configuration is for using a CDN only and is global to NextJs ( documentation ). assetPrefix配置仅用于使用CDN,并且是NextJs的全局文件文档 )。 You don't want to set that for something else, like modifying the NextJs router paths. 您不想将其设置为其他内容,例如修改NextJs路由器路径。 If you don't plan on using a CDN, just ignore this setting. 如果您不打算使用CDN,请忽略此设置。

// in constants/index.js
const assetPrefix = process.env.NODE_ENV === "production" 
    ? "https://cdn.mydomain.com" 
    : "";

You also do not want to list all NextJs internal routes and use the NextJs request handler to handle all calls: 您也不想列出所有NextJs内部路由并使用NextJs请求处理程序来处理所有调用:

// index.js
const next = require("next");
const path = require("path");

const nextRenderService = next({
  dir: path.join(__dirname),
  dev: process.env.NODE_ENV !== "production"
});

const { defaultHandler, nextHandlerWrapper } = require("./hanlders");

module.exports = {
  name: "AppService",
  version: "0.0.1",
  register: async function(server, options) {
    // https://github.com/zeit/next.js/blob/master/examples/custom-server-hapi/server.js
    await nextRenderService.prepare();

    // handle NextJs application requests
    const handler = nextRenderService.getRequestHandler();
    server.route({
      method: "GET",
      path: "/app/{p*}",
      handler: async ({ raw, url }, h) => {
        await handler(raw.req, raw.res, url);
        return h.close;
      }
    });

    // handle NextJs private routes
    server.route({
      method: "GET",
      path: "/_next/{p*}" /* next specific routes */,
      handler: nextHandlerWrapper(nextRenderService)
    });

    // Other routes
    server.route({
      method: "GET",
      path: "/{p*}" /* catch all route */,
      handler: defaultHandler(nextRenderService)
    });
  }
};

The issue has arisen right after upgrading nextjs 11 > 12.升级 nextjs 11 > 12 后问题就出现了。

This helped me:这帮助了我:

npm install webpack-dev-server -g

source: https://coderedirect.com/questions/541528/webpack-hmr-webpack-hmr-404-not-found来源: https : //coderedirect.com/questions/541528/webpack-hmr-webpack-hmr-404-not-found

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

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