GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve loading of 3P scripts · Issue #21229 · angular/angular-cli

Improve loading of 3P scripts #21229

New issue
Copy link
New issue
Copy link
Open
Enhancement
Open
Improve loading of 3P scripts#21229
Enhancement
Copy link
Assignees
clydin
Labels
area: @angular-devkit/build-angulardevkit/build-angular:browserfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
Milestone
Backlog

Description

@mgechev
mgechev
opened on Jun 28, 2021
Issue body actions

🚀 Feature request

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Description

Loading third-party scripts in a suboptimal way can cause performance regressions. A typical example is adding an SDK as script tag directly to the page head, which will block the loading of first-party scripts and delay hydration/CSR if done incorrectly.

Describe the solution you'd like

Expand scripts the functionality already available in angular.json, allowing developers to configure different script loading strategies. Project Aurora identified three script loading strategies that we can reuse:

  • afterInteractive - this strategy would prioritize first-party scripts by executing third-party scripts after the page has been hydrated or client-side rendered.
  • beforeInteractive - execute third-party scripts before first-party scripts. This strategy is particularly important for loading polyfills. Since we already have polyfill.ts, I'm a little hesitant to include it in the Angular CLI as part of the first feature iteration. I'd suggest keeping it out of scope and collecting feedback in the meantime.
  • lazyOnload - lowest priority. We load these scripts and execute them in requestIdleCallback. Examples for libraries using this strategy for prefetching are quicklink, ngx-quicklink, and guess-js.

We'd sometimes need a hook after the script has been loaded and executed if we load it asynchronously (afterInteractive or lazyOnload). For example, rendering a payment dialog after we've downloaded a third-party SDK.

Using angular.json does not provide an evident approach that would let us accomplish this. A lower-level API that would provide the necessary functionality is to allows developers to specify id's in the script declaration. Using the id, developers will discover the script element in their code and hook to its onload event.

This way, the scripts property in angular.json would change from an array of strings or objects to an array of strings or objects with the following properties:

  • input - the source URL or path of the script. The CLI will perform different actions depending on whether input is a local path or a remote URL
  • strategy - afterInteractive, beforeInteractive, or lazyOnload
  • inject - specifies if the script should be injected or not. We preserve the current semantics of the inject property in the script object
  • bundleName - applicable for local scripts only. For external scripts, we can throw an error
  • id - an optional identifier that the developer can use to get a hold of the script and hook to its onload event

Suppose we find a string rather than an object for a given script. In that case, we can treat it as a script object with an input the specified string, strategy equal to beforeInteractive for the current major, or afterInteractive for the next major. Ideally, we'd want the default strategy to be afterInteractive to prevent folks from causing performance regressions, but since this would be a breaking change, we don't want to do this until v13.

In v13, we can migrate all the scripts from objects to strings using the beforeInteractive strategy.

Describe alternatives you've considered

Project Aurora considered using a script component for alternative frameworks. Their approach easier accommodates the onLoad callback use case but seems to fit less naturally in the Angular CLI model.

Reacted by Addy Osmani, Enea Jahollari, Janicklas Ralph, AwesomeObserver and Minko Gechev

Activity

  1. mgechev
    added
    featureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues
    on Jun 28, 2021
  2. dgp1130
    added
    needs: discussionOn the agenda for team meeting to determine next stepsOn the agenda for team meeting to determine next steps
    on Jun 29, 2021
  3. alan-agius4
    added
    area: @angular-devkit/build-angular
    devkit/build-angular:browser
    on Jun 29, 2021
  4. ngbot
    added this to the Backlog milestone on Jun 29, 2021
  5. mgechev commented on Jul 15, 2021

    @mgechev
    mgechev
    on Jul 15, 2021
    MemberAuthor
    More actions

    Based on our discussion today, I want to add a few more details.

    Motivation

    Currently, folks can accomplish the same by updating index.html manually and adding script references. This technique, however, doesn't guarantee that people will be following best practices. Many application developers add blocking scripts to the page head, which causes regression of CWV.

    Additionally, best practices for loading scripts evolve, which means that to get the best from CWV, developers will have to be constantly following the progress of these low-level browser APIs.

    Conformance

    As part of the solution, we can introduce a conformance rule that will fail the build if folks are loading blocking scripts in the application head. Even though this would be helpful, we still don't provide a solution that aligns with the best set of practices for script loading.

    Ideally, we'd want to have both:

    • Functionality for properly loading scripts with proper defaults
    • Conformance to ensure that index.html is free of blocking scripts
  6. dgp1130 commented on Jul 29, 2021

    @dgp1130
    dgp1130
    on Jul 29, 2021
    Collaborator
    More actions

    After some discussion with the Web SDK team, there are some concerns about not optimizing scripts which are directly authored in the index.html file. We'll need conformance tooling to enforce this anyways, so the main question is around how we want to evolve the angular.json script loading.

    After some evaluation of the API, we're not entirely sure how this should continue to fit in to Angular tooling and how we should evolve the API over time. So we're planning to write an RFC to gather additional information about how devs currently use angular.json script loading to better understand how we can optimize those scripts and how script loading should fit into Angular tooling. @clydin offered to write an initial RFC proposal for this.

  7. dgp1130
    assigned
    clydin
    on Jul 29, 2021
  8. dgp1130
    removed
    needs: discussionOn the agenda for team meeting to determine next stepsOn the agenda for team meeting to determine next steps
    on Jul 29, 2021
  9. alan-agius4 commented on Oct 3, 2023

    @alan-agius4
    alan-agius4
    on Oct 3, 2023
    Collaborator
    More actions

    @mgechev, is this still something we want to do?

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

Metadata

Metadata

Assignees

  • @clydin
    clydin

Labels

area: @angular-devkit/build-angulardevkit/build-angular:browserfeatureLabel used to distinguish feature request from other issuesLabel used to distinguish feature request from other issues

Type

Enhancement

Projects

No projects

    Milestone

    • BacklogNo due date

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.