未定义NextJS Webworker的窗口

时间:2019-05-18 23:18:17

标签: node.js webpack web-worker next.js

使用NextJS和next-web-worker包时出现未定义窗口错误。

require('dotenv').config();
const withPlugins = require('next-compose-plugins');
const withCSS = require('@zeit/next-css');
const withWorkers = require('@zeit/next-workers');
const Dotenv = require('dotenv-webpack');

const webpackConfig = (config) => {
  config.plugins = config.plugins || [];
  config.target = 'universal';
  config.output = {
    ...config.output,
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    globalObject: '(this || global)',
    publicPath: 'http://localhost:3000'
  };

  config.plugins = [
    ...config.plugins,

    // Read the .env file
    new Dotenv({
      path: path.join(__dirname, '.env'),
      systemvars: true
    })
  ];

  return config;
};

module.exports = withPlugins([[withCSS], [withWorkers]], webpackConfig);

建议的变通办法似乎是将globalObject设置为此或全局,但这不起作用。还有其他解决方法吗?

1 个答案:

答案 0 :(得分:1)

我刚刚遇到了同样的错误,可以通过遵循the advice here来解决:

yarn add @zeit/next-workers@canary