Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service worker #210

Merged
merged 44 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
5a9ce71
ng add @angular/pwa
rosemcc Dec 20, 2021
ff86573
set theme-color
rosemcc Dec 20, 2021
ca0d9b7
replace default Angular icons with UoA
rosemcc Dec 21, 2021
3171af2
clean up codeCoverageExclude
rosemcc Dec 21, 2021
b670ea3
readme
rosemcc Dec 21, 2021
14b2e3c
Material icons and fonts caching
rosemcc Dec 21, 2021
539fb09
Revert "Material icons and fonts caching"
rosemcc Dec 21, 2021
642f587
material icons, uoa fonts, contentful assets caching, readme
rosemcc Dec 21, 2021
e71190b
lazy fetch contentful assets
rosemcc Dec 21, 2021
8c6955c
service worker on prod index.html
rosemcc Dec 21, 2021
8d75805
App name
rosemcc Dec 21, 2021
7b4b3cf
add script to run in service worker mode locally
rosemcc Dec 21, 2021
97e20e7
csp update WIP
rosemcc Dec 21, 2021
68ce33d
504 gateway timeout bypass
rosemcc Dec 22, 2021
2063161
sw updates service WIP
rosemcc Dec 22, 2021
3dd6eb6
note
rosemcc Dec 22, 2021
5954d80
unit tests passing
rosemcc Dec 22, 2021
931f14f
more csp updates due to service worker fetching
rosemcc Dec 22, 2021
4a989d6
about link fix on footer
rosemcc Dec 22, 2021
f045ca0
adding sw-updates functionality
rosemcc Dec 22, 2021
5da273e
add nontransparent icon for apple-touch-icon
rosemcc Dec 22, 2021
6b68622
replace deprecated methods, add error handling
rosemcc Jan 16, 2022
b0b91ef
Merge branch 'master' into service-worker
rosemcc Jan 19, 2022
6cebf44
Merge branch 'master' into service-worker
rosemcc Jan 19, 2022
ed7147b
package-lock fix after merge
rosemcc Jan 19, 2022
c7659da
tidying, add types
rosemcc Jan 20, 2022
3c8525d
material dialog for update confirmation
rosemcc Jan 20, 2022
12dcc20
tests passing
rosemcc Jan 20, 2022
cba547e
Change dialog to snackbar, add styling
rosemcc Jan 20, 2022
0740174
add a test
rosemcc Jan 21, 2022
0fe37ba
remove unused material modules
rosemcc Jan 21, 2022
831291d
button style
rosemcc Jan 21, 2022
dd87336
minor update to script and readme for running service worker locally
rosemcc Jan 24, 2022
183ab57
fix snackbar style
rosemcc Jan 24, 2022
5fce957
Merge branch 'master' into service-worker
rosemcc Jan 24, 2022
699ba8f
add app id to manifest
rosemcc Jan 24, 2022
c139901
refactor swUpdates, add tests
rosemcc Jan 25, 2022
9d953f7
remove f
rosemcc Jan 25, 2022
e4a5208
add maskable icons
rosemcc Jan 25, 2022
dd61966
icons fix
rosemcc Jan 25, 2022
3636e86
add apple touch icon
rosemcc Jan 25, 2022
e61d914
changes to pipe and unit test
Trombach Jan 28, 2022
6f3350d
remove unused imports
Trombach Jan 28, 2022
b6c75bd
Merge branch 'master' into service-worker
Trombach Jan 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion research-hub-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ This project comes bundled with an interactive bash CLI test runner capable of l

Simply run `./test.sh` to specify what sort of testing you would like to do.

### Preview environment
## Preview environment
Aside from the main site, a preview version of the site is also built for content authors to view draft articles before they're published. The preview version is identical to the main site aside from content query endpoints. There is a preview environment configuration file for each deployment environment. The files are kept in the `src/environments` folder, named `environment.[DEPLOYMENT ENVIRONMENT]-preview.ts`. The files determine where the frontend sends GraphQL content queries, and which requests require authentication.

