Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
chore!: remove service worker altogether
Browse files Browse the repository at this point in the history
it was just too complicated
  • Loading branch information
johannschopplich committed Aug 16, 2022
1 parent 3beee02 commit 5086bd0
Show file tree
Hide file tree
Showing 20 changed files with 25 additions and 559 deletions.
1 change: 0 additions & 1 deletion .env.development.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ KIRBY_DEV_PORT=8080
KIRBY_DEV_PROTOCOL=http
VITE_DEV_PORT=3000

VITE_SERVICE_WORKER=false
VITE_STALE_WHILE_REVALIDATE=false

# API slug to fetch JSON-encoded page data from
Expand Down
1 change: 0 additions & 1 deletion .env.production.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ KIRBY_DEV_PORT=8080
KIRBY_DEV_PROTOCOL=http
VITE_DEV_PORT=3000

VITE_SERVICE_WORKER=false
VITE_STALE_WHILE_REVALIDATE=false

# API slug to fetch JSON-encoded page data from
Expand Down
Binary file modified .github/kirby-vue-3-cache-and-store.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ vendor
# Build assets
/public/assets
/public/dist
/public/service-worker.js
33 changes: 3 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
- 🔍 SEO-friendly: [server-side generated](https://github.com/johannschopplich/kirby-extended/blob/main/docs/meta.md) meta tags
- 🌐 [Multi-language](#multi-language) support
- ♿ Accessible frontend routing
- 🚝 [Offline-first](#caching--offline-capability-with-service-worker)
- 💫 [Stale-while-revalidate](#stale-while-revalidate) page data

## Alternatives
Expand Down Expand Up @@ -60,12 +59,6 @@ kirby-vue3-starterkit/
| | # Kirby's media folder for thumbnails and more (not tracked by Git)
| └── media/
|
| # Various development-related Node scripts
├── scripts/
| |
| | # Service worker generator (optional)
| └── buildServiceWorker.js
|
| # Kirby's core folder containing templates, blueprints, etc.
├── site/
| ├── config/
Expand Down Expand Up @@ -116,9 +109,6 @@ kirby-vue3-starterkit/
| | | # Returns page data for the current path, similarly to Kirby's `$page` object
| | ├── usePage.js
| | |
| | | # Various service worker methods like registering
| | ├── useServiceWorker.js
| | |
| | | # Returns a object corresponding to Kirby's global `$site`
| | └── useSite.js
| |
Expand All @@ -137,8 +127,7 @@ kirby-vue3-starterkit/
| |
| ├── App.vue
| ├── index.css
| ├── index.js
| └── serviceWorker.js
| └── index.js
|
| # Contains everything content and user data related (not tracked by Git)
├── storage/
Expand All @@ -160,20 +149,12 @@ kirby-vue3-starterkit/

</details>

## Caching & Offline Capability With Service Worker
## Caching

Even without a service worker installed, the frontend will store pages between individual routes/views. When the tab get reloaded, the data for each page is freshly fetched from the API once again.

For offline capability of your Vue app, you can choose to activate the included [service worker](src/serviceWorker.js).

A visual explanation of both methods can be found in the following flow chart:
The frontend will store pages between individual routes/views. When the tab get reloaded, the data for each page is freshly fetched from the API once again.

![Caching for Kirby and Vue 3 starterkit](./.github/kirby-vue-3-cache-and-store.png)

The service worker precaches all CSS & JS assets required by the Vue app and caches the data of every requested page. All assets are versioned and served from the service worker cache directly.

Each JSON request will be freshly fetched from the network and saved to the cache. If the user's navigator turns out to be offline, the cached page request will be returned.

## Stale-While-Revalidate

The stale-while-revalidate mechanism for the [`usePage`](src/composables/usePage.js) hook allows you to respond as quickly as possible with cached page data if available, falling back to the network request if it's not cached. The network request is then used to update the cached page data – which directly affects the view after lazily assigning changes (if any), thanks to Vue's reactivity.
Expand Down Expand Up @@ -292,14 +273,6 @@ Then, visit the panel and add new languages by your liking. The Panel **automati

Language data is provided by the global `site` object, which can be accessed via the `useSite()` hook.

### Service Worker

To enable the **service worker** which precaches essential assets and page API calls for offline capability, set:

- `VITE_SERVICE_WORKER` to `true`

> ⚠️ Don't change the `CONTENT_API_SLUG` once you deployed your app publicly and thus a service worker is installed on clients. Otherwise fetch requests will fail and a blank page will show until the new service worker is activated, which then is only possible by closing the tab/PWA.
### Stale-While-Revalidating

To keep page data fresh with **stale-while-revalidate**, set:
Expand Down
62 changes: 14 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"kirby": "env-cmd --use-shell \"php -S \\$KIRBY_DEV_HOSTNAME:\\$KIRBY_DEV_PORT -t public vendor/getkirby/cms/router.php\"",
"dev": "shx touch src/.lock && concurrently \"npm:kirby\" \"vite\"",
"build": "shx rm -f src/.lock && vite build && node scripts/buildServiceWorker.mjs",
"build": "shx rm -f src/.lock && vite build",
"lint": "eslint \"src/**/*.{js,vue}\"",
"lint:fix": "npm run lint -- --fix",
"format": "prettier --write \"src/**/*.{js,vue}\""
Expand All @@ -19,13 +19,11 @@
"@types/node": "^18.7.4",
"@vitejs/plugin-vue": "^3.0.3",
"concurrently": "^7.3.0",
"consola": "^2.15.3",
"dotenv": "^16.0.1",
"env-cmd": "^10.1.0",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-vue": "^9.3.0",
"nanoid": "^4.0.0",
"prettier": "2.7.1",
"shx": "^0.3.4",
"unplugin-vue-components": "^0.22.4",
Expand Down
36 changes: 0 additions & 36 deletions scripts/buildServiceWorker.mjs

This file was deleted.

1 change: 0 additions & 1 deletion src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</main>
</div>

<UpdateNotification />
<Footer />
</template>

Expand Down
2 changes: 1 addition & 1 deletion src/components/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useSite } from "~/composables";
const site = useSite();
</script>

<style>
<style scoped>
.footer {
padding: 1.5rem 5vw 10vh;
text-align: center;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const site = useSite();
const route = useRoute();
</script>

<style>
<style scoped>
.header {
margin-bottom: 1.5rem;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Intro.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</header>
</template>

<style>
<style scoped>
.intro {
padding: 10vh 0;
text-align: center;
Expand Down
13 changes: 4 additions & 9 deletions src/components/SkipToContentLink.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<template>
<a
ref="skipLink"
href="#main"
class="skip-to-content-link"
@click.prevent="navigate()"
>
<a ref="skipLink" href="#main" class="skip-link" @click.prevent="navigate()">
Skip to content
</a>
</template>
Expand All @@ -30,15 +25,15 @@ function navigate({ target }) {
}
</script>
<style>
.skip-to-content-link {
<style scoped>
.skip-link {
position: absolute;
top: 0;
left: 50%;
padding: 0.5rem;
transform: translate(-50%, -100%);
}
.skip-to-content-link:focus {
.skip-link:focus {
transform: translate(-50%, 0);
/* Copy the browser's native focus styles */
outline: 5px auto Highlight;
Expand Down

0 comments on commit 5086bd0

Please sign in to comment.