Skip to content

Commit

Permalink
feat: implement Vue Query SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitano committed Oct 9, 2023
1 parent df6266c commit fe503aa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
"prettier.enable": false,
"editor.formatOnSave": false,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"files.associations": {
"*.css": "tailwindcss"
},
"editor.quickSuggestions": {
"strings": true
},
"[vue]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
}
}
6 changes: 5 additions & 1 deletion pages/products.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { useQuery } from '@tanstack/vue-query'
const url = 'https://dummyjson.com/products'
const { isLoading, isError, data, error } = useQuery({
const { isLoading, isError, data, error, suspense } = useQuery({
queryKey: ['products'],
queryFn: () => {
return fetch(url).then(res => res.json())
},
})
onServerPrefetch(async () => {
await suspense()
})
</script>

<template>
Expand Down
28 changes: 25 additions & 3 deletions plugins/vue-query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
import { VueQueryPlugin } from '@tanstack/vue-query'
import type { DehydratedState, VueQueryPluginOptions } from '@tanstack/vue-query'
import { QueryClient, VueQueryPlugin, dehydrate, hydrate } from '@tanstack/vue-query'
import { useState } from '#app'

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(VueQueryPlugin)
export default defineNuxtPlugin((nuxt) => {
const vueQueryState = useState<DehydratedState | null>('vue-query')

// Modify your Vue Query global settings here
const queryClient = new QueryClient({
defaultOptions: { queries: { staleTime: 5000 } },
})
const options: VueQueryPluginOptions = { queryClient }

nuxt.vueApp.use(VueQueryPlugin, options)

if (process.server) {
nuxt.hooks.hook('app:rendered', () => {
vueQueryState.value = dehydrate(queryClient)
})
}

if (process.client) {
nuxt.hooks.hook('app:created', () => {
hydrate(queryClient, vueQueryState.value)
})
}
})

1 comment on commit fe503aa

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for morpheme-nuxt-starter ready!

✅ Preview
https://morpheme-nuxt-starter-c7nucrjl6-gravitano.vercel.app

Built with commit fe503aa.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.