将自定义Modernizr构建添加到Gatsby网站

时间:2019-04-09 11:29:36

标签: gatsby modernizr

向Gatsby网站添加自定义Modernizr构建的最佳方法是什么?

一个想法是在modernizr.js文件夹中包含一个static/文件,然后在该布局组件中包含一个脚本标签,用于将该文件导入。但这似乎不是最符合盖茨比方式的方法。

任何其他建议将非常有帮助!

2 个答案:

答案 0 :(得分:0)

他们有一个npm软件包modernizr。也许试试看?

/src/pages/index.js中添加:

// Listen to a test, give it a callback
Modernizr.on('testname', function( result ) {
  if (result) {
    console.log('The test passed!');
  }
  else {
    console.log('The test failed!');
  }
});

答案 1 :(得分:0)

将以下代码添加到您的gatsby-ssr.js

const React = require("react")
export const onRenderBody = ({ setHeadComponents }, pluginOptions) => {
  setHeadComponents([
    <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>,
  ])
}

这将加载Modenizr脚本并执行