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

Add service worker for offline caching #9

Merged
merged 2 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
key: yarn-cache-${{ runner.os }}-${{ steps.yarn-version.outputs.YARN_VERSION }}-${{ hashFiles('yarn.lock') }}
- run: yarn --frozen-lockfile
- run: yarn allow-scripts
- run: yarn build
- run: yarn build:prod
- run: yarn lint
- run: yarn test
- name: Validate RC changelog
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Run allowed npm lifecycle scripts
run: yarn allow-scripts
- name: Run build script
run: yarn build
run: yarn build:prod
- name: Deploy to `${{ inputs.destination_dir }}` directory of `gh-pages` branch
uses: peaceiris/actions-gh-pages@068dc23d9710f1ba62e86896f84735d869951305
with:
Expand Down
29 changes: 29 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const browserify = require('browserify');
const minifyStream = require('minify-stream');
const exorcist = require('exorcist');
const { copy } = require('fs-extra');
const { generateSW } = require('workbox-build');
const babelConfig = require('../.babelrc.json');

const rootDirectory = path.resolve(__dirname, '..');
const distDirectory = path.join(rootDirectory, 'dist');
Expand Down Expand Up @@ -36,6 +38,30 @@ const filesFromPackages = [
},
];

/**
* Generate a service worker using Workbox.
*
* Documentation about the `generateSW` function can be found here:
* {@link https://developer.chrome.com/docs/workbox/modules/workbox-build/#generatesw }
*
* This service worker will pre-cache all essential components of the page. It does not perform
* any runtime caching.
*/
async function generateServiceWorker() {
await generateSW({
babelPresetEnvTargets: babelConfig.presets[0][1].targets.browsers,
cacheId: 'phishing-warning-page',
cleanupOutdatedCaches: true,
// Pre-cache CSS, HTML, SVG, and JavaScript files,
// The fonts and the favicon are conditionally fetched and not strictly necessary.
globDirectory: distDirectory,
globPatterns: ['**/*.{css,html,js,svg}'],
// eslint-disable-next-line node/no-process-env
mode: process.env.NODE_ENV,
swDest: path.join(distDirectory, 'service-worker.js'),
});
}

/**
* Build a JavaScript bundle for the phishing warning page.
*/
Expand Down Expand Up @@ -65,5 +91,8 @@ async function main() {
errorOnExist: true,
}),
]);

await generateServiceWorker();
}

main().catch(console.error);
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const config: Config.InitialOptions = {
global: {
branches: 20,
functions: 50,
lines: 74,
statements: 74,
lines: 67,
statements: 67,
},
},

Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
],
"scripts": {
"build": "node ./build/index.js",
"build:prod": "NODE='production' yarn build",
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,ts",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern",
"prepublishOnly": "yarn build && yarn lint && yarn test",
"prepublishOnly": "yarn build:prod && yarn lint && yarn test",
"setup": "yarn install && yarn allow-scripts",
"test": "jest",
"test:watch": "jest --watch"
Expand Down Expand Up @@ -66,7 +67,8 @@
"terser": "^5.13.1",
"ts-jest": "^28.0.1",
"ts-node": "^10.7.0",
"typescript": "~4.4.4"
"typescript": "~4.4.4",
"workbox-build": "^6.5.3"
},
"engines": {
"node": ">=14.0.0"
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ function createRandomId(): number {
}

window.document.addEventListener('DOMContentLoaded', start);
window.addEventListener('load', async () => {
if ('serviceWorker' in navigator) {
try {
await navigator.serviceWorker.register('./service-worker.js');
console.log('Service worker registered!');
} catch (error) {
console.warn('Error registering service worker:');
console.warn(error);
}
}
});

/**
* Initialize the phishing warning page.
Expand Down
Loading