Skip to content

HMR implementation is unnecessary intrusive into <link> nodes? #883

Open
@birdofpreyru

Description

@birdofpreyru

Modification Proposal

Expected Behavior / Situation

My expectation for HMR implementation is to be as little intrusive into DOM as possible. I believe, to reload CSS chunk it is enough to just alter <link>'s HREF by updating some query param (as it is done now), but it is not needed to remove existing <link> node and replace it by its clone.

Actual Behavior / Situation

The current implementation of updateCss() clones and replaces existing <link> nodes. I am not sure why is it necessary (and I hope you could clarify it to me), but it definitely causes a few shortcomings:

  • el.cloneNode() does not necessarily produces an exact clone of the original node. It loses custom fields attached to it, e.g. if the host code does some assignments to that node like el.fields = someValue, or el.dataset.key = someValue < these data are currently lost in the result of HMR.
  • Current HMR implementation normalizes HREF format, e.g. if the original <link> URL was /an/absolute/path, the HMR unnecessarity turns it into schema://domain/an/absolute/path?timestamp (I believe, just adding/modifying the timestamp would be enough to reload the chunk).
  • As the current implementation does newEl.href = `${url}?${Date.now()}`; it looses any custom query parameters of the <link> HREF. IMHO, if the original HREF had a query string, the HMR implementation should correctly parse and modify it, rather than throwing the original query away.

Because of these points, the current HMR implementation is likely to unnecessary interfere with the host code operating <link> nodes in some custom manner, e.g. for operating code-splitting, etc..

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

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