Vue-阻止prerender-spa-plugin加载modernizr脚本

问题描述 投票:0回答:1

我正在使用Vue-cli 3 + prerender-spa-plugin构建一个生产网站,但是我需要一些只能由最终用户而不是pre-spa-plugin环境加载的脚本(例如modernizr检测)。

如何配置设置以使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>
webpack single-page-application modernizr vue-cli-3 prerender
1个回答
0
投票

搜索插件的问题,我找到了一种解决方案:

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
}
© www.soinside.com 2019 - 2024. All rights reserved.