diff --git a/index.html b/index.html index 894d6105..9b0102a1 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,7 @@ diff --git a/server-dev.ts b/server-dev.ts index 84ea7403..abffd57f 100644 --- a/server-dev.ts +++ b/server-dev.ts @@ -13,12 +13,29 @@ const APP_PORT = 1007 const __dirname = path.dirname(fileURLToPath(import.meta.url)) +// Inject teleports in template +const injectTeleports = (html: string, teleports: string) => { + if (teleports) { + for (const [target, content] of Object.entries(teleports)) { + if (['head', 'body', 'html'].includes(target)) { + const replacement = `` + html = html.replace(replacement, content + replacement) + } else { + const replacement = ` id="${target.replace('#', '')}">` + html = html.replace(replacement, replacement + content) + } + } + } + + return html +} + ;(async () => { const app = new Koa() const vite = await createViteServer({ server: { middlewareMode: true }, - appType: 'custom', + appType: 'spa', }) app.use(koaConnect(vite.middlewares)) @@ -34,11 +51,15 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url)) const { render } = await vite.ssrLoadModule('/src/entry-server.ts') - const [renderedHtml, state] = await render(ctx, {}) + const [renderedHtml, renderedPinia, renderedLinks, renderedTeleports] = + await render(ctx, {}) - const html = template + const injectedTeleportsHtml = injectTeleports(template, renderedTeleports) + + const html = injectedTeleportsHtml + .replace('', renderedLinks) .replace('', renderedHtml) - .replace('', state) + .replace('__pinia', renderedPinia) ctx.type = 'text/html' ctx.body = html diff --git a/server-prod.ts b/server-prod.ts index 2fc1c01c..cfd87936 100644 --- a/server-prod.ts +++ b/server-prod.ts @@ -33,8 +33,8 @@ import manifest from './dist/client/ssr-manifest.json' assert { type: 'json' } const html = template .replace('', preloadLinks) - .replace('', state) .replace('', renderedHtml) + .replace('__pinia', state) ctx.type = 'text/html' ctx.body = html diff --git a/src/entry-client.ts b/src/entry-client.ts index d73de35e..10c5545d 100644 --- a/src/entry-client.ts +++ b/src/entry-client.ts @@ -1,15 +1,18 @@ import { createApp } from './main' import { createKUNGalgameRouter } from './router' import { setupPinia } from './store' +import i18n from '@/language/i18n' import '@/styles/index.scss' +const router = createKUNGalgameRouter('client') +const pinia = setupPinia() + const { app } = createApp() -const router = createKUNGalgameRouter() -const pinia = setupPinia() +app.use(router).use(pinia).use(i18n) -if (window.__INITIAL_STATE__) { - pinia.state.value = JSON.parse(window.__INITIAL_STATE__) +if (window.__pinia) { + pinia.state.value = JSON.parse(window.__pinia) } router.isReady().then(() => { diff --git a/src/entry-server.ts b/src/entry-server.ts index 057a8990..075fbdfb 100644 --- a/src/entry-server.ts +++ b/src/entry-server.ts @@ -13,8 +13,6 @@ import '@/styles/index.scss' const renderPreloadLink = (file: string) => { if (file.endsWith('.js')) { return `` - } else if (file.endsWith('.js')) { - return `` } else if (file.endsWith('.css')) { return `` } else if (file.endsWith('.png')) { @@ -49,11 +47,11 @@ const renderPreloadLinks = ( export const render = async ( ctx: ParameterizedContext, manifest: Record -): Promise<[string, string, string]> => { +): Promise<[string, string, string, string]> => { const { app } = createApp() // router - const router = createKUNGalgameRouter() + const router = createKUNGalgameRouter('server') app.use(router) await router.push(ctx.path) await router.isReady() @@ -61,7 +59,8 @@ export const render = async ( // pinia const pinia = setupPinia() app.use(pinia) - const state = JSON.stringify(pinia.state.value) + + const renderedPinia = JSON.stringify(pinia.state.value) // i18n app.use(i18n) @@ -70,7 +69,9 @@ export const render = async ( const renderedHtml = await renderToString(app, renderCtx) - const preloadLinks = renderPreloadLinks(renderCtx.modules, manifest) + const renderedLinks = renderPreloadLinks(renderCtx.modules, manifest) - return [renderedHtml, state, preloadLinks] + const renderedTeleports = ctx.teleports + + return [renderedHtml, renderedPinia, renderedLinks, renderedTeleports] } diff --git a/src/router/index.ts b/src/router/index.ts index 8330617d..532ca5a4 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -3,11 +3,12 @@ import { createWebHistory, createRouter, createMemoryHistory } from 'vue-router' import { constantRoutes } from './router' import { asyncRoutes } from './router' -export const createKUNGalgameRouter = (): Router => +export const createKUNGalgameRouter = (type: 'client' | 'server'): Router => createRouter({ - history: import.meta.env.SSR - ? createMemoryHistory(import.meta.env.BASE_URL) - : createWebHistory(import.meta.env.BASE_URL), + history: + type === 'server' + ? createMemoryHistory(import.meta.env.BASE_URL) + : createWebHistory(import.meta.env.BASE_URL), routes: [...constantRoutes, ...asyncRoutes] as RouteRecordRaw[], diff --git a/src/types/shims-global.d.ts b/src/types/shims-global.d.ts index e1210613..4d3d7c3c 100644 --- a/src/types/shims-global.d.ts +++ b/src/types/shims-global.d.ts @@ -1,3 +1,3 @@ interface Window { - __INITIAL_STATE__: string; + __pinia: string }