Skip to content

Commit

Permalink
fix: missing store when render at server side
Browse files Browse the repository at this point in the history
  • Loading branch information
Gem Xli committed Mar 1, 2021
1 parent 7dfa07a commit 0e679c8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 51 deletions.
24 changes: 17 additions & 7 deletions client/src/hooks/useUniversalFetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RouteLocationNormalizedLoaded, useRoute } from 'vue-router'
import { Store } from 'vuex'
import { ISiteState } from '~/store'
import { isSSR } from '~/utils'

interface IUniversalFetchData {
data?: any
Expand All @@ -7,19 +10,26 @@ interface IUniversalFetchData {

export async function useUniversalFetch(
to: RouteLocationNormalizedLoaded,
fetch: () => any | Promise<any>
fetch: (store: Store<ISiteState>) => any | Promise<any>
) {
const state: IUniversalFetchData | null = to.meta.state as any
const meta = to.meta as any
const state: IUniversalFetchData | null = meta.state

if (state?.data && !state?.__used) {
state.__used = true
return state.data
const isFetchedInServerSide = !!(state?.data && !state?.__used)

if (isFetchedInServerSide) {
state!.__used = true
return state!.data
}

const data = await fetch()
// todo: test
const store = meta.store

const data = await fetch(store)

to.meta.state = {
data,
__used: false,
__used: !isSSR,
}

return data
Expand Down
37 changes: 12 additions & 25 deletions client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import './style/app.css'
import viteSSR from 'vite-ssr'
import routes from 'voie-pages'
import { i18n } from 'vite-i18n-plugin'
import { useI18n } from 'vue-i18n'
// import { useI18n } from 'vue-i18n'
import { createStore } from './store'
import { isSSR, sharedData } from './utils'
import { isSSR } from './utils'
import App from './App.vue' // Vue or React main app
import { Router } from 'vue-router'
import { NProgressPlugin } from './plugins'
Expand All @@ -14,41 +14,28 @@ export default viteSSR(
App,
{ routes },
async ({ app, initialState, router }) => {
NProgressPlugin(router)

app.use(i18n)

const store = createStore()
app.use(store)

const r: Router = router

if (isSSR) {
await store.dispatch('serverInit')
initialState.storeState = store.state
} else {
// console.log('routes', routes)
store.replaceState(initialState.storeState)
// sync data that fetched in `beforeRouteEnter`
r.currentRoute.value.meta.state = initialState.state
}

NProgressPlugin(router)

app.use(i18n)

Object.defineProperty(app.config.globalProperties, 't', {
get: () => {
const { t } = useI18n()
return t
},
})

Object.defineProperty(app.config.globalProperties, 'locale', {
get: () => {
const { locale } = useI18n()
return locale
},
// provide app/store context for `beforeRouteEnter` hook
r.beforeEach((to) => {
to.meta.app = app
to.meta.store = store
})

app.use(store)

sharedData.$store = store
sharedData.$app = app
sharedData.$router = router
}
)
5 changes: 1 addition & 4 deletions client/src/pages/archives.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@
import { computed, defineComponent } from 'vue'
import { useUniversalFetch } from '../hooks'
import { useStore } from '../store'
import { useSharedStore } from '../utils'
export default defineComponent({
async beforeRouteEnter(to, _, next) {
const store = useSharedStore()
await useUniversalFetch(to, () => store.dispatch('fetchArchives'))
await useUniversalFetch(to, (store) => store.dispatch('fetchArchives'))
next()
},
Expand Down
6 changes: 2 additions & 4 deletions client/src/pages/post/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ import { computed, defineComponent } from 'vue'
import { useRoute } from 'vue-router'
import { useUniversalFetch } from '../../hooks'
import { useStore } from '../../store'
import { useSharedStore } from '../../utils'
export default defineComponent({
async beforeRouteEnter(to, _from, next) {
await useUniversalFetch(to, async () => {
const store = useSharedStore()
await useUniversalFetch(to, async (store) => {
await store.dispatch('fetchPost', to.query)
})
next()
},
async beforeRouteUpdate(to, _from, next) {
const store = useSharedStore()
const store = useStore()
await store.dispatch('fetchPost', to.query)
next()
},
Expand Down
2 changes: 0 additions & 2 deletions client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './sharedData'

export const isSSR = import.meta.env.SSR

export function sleep(ms = 1000) {
Expand Down
8 changes: 0 additions & 8 deletions client/src/utils/sharedData.ts

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/vue.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Composer } from 'vue-i18n'

declare module '@vue/runtime-core' {
// provide typings for `this.t`
interface ComponentCustomProperties {
// i18n plugin
t: Composer['t']
}
}

0 comments on commit 0e679c8

Please sign in to comment.