Open jeffbcross opened 8 years ago
I know it's long, so feel free to skip to code samples, but I'd love comments from: @addyosmani @mgechev @gdi2290 @jeffwhelpley @alxhub @robwormald
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 *asNoRender
. In short, I would like to expand the idea of *asNoRender
to a more generic mechanism for configuring whether a component renders in a particular container/platform/context. At a high level, the ideal is to have a declarative way of specifying whether a component should or should not render in a particular container. With services, this is very easy since we can use DI and simply bootstrap different implementations for different environments. We can't use this approach with components, though, since they aren't injected. As a workaround, Patrick and I have started to use a hack with the package.json browser field, but this solution has a lot of limitations. A more ideal solution would be allow developers to specify either within templates (as you have here) or within component decorator config the target containers. I will think about what the API for this type of thing would be, but it would basically involve the following changes:
IS_PRERENDER
token, you would have a CONTAINER
token that has a value of a string which is the name of the target container.*ngIf
in templates or perhaps some convenience decorators. In fact, I guess we could even still use *asNoRender
but it would use the CONTAINER
token under the scenes instead of IS_PRERENDER
.@Component
decorator. Something like displayInContainers: ['browser', 'prerenderer', 'webserver']
. We could also add convenience values like onlyBrowser: true
.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 install
event?
Having thought about similar questions when writing sw-precache
, I ended up with an approach that required build-time integration and included the hashes of each individual resource inline within the generated service worker script, so that any changes to resources would in turn result in a change to the service worker script.
Since this is described as a runtime library, I'm assuming that same approach wouldn't be possible in what you're building.
@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 is better 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
token and 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 theIS_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
directive can 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: