From an architectural perspective, VPS is versatile:
renderPage()
.In practice, this means that we can use VPS with whatever tool we want. In fact, VPS is being used with all kinds of tools within all kinds of environments.
// /renderer/_default.page.server.js
// Environment: Node.js
import { escapeInject, dangerouslySkipEscape } from 'vite-plugin-ssr/server'
// This can be any UI framework (React, Vue, Svelte, ...)
import renderToHtml from 'some-ui-framework'
export { render }
async function render(pageContext) {
// `Page` is the `export { Page }` of our `*.page.js` files;
// `vite-plugin-ssr` doesn't do anything with `Page` and just makes it available as
// `pageContex.Page`; we can export any `Page` value we want and do whatever we want with it.
const { Page } = pageContext
// We have control over how we use our UI framework to render our pages to HTML
const pageHtml = await renderToHtml(Page)
// We have control over the entire HTML
return escapeInject`<html>
<body>
<head>
<!-- Some libraries recommend loading code from a CDN -->
<script src="https://cdn.example.com/some-library/3.3.7/lib.min.js"></script>
</head>
<div id="root">
${dangerouslySkipEscape(pageHtml)}
</div>
</body>
</html>`
}
// /renderer/_default.page.client.js
// Environment: Browser
export { render }
import { hydrateDom } from 'some-ui-framework'
async render(pageContex) {
const { Page } = pageContext
// We have control over how our pages are hydrated
await hydrateDom(Page)
}
Since we control how our pages are render to HTML and hydrated, we can use:
petite-vue
, Svelte, Solid, Preact, ...)And, more importantly, integrating a view tool is simply a matter of following its official installation guide.
// /renderer/_default.page.client.js
// Environment: Browser
export { render }
import { hydrateDom } from 'some-ui-framework'
// This is a good place to initialize error tracking such as Sentry or Bugsnag.
Sentry.init()
// And also for initializing a Service Worker.
navigator.serviceWorker.register(/* ... */)
async render(pageContex) {
// Here we can integrate performance measurement tools, e.g. to measure hydration performance
const { Page } = pageContex
await hydrateDom(Page)
init()
}
function init() {
// After hydration we usually initialize vanilla JS component libraries, for example tooltips
tooltip.init(document.querySelectorAll('.tooltip')
// Or some vanilla JS modal library
$('.my-modals').modal()
}
We can use:
Integrating a browser tool is only a matter of following its official installation guide.
From the perspective of our server architecture, vite-plugin-ssr
is just a middleware:
// `app` can be any server framework such as Express.js/Fastify/Koa/Hapi/...
app.get('*', async (req, res) => {
// renderPage() is just a function that, for a given URL, returns the result of
// of our render() hook (usually an HTML string). The renderPage() function
// can be used within any server environment, including serverless and edge
// environments such as AWS Lambda, Cloudflare Workers, Vercel, ...
const pageContext = await renderPage({ urlOriginal: req.url })
res.send(pageContext.httpResponse.body)
})
By pre-rendering our pages we can remove the need for a production server and, instead, deploy to a static host.
We can use:
Official examples: GitHub > brillout/vite-plugin-ssr
> examples/
.
Beyond the offical examples, many tools have community examples of being used with vite-plugin-ssr
– search this website (CTRL+K
) for the tool you want an example of.
If an example is missing/outdated, PR welcome to add yours.