Current section

Files

Jump to
live_vue priv static server.js
Raw

priv/static/server.js

import fs from "fs";
import { createSSRApp, h } from "vue";
import { renderToString } from "vue/server-renderer";
import { migrateToLiveVueApp } from "./app";
import { mapValues } from "./utils";
const mockLive = {
el: {},
liveSocket: {},
pushEvent: () => 0,
pushEventTo: () => 0,
handleEvent: () => () => { },
removeHandleEvent: () => { },
upload: () => { },
uploadTo: () => { },
vue: {
props: {},
slots: {},
app: {},
},
};
export const getRender = (componentsOrApp) => {
const { resolve, setup } = migrateToLiveVueApp(componentsOrApp);
return async (name, props, slots) => {
const component = await resolve(name);
const slotComponents = mapValues(slots, base64 => () => h("div", { innerHTML: atob(base64).trim() }));
const app = setup({
createApp: createSSRApp,
component,
props,
slots: slotComponents,
plugin: {
install: (app) => {
// we don't want to mount the app in SSR
app.mount = (...args) => undefined;
// we don't have hook instance in SSR, so we need to mock it
app.provide("_live_vue", Object.assign({}, mockLive));
},
},
el: {},
ssr: true,
});
if (!app)
throw new Error("Setup function did not return a Vue app!");
return renderToString(app);
};
};
/**
* Loads the manifest file from the given path and returns a record of the assets.
* Manifest file is a JSON file generated by Vite for the client build.
* We need to load it to know which files to preload for the given page.
* @param path - The path to the manifest file.
* @returns A record of the assets.
*/
export const loadManifest = (path) => {
const manifest = JSON.parse(fs.readFileSync(path, "utf8"));
return mapValues(manifest, value => (Array.isArray(value) ? value : [value]));
};