javascript - 执行上下文被破坏,很可能是因为导航
问题描述
当我为我的 React 应用程序进行生产构建时,会出现此错误。发生这种情况是因为我有一个脚本可以检测浏览器的语言并将用户重定向到使用他们的语言的网站版本。
这是有问题的代码(在 App.js 中):
useEffect(() => {
let navLang = navigator.language || navigator.userLanguage;
navLang = navLang.split("-");
let myLang = navLang[0];
let fullPathname = window.location.pathname;
fullPathname = fullPathname.toLowerCase();
let newPathname = fullPathname.split("/")
// If the site is already in the browser's language stop the function
if(myLang === newPathname[1] || (newPathname[1] === "jp" && (myLang === "ja" || myLang === "jpn"))) {
return
} else {
// get the pathname without the language
// i.e if the url is "/en/profile/" the output will be /profile"
let pathname = getPathname(fullPathname)
if(myLang === "es" || myLang === "spa") {
window.location.pathname = "/es/" + pathname;
} else if(myLang === "ja" || myLang === "jpn") {
window.location.pathname = "/jp/" + pathname;
} else {
window.location.pathname = "/en/" + pathname;
}
}
}, [])
如果我理解正确,问题是 react-snap 在抓取页面时,这段代码有问题。
如果您想知道为什么我使用它window.location.pathname
而不是history.replace()
它是因为其他一些原因,现在不值得解释。
解决方案
推荐阅读
- elixir - 在长生不老药中迭代地图并更新值?
- d3.js - 缩放时的 D3js 画笔区域未居中
- python - 基本转换代码的时间和空间复杂度
- server - 如何减少 Angular 6 应用程序在 AWS 上运行的服务器上的加载时间?
- flutter - 新建 Flutter 项目导致项目空白
- java - if语句中进行字符串扫描时如何解决“字符串无法解析为变量”?
- c - 我被困在 pset 4 - CS50 上的垂直调整大小
- java - 无法使用 appium-android 创建新会话,但是我给出了正确的包名称
- apache-kafka - 如何使用spring boot在kafka中将文件从生产者发送到消费者?
- python - PDF 矿工,坏新行检测