首页 > 解决方案 > Next.js 将编译后的 service.worker.ts 文件作为静态文件提供。不允许自定义服务器

问题描述

不是重复的。引用:“我试图在使用 webpack 的非下一个 js 项目中复制它”


就我而言(请参见下面的代码),我使用的是 IN next.js。我想提供编译后的service.worker.js文件,就好像它在公用文件夹中一样。但就我而言,该service.worker.ts文件位于public/文件夹之外。它首先是用 webpack 编译的。

我应该将编译service.worker.ts文件的结果放在哪里?目标是能够在mydomain.com/service.worker.js. 也许我使用了错误的配置?也许应该使用webpackDevMiddleware?

const nectConfig = {
  distDir: 'dist',
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    const newConfig = {
      ...config,
      entry: function() {
        return config.entry().then(entry => {
          return {
            ...entry,
            /* where should the output be, so that the service-worker.js file
             can be accessed like any other file under the public/ directory?*/
            './public/service-worker.js': './service-worker.ts'
          };
        });
      },
      output: { ...config.output, globalObject: 'self' }
    };
    return newConfig;
  }
};

标签: webpacknext.jswebpack-dev-server

解决方案


推荐阅读