简体   繁体   English

升级到Nuxt 2后,每个页面的样式都会加载到主页上

[英]Every page's styles are loaded on homepage after upgrading to Nuxt 2

After upgrading to Nuxt.js 2, I noticed that about 30 CSS files are loaded when the homepage loads. 升级到Nuxt.js 2后,我注意到加载首页时会加载大约30个CSS文件。 I actually noticed it when I checked Google Pagespeed Insights and saw about 30 "blocking CSS resources". 当我检查Google Pagespeed Insights并看到大约30个“阻塞CSS资源”时,我实际上注意到了它。

Is there any setting for lazy loading them or something like that? 是否有任何设置可以延迟加载它们或类似的东西?

Nuxt2 has the code splitting and you can use the every css files in the current page only so you have 2 way for bundling css, first is the common css in the all project and second is an isolate css file for each page. Nuxt2具有代码拆分功能,您只能使用当前页面中的每个css文件,因此您有2种方法来捆绑css,第一种是all项目中的通用css,第二种是每页的独立css文件。 use the scoped attribute in the style tag. style标签中使用scoped属性。

for example: 例如:

  //////// sample.vue//////
 <template>
           write somethin.....
 </template>

 <script>
           write som,ething.....
  </script>

  <style lang="scss" scoped>
    body {
          background-color: gray;
          color: #9e9e9e;
      }
  </style>  
export default {
  build: {
    extractCSS: true,
    optimization: {
      splitChunks: {
        cacheGroups: {
          styles: {
            name: 'styles',
            test: /\.(css)$/,
            chunks: 'all',
            enforce: true
          }
        }
      }
    }
  }
}

https://github.com/nuxt/nuxt.js/issues/3166#issuecomment-423832425 https://github.com/nuxt/nuxt.js/issues/3166#issuecomment-423832425

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM