Make WordPress Core

Changes between Initial Version and Version 1 of Ticket #12009, comment 100


Ignore:
Timestamp:
05/09/2023 11:09:36 PM (22 months ago)
Author:
westonruter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #12009, comment 100

    initial v1  
    3939Nevertheless, note that there is a somewhat brittle connection here between the `after` inline script and the `defer` script: whether there is a `script` element in the page with the expected `id`. If there is an optimization plugin that tries concatenating `defer` scripts together, then any such `load` event handler will fail because the original `script` will be gone.
    4040
    41 In this case, it seems perhaps the safest thing to do is to not wait for the `load` event on the `script[defer]` at all, but rather to just attach a `DOMContentLoad` event handler to the `document` which runs after all `defer` scripts have been evaluated. (The only gotcha here is the `defer` script may have failed to load.)
     41In this case, it seems perhaps the safest thing to do is to not wait for the `load` event on the `script[defer]` at all, but rather to just attach a `DOMContentLoaded` event handler to the `document` which runs after all `defer` scripts have been evaluated. (The only gotcha here is the `defer` script may have failed to load.)
    4242
    4343> > I'd appreciate feedback on these three options:
     
    5151> Another possibility is to implement the "after" script in the `defer` or `async` script itself. I.e. when the script is being executed it can look for "external" object or function or data and use it when appropriate. That would be the "proper way" imho.
    5252
    53 If we require `defer` scripts to rely on the `DOMContentLoad` event, then options 1 & 2 are essentially the same, as authors should instead use `before` inline scripts. As I mentioned just above, I think option 3 may end up being unreliable due to other plugins munging `script` tags and losing the `id`. If the current approach is reworked to rely on `DOMContentLoad` instead, then this would be mitigated for option 3.
     53If we require `defer` scripts to rely on the `DOMContentLoaded` event, then options 1 & 2 are essentially the same, as authors should instead use `before` inline scripts. As I mentioned just above, I think option 3 may end up being unreliable due to other plugins munging `script` tags and losing the `id`. If the current approach is reworked to rely on `DOMContentLoaded` instead, then this would be mitigated for option 3.