webpack - Vue - 防止 prerender-spa-plugin 加载modernizr脚本
问题描述
我正在使用 Vue-cli 3 + prerender-spa-plugin 构建一个生产网站,但我需要一些只能由最终用户加载的脚本(例如 modernizr 检测),而不是 pre-spa-plugin 环境。
如何配置设置以使 prerender-spa-plugin 忽略这些脚本?
我在索引文件中有脚本,在公共文件夹中,如下所示:
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<!-- Facebook Pixel Code -->
</head>
<body>
<div id="app"></div>
<!-- jivochat script -->
<script src="modernizr-webp.js"></script>
</body>
</html>
解决方案
在插件的问题中搜索我找到了一个解决方案:
在vue.config.js
:
new PrerenderSPAPlugin({
....
renderer: new PrerenderSPAPlugin.PuppeteerRenderer({
// The name of the property
injectProperty: '__PRERENDER_INJECTED',
// The values to have access to via `window.injectProperty` (the above property )
inject: { foo: 'bar' }
})
})
在应用程序代码中:
if ( window.__PRERENDER_INJECTED === undefined ) {
// The code snippet to be ignored in pre-render
}
推荐阅读
- regex - 如何在 Python 中只读取文件的一部分?
- javascript - d3.js 具有每片偏移起始角度的多级饼图
- python - 有没有更好的方法来整理这些代码?这是具有 4 个 ODE 的 Runge-Kutta 4 阶
- samesite - 如何编辑 samesite=lax 的 setcookie 代码
- swift - UICollectionView/AppDelegate 在跟随视频教程时导致崩溃
- android - 为生产构建 ionic 3 应用程序失败,我不明白为什么?
- sql-server - 在现有的大型表上创建列存储索引的最有效方法?
- java - 如何获取 JTextArea 中特定字符的精确坐标
- android - Firebase,从父母那里得到特定的孩子
- python - 在 Python 中,迭代不能按我的预期工作