Skip to content

Revise <script> elements to use defer loading #43

Open

Description

@delphidabbler

Pull all <script> elements into document <head> and add defer attribute to them to improve script loading characteristics.

Activity

  1. changed the title [-]Revise `<script>` elements to user `defer` loading[/-] [+]Revise `<script>` elements to use `defer` loading[/+] on Oct 14, 2022
  2. delphidabbler commented on Oct 14, 2022

    @delphidabbler
    MemberAuthor

    Need to check how the use of defer loading affects code that calls into deferred scripts.

    For e.g., the following code is included towards the end of the body of default.html:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.0/anchor.min.js" integrity="sha256-lZaRhKri35AyJSypXXs4o6OPFTbTmUoltBbDCbdzegg=" crossorigin="anonymous"></script>
    <script>anchors.add();</script>

    If

    <script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/4.1.0/anchor.min.js" integrity="sha256-lZaRhKri35AyJSypXXs4o6OPFTbTmUoltBbDCbdzegg=" crossorigin="anonymous"></script>

    is moved to the head section and set to defer loading, will the

    <script>anchors.add();</script>

    line still work if left at the bottom of the body, or will it need moving into a script that triggers when the document has loaded?

  3. delphidabbler commented on Oct 14, 2022

    @delphidabbler
    MemberAuthor

    Another question is whether defer can be used for in place scripts like the following:

    <script>anchors.add();</script>

    I.e. is the following valid?:

    <script defer>anchors.add();</script>
  4. delphidabbler commented on Oct 14, 2022

    @delphidabbler
    MemberAuthor

    Another question is whether defer can be used for in place scripts like the following:

    <script>anchors.add();</script>
    

    I.e. is the following valid?:

    <script defer>anchors.add();</script>
    

    The answer is no, defer can't be use on such inline scripts. The following quotation is taken from the MDN web docs page for the <script> element when the defer attribute is described:

    ⚠️ Warning: This attribute must not be used if the src attribute is absent (i.e. for inline scripts), in this case it would have no effect.

    The defer attribute has no effect on module scripts — they defer by default.

  5. delphidabbler commented on Nov 15, 2022

    @delphidabbler
    MemberAuthor

    Need to check how the use of defer loading affects code that calls into deferred scripts.

    To get round this possible problem is to create a JavaScript file that loads last that calls code like

    anchors.add();
  6. delphidabbler commented on Nov 15, 2022

    @delphidabbler
    MemberAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions