Improve loading of 3P scripts
@clydin ya está trabajando en esto.
Desde el 29/7/2021.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
🚀 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 havepolyfill.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 inrequestIdleCallback. 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 whetherinputis a local path or a remote URLstrategy-afterInteractive,beforeInteractive, orlazyOnloadinject- specifies if the script should be injected or not. We preserve the current semantics of theinjectproperty in the script objectbundleName- applicable for local scripts only. For external scripts, we can throw an errorid- an optional identifier that the developer can use to get a hold of the script and hook to itsonloadevent
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.
- Lenguaje dominante
- TypeScript
- Estrellas
- 27k
- Forks
- 11.8k
- Merge medio
- 16 h 35 min
- PR fusionados (30 d)
- 176
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de angular/angular-cli
-
Can't use an array of hostnames in --allowedHosts cli parameter in @angular/build:dev-server Abiertoarea: @angular/build gemini-triaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
angular/angular-cli#33955 ·
-
area: @angular/cli gemini-triaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
angular/angular-cli#33055 · 1 comentario · 3 reacciones ·
-
unit-test: with --coverage, a setup file's hooks reach only the first spec file of each worker Abiertoarea: @angular/build gemini-triaged
Dificultad 4/5 3-5 días Aptitud para principiantes 72/100
angular/angular-cli#34137 ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34131 · 1 asignado ·
-
angular/build:library area: @angular/build gemini-triaged
angular/angular-cli#34130 · 1 asignado ·
Todos los issues de angular/angular-cli
Issues similares
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
dennys-bd/agent-hive#184 ·
-
Add: hunch Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
AbdelStark/awesome-typesafe#104 ·
-
ai-observability bug team/ai-observability
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
vicharanashala/fln#563 ·