Skip to content

Should defer loading of as much JavaScript as possible #6539

@jdarwood007

Description

@jdarwood007

This is a discussion, not a bug report. But it would introduce some changes to the code.

I tested issuing a defer on all javascript files except the jquery file. When I did this, SMF has errors on pages because the scripts may not be available yet when they are encounter during page loading. This simply means, it is impossible currently for us to issue defer or async on javascript files to improve page loading speeds.

There is two solutions to this.

  1. We don't defer any javascript files ever, causing performance penalties as those files must finish downloading before continuing to render a page.
  2. We wrap all inline javascript in a check such as
  • $(document).ready({function() {....});
  • window.addEventListener("DOMContentLoaded", function() {....});

I think going with the second choice and using the standard javascript loader method is better because it works and will even function without jquery being loaded. Which if we wrap jquery objects in the second method as well, jquery can even be deferred.
What this means is we do not block downloading or processing of a page while the javascript file is downloading in the background and continue on with everything. With the even listener, once the page has almost finished loading, we can execute the javascript to bring in those pieces that enhance the overall usage of SMF.

I started to look around places and notice we do most of our inline javascript with just standard script tags on the pages. Instead of calling the built in function addInlineJavaScript(string $code, bool $defer)
Ideally we would move as much as we can to the inline javascript with a defer, which sends all this to the end of the page and defers it automatically with the appropriate event listener.

As it is late in RC stages, this should have a discussion before we even build a PR.
I feel it is worth getting this fixed because it allows defer on the javascript to occur, improving overall performance on a site.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions