Skip to content

Commit 4d5b9c7

Browse files
flolualexeagle
authored andcommitted
feat(example): add full pwa support
1 parent e3698d2 commit 4d5b9c7

23 files changed

Lines changed: 212 additions & 62 deletions

examples/angular/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ load("@k8s_deploy//:defaults.bzl", "k8s_deploy")
22

33
package(default_visibility = ["//:__subpackages__"])
44

5+
exports_files(["favicon.ico"])
6+
57
# ts_library uses the `//:tsconfig.json` target
68
# by default. This alias allows omitting explicit tsconfig
79
# attribute.

examples/angular/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This example is a monorepo, meant to show many different features and integratio
2020
- **Differential loading**: in production mode, we load a pair of `<script>` tags. Modern browsers will load code in the ES2015 syntax, which is smaller and requires fewer polyfills. Older browsers will load ES5 syntax.
2121
- **Docker**: see below where we package up the production app for deployment on Kubernetes.
2222
- **Server Side Rendering**: with the help of Angular Universal you can render your application on the server
23+
- **Progressive Web App**: support for service worker and the app can be installed on phones and desktops (also has 90+ Lighthouse score)
2324

2425
This example is deployed at https://bazel.angular.io/example
2526

examples/angular/favicon.ico

15 KB
Binary file not shown.

examples/angular/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
"@angular/platform-browser-dynamic": "10.0.2",
1919
"@angular/platform-server": "10.0.2",
2020
"@angular/router": "10.0.2",
21+
"@angular/service-worker": "10.0.2",
2122
"@ngrx/store": "9.2.0",
2223
"@nguniversal/express-engine": "^9.0.0",
24+
"compression": "^1.7.4",
2325
"date-fns": "1.30.1",
2426
"rxjs": "6.5.3",
2527
"systemjs": "6.1.2",

examples/angular/src/BUILD.bazel

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ load("@npm//history-server:index.bzl", "history_server")
1010
load("@npm//html-insert-assets:index.bzl", "html_insert_assets")
1111
load("//tools:angular_prerender.bzl", "ng_prerender", "ng_prerender_test")
1212
load("//tools:angular_ts_library.bzl", "ng_ts_library")
13+
load("//tools:ngsw_config.bzl", "ngsw_config")
1314

1415
package(default_visibility = ["//:__subpackages__"])
1516

@@ -73,6 +74,8 @@ filegroup(
7374
_ASSETS = [
7475
# This label references an output of the "styles" sass_binary above.
7576
":styles.css",
77+
":manifest.webmanifest",
78+
"//:favicon.ico",
7679

7780
# Directly reference a file that came from @angular/material npm package
7881
"@npm//:node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
@@ -244,30 +247,34 @@ pkg_web(
244247
":inject_scripts_for_prod_hello_route_index",
245248
":inject_scripts_for_prod_todos_route_index",
246249
"//src/assets",
250+
":robots.txt",
251+
# Service worker
252+
"@npm//:node_modules/@angular/service-worker/ngsw-worker.js",
247253
# Include polyfills that will be requested by old browsers
248254
"@npm//:node_modules/systemjs/dist/system.js",
249255
"@npm//:node_modules/core-js/client/core.min.js",
250-
"index.html",
256+
# NOW needed ?"index.html",
251257
],
252258
# In production mode we serve some polyfills with script tags that have hard-coded paths in the index.html
253259
# so we must serve them at that path, by stripping a prefix
254260
additional_root_paths = [
255261
"npm/node_modules/core-js/client",
256262
"npm/node_modules/systemjs/dist",
263+
"npm/node_modules/@angular/service-worker",
257264
],
258265
)
259266

267+
ngsw_config(
268+
name = "pwa",
269+
src = ":prodapp",
270+
config = "//src:ngsw-config.json",
271+
index_html = ":inject_scripts_for_prod",
272+
)
273+
260274
history_server(
261275
name = "prodserver",
262-
data = [":prodapp"],
263-
# '-a src/prodapp' will ask history-server to scan for all apps under the
264-
# given folder this will result in the following auto-configuration:
265-
# /example => src/prodapp/example
266-
# / => src/prodapp
267-
templated_args = [
268-
"-a",
269-
"$$(rlocation $(rootpath :prodapp))",
270-
],
276+
data = [":pwa"],
277+
templated_args = ["-a $$(rlocation $(rootpath :pwa))"],
271278
)
272279

273280
nodejs_image(
@@ -288,21 +295,20 @@ container_image(
288295

289296
ts_library(
290297
name = "universal_server_lib",
291-
srcs = [
292-
"server.ts",
293-
],
298+
srcs = ["server.ts"],
294299
deps = [
295300
"//src/app:app_server",
296301
"@npm//@nguniversal/express-engine",
297302
"@npm//@types/node",
303+
"@npm//compression",
298304
"@npm//express",
299305
],
300306
)
301307

302308
nodejs_binary(
303309
name = "universal_server",
304310
data = [
305-
":prodapp",
311+
":pwa",
306312
":universal_server_lib",
307313
],
308314
entry_point = ":server.ts",

examples/angular/src/app/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ng_ts_library(
1919
"@npm//@angular/core",
2020
"@npm//@angular/platform-browser",
2121
"@npm//@angular/router",
22+
"@npm//@angular/service-worker",
2223
"@npm//@ngrx/store",
2324
],
2425
)

examples/angular/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {NgModule} from '@angular/core';
33
import {BrowserModule} from '@angular/platform-browser';
44
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
55
import {StoreModule} from '@ngrx/store';
6+
import { ServiceWorkerModule } from '@angular/service-worker';
67

78
import {MaterialModule} from '../shared/material/material.module';
89

@@ -16,7 +17,8 @@ import {todoReducer} from './todos/reducers/reducers';
1617
imports: [
1718
AppRoutingModule, BrowserModule, BrowserAnimationsModule, MaterialModule, HomeModule,
1819
StoreModule.forRoot({todoReducer}),
19-
BrowserModule.withServerTransition({ appId: 'angular-bazel-example' })
20+
BrowserModule.withServerTransition({ appId: 'angular-bazel-example' }),
21+
ServiceWorkerModule.register('ngsw-worker.js')
2022
],
2123
exports: [AppComponent],
2224
bootstrap: [AppComponent],

examples/angular/src/assets/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ filegroup(
44
name = "assets",
55
srcs = glob([
66
"*.svg",
7-
"*.png",
7+
"**/*.png",
88
"*.css",
99
]),
1010
)
2.85 KB
Loading
4.44 KB
Loading

0 commit comments

Comments
 (0)