-
Notifications
You must be signed in to change notification settings - Fork 175
design: App Shell library #12
Comments
I know it's long, so feel free to skip to code samples, but I'd love comments from: |
Very cool to see this project start to take some shape. I will think about this some more over the weekend, but my biggest initial thoughts center around
|
Apologies for the drive-by feedback, but I'm curious to hear how updating the previously cached App Shell is going to work. What's the process for changing the service worker script in order to trigger a new Having thought about similar questions when writing Since this is described as a runtime library, I'm assuming that same approach wouldn't be possible in what you're building. |
This adds a new library, published to @angular/app-shell, which provides providers and directives to make it easier to show and hide components based on whether or not the component is being prerendered. Paired with @mgechev Part of angular#12
This adds a new library, published to @angular/app-shell, which provides providers and directives to make it easier to show and hide components based on whether or not the component is being prerendered. Paired with @mgechev Part of angular#12
@jeffbcross the App Shell library is already in master, can we close this? |
Imo it's worth getting this one closed. We've talked about the design here in person through some of our syncs while Jeff was thinking them through and last I tested app-shell support was in a decent place. cc @jeffbcross |
Scope
This issue is concerned with a runtime library to instrument an Angular component to be used to generate an App Shell.
Goal
make it easy to turn any component into a component that can be compiled into an "App Shell", i.e. an html page with inlined html, JS, styles, and images, which can be rendered as soon as the document is served, without waiting for any additional resources to load from network or disk.
Problems to solve:
Design
For the sake of this doc, let's assume developers would like to take their existing "App" component and turn it into an App Shell. The developer might start by using one of the prerender build tools from universal: gulp, grunt, webpack. The developer would incorporate these tools into their build process, providing the component, its providers, and the base html document to the prerender tool, and would generate an index.html in their final output that would contain the inlined component and CSS, and whatever scripts the base html file already contained to load the rest of the application.
For simple app components, this would work out of the box without any problems. But most components depend on some third-party library, or some code which might not be designed to be run in a nodejs build context. Additionally, the developer probably doesn't want any child routes to be included in the App Shell.
Consider this component which depends on AngularFire, and has child routes.
There are things in the template that don't belong in an application-wide app shell:
And in the component constructor, I don't want to actually execute any logic to change the route, or to try to access AngularFire data which doesn't (yet) support running in nodejs.
Proposal
npm install @angular/app-shell
IS_PRERENDER Provider
This provider will be a simple boolean value to use with
imperative code inside of component's to modify behavior if
App is being pre-rendered.
APP_SHELL_BUILD_PROVIDERS
This will be the default providers to include in the prerender configuration.
This sets the value of
IS_PRERENDER
to true when injected.APP_SHELL_RUNTIME_PROVIDERS
This will be the default providers to include when bootstrapping the app at runtime
configuration. This sets the value of
IS_PRERENDER
to false when injected.Although it is technically possible to automatically infer the environment and use
a single export of default providers, i.e.
APP_SHELL_PROVIDERS
, being explicit isbetter future-proofing for different platforms that might be supported, and better for testing.
It is also more intuitive, clearer, less verbose, and more confidence-instilling
to use this clearly-named provider group than to just provide the
IS_PRERENDER
tokenand tell the user to bind it to a boolean value.
APP_SHELL_DIRECTIVES
Set of directives to prepare a component for pre-rendering, and to help with parsing
an app shell out of a fully-compiled application with unwanted superfluous content.
*asNoRender Directive
The
*asNoRender
directive is essentially the equivalent of applying an*ngIf
directive to an element like so:<span *ngIf="!isPrerender">No Prerender</span>
.Being available as a directive saves code of having to inject the
IS_PRERENDER
constant, and attaching it to the component class. The directive also provides power
in how an app shell can be derived from a fully-rendered page at runtime, even
if the page includes content that doesn't belong in the app shell.
When this directive is used in a non-build-time context, such as in the browser
or pre-rendered at runtime with Angular Universal, this directive will cause HTML
comments to be placed at the beginning and end
of an element's contents, which can be easily parsed at runtime to throw away
non-app-shell content, so a pure app shell can be stored in the Service Worker
cache.
This is particularly useful when using server-side prerendering of components,
so no special app shell build step is necessary. The fully-rendered page can be
parsed and modified to be a general-purpose App Shell.
Example component that will be rendered at runtime by Angular Universal:
The compiled component returned from Angular Universal would look something like:
Which could easily be parsed by a runtime parser in the browser to store this app shell:
*asRender Directive
For elements that should be shown only be rendered at build time and not at runtime,
such as loading indicators or substitute elements, the
*asRender
directivecan be applied.
Runtime Parser
The runtime parser will be a small JS library to run in a Service Worker, which will parse an
html page and return a version of the page that can be cached as an app shell to be returned
on future page requests. This is particularly useful when there is server-side pre-rendering
involved, such as Angular Universal.
Example Service Worker Script:
The text was updated successfully, but these errors were encountered: