Skip to content

Commit 6849abc

Browse files
examples(pwa) add localStorage adapter
1 parent b03a259 commit 6849abc

File tree

8 files changed

+41
-54
lines changed

8 files changed

+41
-54
lines changed

.eslintignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
esm

examples/pwa/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
"lint": "vue-cli-service lint"
99
},
1010
"dependencies": {
11-
"@vue/composition-api": "^0.4.0",
11+
"@vue/composition-api": "1.0.0-beta.17",
1212
"core-js": "^3.6.4",
1313
"register-service-worker": "^1.6.2",
14-
"swrv": "^0.1.1",
14+
"swrv": "^0.8.2",
1515
"vue": "^2.6.11"
1616
},
1717
"devDependencies": {

examples/pwa/src/components/Todo.vue

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
<template>
22
<div>
33
<div v-if="todo === undefined && !error">Loading...</div>
4-
<div v-if="error">{{error}}</div>
54
{{ todo }}
5+
<div v-if="error">{{error}}</div>
66
</div>
77
</template>
88

99
<script>
10-
import useSWRV from 'swrv'
10+
import { defineComponent } from '@vue/composition-api'
11+
import useTodos from '../useTodos'
1112
12-
export default {
13+
export default defineComponent({
1314
props: {
1415
id: {
1516
type: Number,
1617
required: true
1718
}
1819
},
1920
setup ({ id }, { root }) {
20-
const { data: todo, error } = useSWRV(`/todos/${id}`, path => root.$api(`${path}`), {
21-
cache: root.$swrvCache
22-
})
21+
const { data, error } = useTodos(root, `/todos/${id}`)
2322
2423
return {
25-
todo,
24+
todo: data,
2625
error
2726
}
2827
}
29-
}
28+
})
3029
</script>
31-
32-
<style lang="scss" scoped>
33-
</style>

examples/pwa/src/components/Todos.vue

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@
2424
</template>
2525

2626
<script>
27-
import useSWRV from 'swrv'
27+
import { defineComponent } from '@vue/composition-api'
28+
import useTodos from '../useTodos'
2829
29-
export default {
30+
export default defineComponent({
3031
setup (props, { root, emit }) {
31-
const { data: todos, error } = useSWRV(`/todos`, path => root.$api(`${path}`), {
32-
cache: root.$swrvCache
33-
})
32+
const { data: todos, error } = useTodos(root, `/todos`)
3433
3534
function viewTodo (id) {
3635
emit('view', id)
@@ -42,7 +41,7 @@ export default {
4241
viewTodo
4342
}
4443
}
45-
}
44+
})
4645
</script>
4746

4847
<!-- Add "scoped" attribute to limit CSS to this component only -->

examples/pwa/src/localStorageCache.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

examples/pwa/src/main.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import Vue from 'vue'
22
import App from './App.vue'
33
import VueCompositionApi from '@vue/composition-api'
4-
import LocalStorageCache from './localStorageCache'
54
import './registerServiceWorker'
65

76
Vue.use(VueCompositionApi)
87
Vue.config.productionTip = false
98

109
Vue.prototype.$api = path => fetch(`https://jsonplaceholder.typicode.com${path}`).then(res => res.json())
11-
Vue.prototype.$swrvCache = new LocalStorageCache()
1210

1311
new Vue({
1412
render: h => h(App)

examples/pwa/src/useTodos.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import useSWRV from '../../../esm'
2+
import LocalStorageCache from '../../../esm/cache/adapters/localStorage'
3+
export default function useTodos (root, path) {
4+
const { data, error } = useSWRV(path, path => root.$api(`${path}`), {
5+
cache: new LocalStorageCache('swrv'),
6+
shouldRetryOnError: false
7+
})
8+
9+
return {
10+
data,
11+
error
12+
}
13+
}
14+

examples/pwa/yarn.lock

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,12 +1097,12 @@
10971097
source-map "~0.6.1"
10981098
vue-template-es2015-compiler "^1.9.0"
10991099

1100-
"@vue/composition-api@^0.4.0":
1101-
version "0.4.0"
1102-
resolved "https://registry.yarnpkg.com/@vue/composition-api/-/composition-api-0.4.0.tgz#04e600106687de719d587c704296aa4a5b37ca28"
1103-
integrity sha512-8QxZK3BBbn29vIoiso+UthT6JUbeS0gPBWAxDUK2EComWNHluaGVWXC9fAnUG+L6PM+ApkFE2dOlv5Oi/o+cSQ==
1100+
"@vue/composition-api@1.0.0-beta.17":
1101+
version "1.0.0-beta.17"
1102+
resolved "https://registry.npmjs.org/@vue/composition-api/-/composition-api-1.0.0-beta.17.tgz#63b65b3ed7cab649c53e4459fb0c5d3d1c545a96"
1103+
integrity sha512-TXa3162xQXeEoT8ar2aLPdSVGUy4phD8b3bytRORJQMvplQxi++QKXKAgYUTH0lwP0t0Dw2ddWtIyTTrOhta5w==
11041104
dependencies:
1105-
tslib "^1.9.3"
1105+
tslib "^2.0.1"
11061106

11071107
"@vue/preload-webpack-plugin@^1.1.0":
11081108
version "1.1.1"
@@ -7668,11 +7668,16 @@ ts-pnp@^1.1.6:
76687668
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.1.6.tgz#389a24396d425a0d3162e96d2b4638900fdc289a"
76697669
integrity sha512-CrG5GqAAzMT7144Cl+UIFP7mz/iIhiy+xQ6GGcnjTezhALT02uPMRw7tgDSESgB5MsfKt55+GPWw4ir1kVtMIQ==
76707670

7671-
tslib@^1.9.0, tslib@^1.9.3:
7671+
tslib@^1.9.0:
76727672
version "1.10.0"
76737673
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
76747674
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==
76757675

7676+
tslib@^2.0.1:
7677+
version "2.0.3"
7678+
resolved "https://registry.npmjs.org/tslib/-/tslib-2.0.3.tgz#8e0741ac45fc0c226e58a17bfc3e64b9bc6ca61c"
7679+
integrity sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==
7680+
76767681
76777682
version "0.0.0"
76787683
resolved "https://registry.yarnpkg.com/tty-browserify/-/tty-browserify-0.0.0.tgz#a157ba402da24e9bf957f9aa69d524eed42901a6"

0 commit comments

Comments
 (0)