Skip to content

Avoid render-blocking requests #949

Open
@evantd

Description

@evantd

Feature Proposal

Either change the default or add an option for making CSS bundle requests non-render-blocking. This is currently possible by using a custom insert function, but an explicit option or even changing the default would be nice.

This feels like it could be risky, but I'm having trouble coming up with any concrete concerns.

Feature Use Case

Dynamically imported modules with associated CSS currently trigger warnings about render-blocking requests in Chrome's Performance Insights tool. Since webpack uses Promises to block execution of the module until the CSS chunks are loaded, the CSS chunks don't need to block any other rendering on the page.

You can see those render-blocking requests here (after DOM Content Loaded, since that's when we do the dynamic imports):

Screen Shot 2022-06-02 at 8 22 28 PM

We can prevent the requests from blocking general rendering by setting media='print' before inserting the link tag, and then switching it to media='all' once it loads. This is currently possible by using a custom insert function, like:

                      // Don't block rendering during the network fetch. Insert link tag with media=print and then only swap it to media=all once it has loaded.
                      // eslint-disable-next-line
                      insert: function (linkTag) {
                          // eslint-disable-next-line
                          var oldOnLoad = linkTag.onload;
                          linkTag.onload = function (event) {
                              linkTag.media = 'all';
                              oldOnLoad(event);
                          };
                          linkTag.media = 'print';
                          document.head.appendChild(linkTag);
                      },

Here you can see that doing so removes the render-blocking request warnings from the Performance Insights (from after DCL):

Screen Shot 2022-06-02 at 8 22 41 PM

Please paste the results of npx webpack-cli info here, and mention other relevant information

% npx webpack-cli info

  System:
    OS: Linux 5.13 Ubuntu 20.04.4 LTS (Focal Fossa)
    CPU: (8) x64 Intel(R) Xeon(R) Platinum 8259CL CPU @ 2.50GHz
    Memory: 5.32 GB / 30.90 GB
  Binaries:
    Node: 14.19.3 - ~/.volta/tools/image/node/14.19.3/bin/node
    npm: 6.14.17 - ~/.volta/tools/image/npm/6.14.17/bin/npm
  Packages:
    html-webpack-plugin: ^5.5.0 => 5.5.0
    webpack-manifest-plugin: ^5.0.0 => 5.0.0
    webpack-node-externals: ^1.7.2 => 1.7.2
    webpack-retry-chunk-load-plugin: ^2.2.0 => 2.2.0
    webpack-stats-plugin: ^1.0.3 => 1.0.3

mini-css-extract-plugin in my project is at 1.6.2, which I realize is old, but this appears to still be relevant to the code on master.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions