6

这基本上是我正在使用的代码,lighthouse 说我的(几乎是空的!)css 包正在延迟我的初始加载。

那么如何在

<head><style>DONT_WANT_TO_WRITE_STUFF_INLINED</style>...</head>

有没有比https://www.npmjs.com/package/critical更好的解决方案或内联编写所有内容?

以及如何延迟主要styles.scss 的加载,直到在浏览器中加载预渲染的通用内容?angular-cli.json 中的服务器应用程序配置不包含stylesor assets,所以我不明白为什么最初加载 styles.scss

4

1 回答 1

0

关于延迟主要styles.scss的加载,我依靠两件事:

  • 首先,我使用--extract-css=false标志构建应用程序。这确保了样式包将是一个 JavaScript 文件。
  • 其次,我推迟加载该文件。为此,我依赖于我调用的一个小脚本,该脚本defer.styles.js在构建过程结束时运行。该脚本只是查找加载样式的脚本标签,并在defer其中添加一个。
const path = require('path');

const htmlPath = path.join(__dirname, 'dist', 'browser', 'index.html');

fs.readFile(htmlPath, 'utf8', (err, data) => {
    if (err) {
        console.log(err);
        return;
    }

    const index = data.search(`src="styles.`);

    if (index > -1) {
        const output = [data.slice(0, index), 'defer ', data.slice(index)].join('');
        console.log(output);
        fs.writeFile(htmlPath, output, 'utf8', () => {
            console.log('Styles succesfully deferred');
        })
    }
});

关于您的其他问题-如何加载关键的CSS-。我仍然没有找到一种舒适的方式。

于 2019-04-09T08:55:51.617 回答