The monorepo [Jenkinsfile](../Jenkinsfile) runs `ng build` to build the preview version of the site at the same time as the main site. The script passes the preview configuration file name to build the preview site. Search "preview" in the `Jenkinsfile` to understand how it's being built in the Continuous Integration process.
Expand All @@ -85,3 +85,23 @@ The generator can be executed in several ways:
* `npm run generate-watch`: Regenerates once, then watches for any changes to `.graphql` files in the [queries](./src/app/graphql/queries/) folder
* `npm run dev`: As mentioned in the [Local Development Section](#local-development), this command also executes `npm run generate-watch`

## PWA / Service Worker

This application is a Progressive Web Application (PWA) - an app which behaves like a native app but can be used by devices on various platforms (Android, iOS or desktop). When a user visits the site, they can choose to install it as an app by clicking the install button in the browser bar. PWAs use Service Workers to manage caching and a web app manifest to make it installable. PWAs have enhanced capabilities and reliability over a traditional web app, such as faster loading time and more reliable functionality offline or on slow network connections.

Angular provides a preconfigured service worker that can be added to an existing Angular app using the command `ng add @angular/pwa`. The command does the following:
* Create a service worker with a default caching configuration.
* Create a manifest file, which tells the browser how your app should behave when installed on the user's device.
* Add a link to the manifest file in index.html.
* Add the theme-color <meta> tag to index.html.
* Create app icons in the src/assets directory.
Read more info [here](https://angular.io/guide/service-worker-intro) about Angular service worker and PWA.

### Testing PWA / Service Worker locally:
Because ng serve does not work with service workers, you must use a separate HTTP server to test service worker locally. You can use http-server, for example.
Install it globally: `npm install --global http-server`.
Now you can run a build of the project (in whichever environment you want to test - e.g. `ng build --configuration dev`), and then serve the build directory (www) using http-server:
`http-server -p 8080 -c-1 www`. You can now see the service worker information in the browser developer tools. You can run the command `npm run start:sw` to do the same.

**NOTE**: not all navigation/redirects will work correctly when serving from the http-server due to the server address and environment settings etc. The best way to test the service worker is to do a proper deployment into the dev environment.

21 changes: 8 additions & 13 deletions research-hub-web/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"tsConfig": "tsconfig.build.json",
"polyfills": "src/polyfills.ts",
"assets": [
"./src/assets"
"./src/assets",
"src/manifest.webmanifest"
],
"styles": [
"src/styles.scss",
Expand All @@ -42,7 +43,9 @@
"buildOptimizer": false,
"sourceMap": true,
"optimization": false,
"namedChunks": true
"namedChunks": true,
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
},
"configurations": {
"local": {
Expand Down Expand Up @@ -363,16 +366,7 @@
"src/environments/**",
"src/**/**.module.ts",
"src/test.ts",
"src/polyfills.ts",
"src/**/search-results/**/*.ts",
"src/**/search-bar/**/*.ts",
"src/**/shared/confirm-dialog/**/*.ts",
"src/**/shared/error-dialog/**/*.ts",
"src/**/shared/markdown/**/*.ts",
"src/**/services/cer-api.service.ts",
"src/**/services/research-hub-api.service.ts",
"src/**/directives/search-result-link.directive.ts",
"src/**/pipes/list-item-to-router-link.pipe.ts"
"src/polyfills.ts"
],
"karmaConfig": "karma.conf.js",
"polyfills": "src/polyfills.ts",
Expand All @@ -382,7 +376,8 @@
"src/styles.scss"
],
"assets": [
"src/assets"
"src/assets",
"src/manifest.webmanifest"
],
"stylePreprocessorOptions": {
"includePaths": [
Expand Down
60 changes: 60 additions & 0 deletions research-hub-web/ngsw-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "./node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": [
"/assets/imgs/favicon.png?v=2",
"/index.html",
"/manifest.webmanifest",
"/*.css",
"/*.js"
],
"urls": [
"https://fonts.googleapis.com/**",
"https://fonts.gstatic.com/**",
"https://cdn.auckland.ac.nz/designs/uoa-digital/clientlibs/css/base/fonts/**"
]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**",
"/*.(svg|cur|jpg|jpeg|png|apng|webp|avif|gif|otf|ttf|woff|woff2)"
],
"urls": [
"https://images.ctfassets.net/**"
]
}
}
],
"dataGroups": [
{
"name": "cer-graphql-service",
"urls": ["*/cer-graphql-service"],
"cacheConfig": {
"maxSize": 10,
"maxAge": "1h",
"timeout": "10s",
"strategy": "freshness"
}
},
{
"name": "cer-graphql-preview-service",
"urls": ["*/cer-graphql-preview-service"],
"cacheConfig": {
"maxSize": 10,
"maxAge": "1m",
"timeout": "10s",
"strategy": "freshness"
}
}
]
}
Loading