bub*_*ser 2 javascript browser internet-explorer next.js
我的 Next.js 应用程序无法在 IE 上运行。
它显示空白屏幕并在控制台上抛出语法错误。
这没关系,因为 IE 即将停止使用,但我想阻止 IE 在检测到浏览器时执行单行代码。
从这个答案,我可以检查浏览器是否是IE:
if (window.document.documentMode) {
// IE detected
document.write('IE is not supported. Please use a modern browser!')
}
Run Code Online (Sandbox Code Playgroud)
用户不会看到空白屏幕,而是会看到为什么该网站无法使用上述消息。
这种方法有两个问题:
任何帮助,将不胜感激。
您不应该依赖 Next.js 应用程序在已弃用/不受支持的浏览器上显示消息,因为代码本身可能会在这些浏览器上崩溃(缺少填充等)
相反,正确显示有关过时浏览器的消息警告的唯一方法是异步加载 JS 脚本,该脚本不使用 ES5+ 功能,这样它就可以在所有浏览器中工作(而且,它不会减慢您的应用程序的速度)或增加包大小,因为它是异步的)。
据我所知,_document.js这是您可以检查浏览器的最早的地方,因为它在第一个页面请求时呈现一次。
您可以使用外部 CDN,或checkBrowser.js在public文件夹中创建文件。
这是我的解决方案。
页面/_document.js
export default class MyDocument extends Document {
...
render() {
return (
<Html>
<Head>
<script async src="/scripts/checkBrowser.js" />
</Head>
...
</Html>
)
}
}
Run Code Online (Sandbox Code Playgroud)
公共/脚本/checkBrowser.js
// if browser is IE, redirect
if (window.document.documentMode) {
window.location.replace('/outdated.html')
}
Run Code Online (Sandbox Code Playgroud)
公共/过时的.html
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Unsupported browser</title>
</head>
<body>
<p>Internet Explorer (IE) is not supported. Please use a modern browser.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
信用:来自 CDN 的脚本,重定向方法,还有 @YuZhou 分享这些链接
| 归档时间: |
|
| 查看次数: |
1878 次 |
| 最近记录: |