Skip to content

Commit 6e5ae6b

Browse files
committed
chore: add example with nuxt and pinia colada
1 parent bab9d0b commit 6e5ae6b

File tree

12 files changed

+160
-33
lines changed

12 files changed

+160
-33
lines changed

examples/nuxt/app.config.ts

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

examples/nuxt/app.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const state = useState('custom-date', () => new Date().toUTCString())
1010
<NuxtLink to="/">Home</NuxtLink>
1111
|
1212
<NuxtLink to="/users/24" v-slot="{ route }">{{ route.href }}</NuxtLink>
13+
|
14+
<NuxtLink to="/users/colada-24" v-slot="{ route }">{{
15+
route.href
16+
}}</NuxtLink>
1317
</nav>
1418
<NuxtPage />
1519
</template>

examples/nuxt/nuxt.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export default defineNuxtConfig({
22
devtools: { enabled: true },
33

4+
modules: ['@pinia/nuxt'],
5+
46
experimental: {
57
typedPages: true,
68
},

examples/nuxt/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
"preview": "nuxt preview"
88
},
99
"devDependencies": {
10+
"@pinia/nuxt": "^0.5.4",
1011
"nuxt": "^3.13.1",
1112
"unplugin-vue-router": "workspace:*"
13+
},
14+
"dependencies": {
15+
"@pinia/colada": "^0.9.0",
16+
"pinia": "^2.2.2"
1217
}
1318
}

examples/nuxt/pages/users/[id].vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const useUserData = defineBasicLoader(
2424
defineOptions({
2525
__loaders: [useUserData],
2626
})
27+
2728
const { data: user } = useUserData()
2829
const route = useRoute('users-id')
2930
</script>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<script lang="ts">
2+
import { defineColadaLoader } from 'unplugin-vue-router/data-loaders/pinia-colada'
3+
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
4+
5+
export const useUserData = defineColadaLoader('users-colada-userId', {
6+
key: (to) => ['user', to.params.userId],
7+
async query(to) {
8+
console.log('fetching user...')
9+
await delay(1000)
10+
const user = {
11+
id: to.params.userId,
12+
when: Date.now(),
13+
n: Math.round(Math.random() * 10000),
14+
name: 'John Doe',
15+
}
16+
console.table(user)
17+
return user
18+
},
19+
// TODO: could display existing data
20+
// lazy: (to, from) => !from || to.name !== from.name,
21+
lazy: false,
22+
})
23+
</script>
24+
25+
<script setup lang="ts">
26+
defineOptions({
27+
__loaders: [useUserData],
28+
})
29+
30+
const { data: user, error, isLoading, refresh } = useUserData()
31+
const route = useRoute('users-colada-userId')
32+
</script>
33+
34+
<template>
35+
<div>
36+
<h1>User {{ route.params.userId }}</h1>
37+
<p v-if="error">An error ocurred: {{ error.message }}</p>
38+
39+
<!-- display both -->
40+
<h3>Display both</h3>
41+
<p v-if="isLoading">Loading fresh data...</p>
42+
<pre v-if="user">{{ user }}</pre>
43+
44+
<hr />
45+
<h3>Display one of them</h3>
46+
<template v-if="user">
47+
<p v-if="isLoading">Loading fresh data...</p>
48+
<pre v-else>{{ user }}</pre>
49+
</template>
50+
<template v-else>
51+
<p v-if="isLoading">Loading fresh data...</p>
52+
<div v-else>
53+
<p>Something went wrong...</p>
54+
<p v-if="error">{{ error }}</p>
55+
<button @click="refresh()">Retry</button>
56+
</div>
57+
</template>
58+
</div>
59+
</template>

examples/nuxt/plugins/data-loaders.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
DataLoaderPlugin,
3+
type DataLoaderPluginOptions,
4+
} from 'unplugin-vue-router/data-loaders'
5+
6+
export default defineNuxtPlugin({
7+
name: 'data-loaders',
8+
dependsOn: ['nuxt:router'],
9+
setup(nuxtApp) {
10+
nuxtApp.vueApp.use(DataLoaderPlugin, {
11+
router: nuxtApp.vueApp.config.globalProperties.$router,
12+
isSSR: import.meta.server,
13+
14+
errors(reason) {
15+
console.error('[Data Loaders]', reason)
16+
return false
17+
},
18+
} satisfies DataLoaderPluginOptions)
19+
},
20+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { serializeTreeMap, TreeMapNode, reviveTreeMap } from '@pinia/colada'
2+
3+
/**
4+
* Handles Firestore Timestamps, GeoPoint, and other types that needs special handling for serialization.
5+
*/
6+
export default definePayloadPlugin(() => {
7+
definePayloadReducer('PiniaColada_TreeMapNode', (data: unknown) => {
8+
if (data instanceof TreeMapNode) {
9+
const serialized = serializeTreeMap(data)
10+
// console.error('🥣', serialized)
11+
return serialized
12+
} else if (
13+
data &&
14+
typeof data === 'object' &&
15+
'children' in data &&
16+
'value' in data
17+
) {
18+
console.log('🥲 not TreeMapNode', data)
19+
// console.log([...data])
20+
// console.log([...data])
21+
// return serialize(data)
22+
}
23+
})
24+
// TODO: pinia colada shouldn't revive itself
25+
definePayloadReviver(
26+
'PiniaColada_TreeMapNode',
27+
(data: ReturnType<typeof serializeTreeMap>) => {
28+
return markRaw(reviveTreeMap(data))
29+
}
30+
)
31+
})

examples/nuxt/plugins/pinia-colada.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { PiniaColada, type PiniaColadaOptions } from '@pinia/colada'
2+
3+
export default defineNuxtPlugin({
4+
dependsOn: ['pinia'],
5+
setup(nuxtApp) {
6+
nuxtApp.vueApp.use(PiniaColada, {
7+
// for some reason there is no autocomplete
8+
} satisfies PiniaColadaOptions)
9+
},
10+
})

examples/nuxt/plugins/uvr.ts

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

0 commit comments

Comments
 (0)