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
SPA suspense works well, but SSR suspense is not ideal (with router keepAlive) #17047
Comments
Hi, This is a Vue bug, not a Quasar one. |
Hi @rstoenescu So could you have a look again? Thx ! |
At least in your example, u're not using preFetch, what is ok, since u're using suspense, but the comment about the preFetch hasn't effect here. The down side of work with suspense instead of the preFetch, is the preFetch will skip if that was be executed in the server side. src/composables/prefetch import { type Ref, type InjectionKey, inject } from 'vue'
export const isFetchEnabledKey: InjectionKey<Ref<boolean>> = Symbol('is-fetch-enabled-key')
export function useIsFetchEnabled () {
const isFetchEnabled = inject(isFetchEnabledKey)
if (!isFetchEnabled) {
throw 'is fetch enabled was not be injected'
}
return isFetchEnabled
}
export async function usePreFetch <T> (fetchFn: () => Promise<T>): Promise<T | undefined> {
const isFetchEnabled = useIsFetchEnabled()
if (isFetchEnabled.value) {
return await fetchFn()
}
} src/boot/prefetch import { boot } from 'quasar/wrappers'
import { isFetchEnabledKey } from 'src/composables/prefetch'
import { ref } from 'vue'
export default boot(({ app, router }) => {
const isFetchEnabled = ref(process.env.MODE !== 'ssr' || !!process.env.SERVER)
app.provide(isFetchEnabledKey, isFetchEnabled)
if (process.env.MODE === 'ssr' && !!process.env.CLIENT) {
router.isReady().then(() => {
setTimeout(() => {
isFetchEnabled.value = true
}, 0)
})
}
}) so, in your async comp: import { useSampleStore } from 'src/stores/sample'
import { usePreFetch } from 'src/composables/prefetch'
const sampleStore = useSampleStore()
await usePreFetch(() => sampleStore.fetchSamples()) I didn't tried that with Vite 5, but with the Vite 2, there was some inconsistencies with vue-router + ssr + suspense, specially when i tried to use async components in the router (a.k.a. Pages and Layouts) NOT WORK: suspense directly in the router component WORK: suspense in the component imported by the router component |
What happened?
$
quasar dev
(SPA mode - no problem)When the web page show, take turn to click "first" & "second" button quickly and immediately.
suspense #fallback template works well. It renders a loading state until async component to be resolved. Perfect.
$
quasar dev -m ssr
(SSR mode - two problems)Problem 1: It can not render a loading state when the web page show (first route is in pending);
Problem 2: When the web page show, take turn to click "first" & "second" button quickly and immediately. No loading state and Browser console Error happens like:
vue-router.mjs:3479 TypeError: Cannot read property 'shapeFlag' of null
and
TypeError: Cannot read property 'parentNode' of null
What did you expect to happen?
SSR suspense works well like SPA (with router keepAlive), no error, render loading state as expect
Reproduction URL
https://github.com/fengjac/spa-ssr-suspense-keepalive-demo
How to reproduce?
$ pnpm create quasar
√ What would you like to build? » App with Quasar CLI, let's go!
√ Project folder: ... spa-ssr-suspense-keepalive-demo
√ Pick Quasar version: » Quasar v2 (Vue 3 | latest and greatest)
√ Pick script type: » Typescript
√ Pick Quasar App CLI variant: » Quasar App CLI with Vite 5 (BETA | next major version - v2)
√ Package name: ... spa-ssr-suspense-keepalive-demo
√ Project product name: (must start with letter if building mobile apps) ... Quasar App
√ Project description: ... A Quasar Project
√ Pick a Vue component style: » Composition API with <script setup>
√ Pick your CSS preprocessor: » Sass with SCSS syntax
√ Check the features needed for your project: » Linting (vite-plugin-checker + ESLint + vue-tsc)
√ Pick an ESLint preset: » Prettier
Add two routers and use keepAlive and suspense
Flavour
Quasar CLI with Vite (@quasar/cli | @quasar/app-vite)
Areas
SPA Mode, SSR Mode
Platforms/Browsers
Chrome
Quasar info output
Relevant log output
Additional context
No response
The text was updated successfully, but these errors were encountered: