Skip to content

Offline Mode di Svelte Kit dengan Workbox #6

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

Open
mzaini30 opened this issue May 9, 2021 · 0 comments
Open

Offline Mode di Svelte Kit dengan Workbox #6

mzaini30 opened this issue May 9, 2021 · 0 comments
Labels

Comments

@mzaini30
Copy link
Owner

mzaini30 commented May 9, 2021

Install:

pnpm i -D workbox-cli

Buat file workbox-config.cjs yang isinya:

module.exports = {
	globDirectory: 'build/',
	globPatterns: [
		'**/*.{css,js,html}'
	],
	ignoreURLParametersMatching: [
		/^utm_/,
		/^fbclid$/
	],
	swDest: 'build/sw.js'
};

Buat file static/registerSW.js yang isinya:

if('serviceWorker' in navigator) {window.addEventListener('load', () => {navigator.serviceWorker.register('/sw.js', { scope: '/' })})}

Tambahkan di src/app.html:

<script src="/registerSW.js"></script>

Edit di package.json:

{
  "name": "~TODO~",
  "version": "0.0.1",
  "scripts": {
    "dev": "svelte-kit dev",
+    "build": "svelte-kit build && workbox generateSW workbox-config.cjs",
    "preview": "svelte-kit preview"
  },
  "devDependencies": {
    "@sveltejs/adapter-static": "^1.0.0-next.9",
    "@sveltejs/kit": "next",
    "axios": "^0.21.1",
    "qs": "^6.10.1",
    "replace": "^1.2.1",
    "svelte": "^3.34.0",
    "workbox-cli": "^6.1.5"
  },
  "type": "module"
}
@mzaini30 mzaini30 added the svelte label May 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant