代码之家  ›  专栏  ›  技术社区  ›  Андрей Сорока

使用nuxt异步加载样式

  •  0
  • Андрей Сорока  · 技术社区  · 7 年前

    如何将样式异步加载到站点。还是在页脚中插入样式?

    我使用numxt:2.0.0

    我试着:

    • 在webpack中添加插件:异步样式表webpack插件。但是,numxt中的prerender添加样式。
    • 创建模块。但是如何去除旧的风格呢?

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   Андрей Сорока    7 年前

    我开发模块:

    const STYLE_REGEXP = '<link rel="stylesheet" href="(.+?)">'
    const STYLE_TEMPLATE = '<link rel="preload" href="$1" as="style" onload="this.onload=null;this.rel=\'stylesheet\'">' +
      '<noscript><link rel="stylesheet" href="$1"></noscript>'
    
    module.exports = async function asyncCss (options) {
      this.nuxt.hook('render:route', renderRouteHandle.bind(this))
    }
    
    async function renderRouteHandle (url, result) {
      result.html = result.html.replace(
        new RegExp(STYLE_REGEXP, 'g'), STYLE_TEMPLATE)
    }
    

    帮了我的忙 https://github.com/nuxt/nuxt.js/issues/2822