feat: render text

This commit is contained in:
KUN1007 2023-11-04 22:06:19 +08:00
parent d74d5845d4
commit 3673c0dc6f
6 changed files with 31 additions and 54 deletions

View file

@ -17,15 +17,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
const app = new Koa() const app = new Koa()
const vite = await createViteServer({ const vite = await createViteServer({
server: { server: { middlewareMode: true },
middlewareMode: true, appType: 'custom',
watch: {
// During tests we edit the files too fast and sometimes chokidar
// misses change events, so enforce polling for consistency
usePolling: true,
interval: 100,
},
},
}) })
app.use(koaConnect(vite.middlewares)) app.use(koaConnect(vite.middlewares))
@ -50,7 +43,7 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))
ctx.type = 'text/html' ctx.type = 'text/html'
ctx.body = html ctx.body = html
} catch (e) { } catch (e) {
vite.ssrFixStacktrace(e as Error) vite && vite.ssrFixStacktrace(e as Error)
ctx.throw(500, e as Error) ctx.throw(500, e as Error)
} }
}) })

View file

@ -1,17 +1,17 @@
import { createApp } from './main' import { createApp } from './main'
import i18n from '@/language/i18n' import { createKUNGalgameRouter } from './router'
import { setupPinia } from './store'
import '@/styles/index.scss' import '@/styles/index.scss'
const { app, router, pinia } = createApp() const { app } = createApp()
app.use(router) const router = createKUNGalgameRouter()
app.use(i18n) const pinia = setupPinia()
if (window.__INITIAL_STATE__) { if (window.__INITIAL_STATE__) {
pinia.state.value = JSON.parse(window.__INITIAL_STATE__) pinia.state.value = JSON.parse(window.__INITIAL_STATE__)
} }
router.isReady().then(() => { router.isReady().then(() => {
app.mount('#app') app.mount('#app', true)
console.log('hydrated')
}) })

View file

@ -1,6 +1,9 @@
import { basename } from 'path'
import { createApp } from './main' import { createApp } from './main'
import { createKUNGalgameRouter } from './router'
import { setupPinia } from './store'
import i18n from '@/language/i18n'
import { renderToString } from '@vue/server-renderer' import { renderToString } from '@vue/server-renderer'
import type { ParameterizedContext } from 'koa' import type { ParameterizedContext } from 'koa'
@ -27,20 +30,12 @@ const renderPreloadLinks = (
let links = '' let links = ''
const seen = new Set() const seen = new Set()
if (modules === undefined) throw new Error() if (modules === undefined) throw new Error()
modules.forEach((id) => { modules.forEach((id) => {
const files = manifest[id] const files = manifest[id]
if (files) { if (files) {
files.forEach((file) => { files.forEach((file) => {
if (!seen.has(file)) { if (!seen.has(file)) {
seen.add(file) seen.add(file)
const filename = basename(file)
if (manifest[filename]) {
for (const depFile of manifest[filename]) {
links += renderPreloadLink(depFile)
seen.add(depFile)
}
}
links += renderPreloadLink(file) links += renderPreloadLink(file)
} }
}) })
@ -52,18 +47,28 @@ const renderPreloadLinks = (
export const render = async ( export const render = async (
ctx: ParameterizedContext, ctx: ParameterizedContext,
manifest: Record<string, string[]> manifest: Record<string, string[]>
): Promise<[string, string]> => { ): Promise<[string, string, string]> => {
const { app, router } = createApp() const { app } = createApp()
// router
const router = createKUNGalgameRouter()
app.use(router) app.use(router)
await router.push(ctx.path) await router.push(ctx.path)
await router.isReady() await router.isReady()
// pinia
const pinia = setupPinia()
app.use(pinia)
const state = JSON.stringify(pinia.state.value)
// i18n
app.use(i18n)
const renderCtx: { modules?: string[] } = {} const renderCtx: { modules?: string[] } = {}
const renderedHtml = await renderToString(app, renderCtx) const renderedHtml = await renderToString(app, renderCtx)
const preloadLinks = renderPreloadLinks(renderCtx.modules, manifest) const preloadLinks = renderPreloadLinks(renderCtx.modules, manifest)
return [renderedHtml, preloadLinks] return [renderedHtml, state, preloadLinks]
} }

View file

@ -1,4 +1,3 @@
import { useRouter } from 'vue-router'
// Global message component (top) // Global message component (top)
import Message from '@/components/alert/Message' import Message from '@/components/alert/Message'
import { generateTokenByRefreshTokenApi } from '@/api' import { generateTokenByRefreshTokenApi } from '@/api'
@ -14,7 +13,7 @@ interface ErrorResponseData {
message: string message: string
} }
const router = useRouter() const router = createKUNGalgameRouter()
/** /**
* Acts as an interceptor, first recognizing common errors based on predictable status codes. * Acts as an interceptor, first recognizing common errors based on predictable status codes.

View file

@ -1,25 +1,7 @@
import App from './App.vue'
import { createSSRApp } from 'vue' import { createSSRApp } from 'vue'
import App from './App.vue'
import { createKUNGalgameRouter } from './router' export const createApp = () => {
import { setupRouterGuard } from '@/router/guard'
import i18n from '@/language/i18n'
import { setupPinia } from '@/store/index'
import '@/styles/index.scss'
export function createApp() {
const app = createSSRApp(App) const app = createSSRApp(App)
const pinia = setupPinia(app) return { app }
app.use(pinia)
const router = createKUNGalgameRouter()
setupRouterGuard(router)
app.use(router)
app.use(i18n)
return { app, router, pinia }
} }

View file

@ -4,7 +4,6 @@
*/ */
import { createPinia } from 'pinia' import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import type { App } from 'vue'
// Import store for the income and expense public disclosure page // Import store for the income and expense public disclosure page
import { useKUNGalgameBalanceStore } from './modules/balance' import { useKUNGalgameBalanceStore } from './modules/balance'
@ -36,12 +35,11 @@ import { useKUNGalgameTopicStore } from './modules/topic'
const pinia = createPinia() const pinia = createPinia()
// Function to set up Pinia, to be called in main.ts // Function to set up Pinia, to be called in main.ts
export const setupPinia = (app: App<Element>) => { export const setupPinia = () => {
if (!import.meta.env.SSR) { if (!import.meta.env.SSR) {
pinia.use(piniaPluginPersistedstate) pinia.use(piniaPluginPersistedstate)
} }
app.use(pinia)
return pinia return pinia
} }