Gatsby构建时错误-WebpackError:TypeError:isRedirect不是函数



自从我在login.js上尝试使用此保护路由以来

<Router basepath="/">
<PrivateRoute path="/edit" />
</Router>

这在edit.js

useEffect(() => {
// ... some code here
getEscritos();
if (!user && location.pathname !== `/login`) {
navigate("/login");
return null;
}
}, []);

我不能再构建它了…错误是:

窗口未定义

虽然在本地运行良好。一切正常。

在研究后,我在gatsby-node.js上尝试了这个

exports.onCreateWebpackConfig = ({ stage, loaders, 
actions, getConfig }) => {
if (stage === "build-html" || stage === "develop-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /reach-router/,
use: loaders.null(),
},
],
},
});
externals: getConfig().externals.concat(function (
context,
request,
callback
) {
const regex = /^@?firebase(/(.+))?/;
// exclude firebase products from being bundled, so 
they will be loaded using require() at runtime.
if (regex.test(request)) {
return callback(null, "commonjs " + request); // <- 
use commonjs!
}
callback();
});
}

};

现在的错误是:

WebpackError:TypeError:isRedirect不是一个函数

我不知道该怎么办…

这是回购,如果有任何帮助的话。

提前感谢您抽出时间。

您需要将Firebase的使用和初始化封装在以下条件中:

if(typeof window !== 'undefined')

在SSR(服务器端渲染.(中触发了多个Firebase实例

应用于您的escritos.js:

if(typeof window !== 'undefined'){
const db = getFirestore(app);
const escritosColRef = collection(db, "escritos");
}

这同样适用于index.js(第31行(等…

useEffect钩子内的引用(deps[]为空(应该在没有window条件的情况下工作,因为它们是在加载DOM树时触发的,所以不会破坏SSR。如果错误仍然存在,请尝试包装它们。

最新更新