Open chriscalo opened 8 months ago
I want to instead try the Vite approach described at https://vitejs.dev/guide/ssr.html.
Goal #1: We should only have to author .vue
files that correspond to pages. No .js
or .html
entry points. We use unhead's <Head>
component to inject content into the HTML <head>
.
<template>
<Head>
<title>Hi</title>
<meta name="description" content="hi hi" />
</Head>
<h1>Hello, World!</h1>
</template>
Goal #2: We first render the component on the server using the Vite method.
// 1. Read index.html
let template = fs.readFileSync(
path.resolve(__dirname, 'index.html'),
'utf-8',
)
// 2. Apply Vite HTML transforms. This injects the Vite HMR client,
// and also applies HTML transforms from Vite plugins, e.g. global
// preambles from @vitejs/plugin-react
template = await vite.transformIndexHtml(url, template)
// 3a. Load the server entry. ssrLoadModule automatically transforms
// ESM source code to be usable in Node.js! There is no bundling
// required, and provides efficient invalidation similar to HMR.
const { render } = await vite.ssrLoadModule('/src/entry-server.js')
What's great is it seems
findMatchingPageComponent(req.originalUrl)
is the only function we need to write.