Skip to content

Commit

Permalink
feat: remove vite-ssr plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Gem Xli committed Mar 3, 2021
1 parent af857aa commit 507252e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 18 deletions.
5 changes: 1 addition & 4 deletions client/components/VTag.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<template>
<span
class="tag text-gray-400"
@click.stop="clickTag"
>
<span class="tag text-gray-400" @click.stop="clickTag">
<v-link>
<v-icon name="tags" />
<span>{{ tag.name }}</span>
Expand Down
5 changes: 4 additions & 1 deletion client/entry-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createApp } from './main'

createApp().then(({ app, router }) => {
// @ts-ignore
const initialState = window.__INITIAL_STATE__

createApp(initialState).then(({ app, router }) => {
router.isReady().then(() => {
app.mount('#app')
})
Expand Down
13 changes: 11 additions & 2 deletions client/entry-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface RenderedContext {
}

export async function render(url: string, manifest: SSRManifest) {
const { app, router } = await createApp()
const initialState = {}
const { app, router } = await createApp(initialState)

// set the router to the desired URL before rendering
router.push(url)
Expand All @@ -28,7 +29,15 @@ export async function render(url: string, manifest: SSRManifest) {
// which we can then use to determine what files need to be preloaded for this
// request.
const preloadLinks = renderPreloadLinks(ctx.modules, manifest)
return [html, preloadLinks]

// merge router state
Object.assign(initialState, router.currentRoute.value.meta.state)

return {
html,
preloadLinks,
initialState
}
}

function renderPreloadLinks(modules: string[], manifest: SSRManifest) {
Expand Down
8 changes: 4 additions & 4 deletions client/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { i18n } from 'vite-i18n-plugin'
import { NProgressPlugin, i18nPlugin } from './plugins'
import { createSSRApp } from 'vue'

export async function createApp() {
export async function createApp(initialState: Record<string, any>) {
const router = createRouter({
history: isSSR ? createMemoryHistory() : createWebHistory(),
routes
Expand All @@ -28,10 +28,10 @@ export async function createApp() {

if (isSSR) {
await store.dispatch('serverInit')
// initialState.storeState = store.state
initialState.storeState = store.state
} else {
// store.replaceState(initialState.storeState)
// r.currentRoute.value.meta.state = initialState.state
store.replaceState(initialState.storeState)
router.currentRoute.value.meta.state = initialState.state
NProgressPlugin(router)
}

Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
crossorigin="anonymous"
/>
<!--preload-links-->
<!--initial-state-->
<title>Vite App</title>
</head>
<body>
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
"author": "Gem Xli <x.li.gem@gmail.com>",
"license": "MIT",
"scripts": {
"dev": "run-p dev:*",
"dev:client": "vite-ssr dev",
"dev:server": "ts-node server/index.ts",
"dev": "ts-node server/index.ts",
"build": "run-p build:*",
"build:client": "vite-ssr build",
"build:server": "rm -rf dist-server && tsc -p server",
Expand Down
14 changes: 10 additions & 4 deletions server/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ export function serveViteDev(vite: ViteDevServer): Middleware {
const render = (await vite.ssrLoadModule('/client/entry-server.ts'))
.render

const [appHtml, preloadLinks] = await render(url, manifest)
const { html, preloadLinks, initialState } = await render(url, manifest)

const html = template
const appHtml = template
.replace('<!--preload-links-->', preloadLinks)
.replace('<!--app-html-->', appHtml)
.replace('<!--app-html-->', html)
.replace(
'<!--initial-state-->',
`<script>window.__INITIAL_STATE__=${JSON.stringify(
initialState
)}</script>`
)

ctx.status = 200
ctx.set({ 'Content-Type': 'text/html' })
ctx.body = html
ctx.body = appHtml
} catch (e) {
vite && vite.ssrFixStacktrace(e)
console.log(e.stack)
Expand Down

0 comments on commit 507252e

Please sign in to comment.