nuxt
Version:
95 lines (94 loc) • 3.59 kB
JavaScript
import {
createRenderer
} from "vue-bundle-renderer/runtime";
import { renderToString as _renderToString } from "vue/server-renderer";
import { propsToString } from "@unhead/vue/server";
import { useRuntimeConfig } from "nitropack/runtime";
import { appRootAttrs, appRootTag, appSpaLoaderAttrs, appSpaLoaderTag, spaLoadingTemplateOutside } from "#internal/nuxt.config.mjs";
import { buildAssetsURL } from "#internal/nuxt/paths";
const APP_ROOT_OPEN_TAG = `<${appRootTag}${propsToString(appRootAttrs)}>`;
const APP_ROOT_CLOSE_TAG = `</${appRootTag}>`;
const getServerEntry = () => import("#build/dist/server/server.mjs").then((r) => r.default || r);
const getClientManifest = () => import("#build/dist/server/client.manifest.mjs").then((r) => r.default || r).then((r) => typeof r === "function" ? r() : r);
export const getSSRRenderer = lazyCachedFunction(async () => {
const manifest = await getClientManifest();
if (!manifest) {
throw new Error("client.manifest is not available");
}
const createSSRApp = await getServerEntry();
if (!createSSRApp) {
throw new Error("Server bundle is not available");
}
const options = {
manifest,
renderToString,
buildAssetsURL
};
const renderer = createRenderer(createSSRApp, options);
async function renderToString(input, context) {
const html = await _renderToString(input, context);
if (import.meta.dev && process.env.NUXT_VITE_NODE_OPTIONS) {
renderer.rendererContext.updateManifest(await getClientManifest());
}
return APP_ROOT_OPEN_TAG + html + APP_ROOT_CLOSE_TAG;
}
return renderer;
});
const getSPARenderer = lazyCachedFunction(async () => {
const manifest = await getClientManifest();
const spaTemplate = await import("#spa-template").then((r) => r.template).catch(() => "").then((r) => {
if (spaLoadingTemplateOutside) {
const APP_SPA_LOADER_OPEN_TAG = `<${appSpaLoaderTag}${propsToString(appSpaLoaderAttrs)}>`;
const APP_SPA_LOADER_CLOSE_TAG = `</${appSpaLoaderTag}>`;
const appTemplate = APP_ROOT_OPEN_TAG + APP_ROOT_CLOSE_TAG;
const loaderTemplate = r ? APP_SPA_LOADER_OPEN_TAG + r + APP_SPA_LOADER_CLOSE_TAG : "";
return appTemplate + loaderTemplate;
} else {
return APP_ROOT_OPEN_TAG + r + APP_ROOT_CLOSE_TAG;
}
});
const options = {
manifest,
renderToString: () => spaTemplate,
buildAssetsURL
};
const renderer = createRenderer(() => () => {
}, options);
const result = await renderer.renderToString({});
const renderToString = (ssrContext) => {
const config = useRuntimeConfig(ssrContext.event);
ssrContext.modules ||= /* @__PURE__ */ new Set();
ssrContext.payload.serverRendered = false;
ssrContext.config = {
public: config.public,
app: config.app
};
return Promise.resolve(result);
};
return {
rendererContext: renderer.rendererContext,
renderToString
};
});
function lazyCachedFunction(fn) {
let res = null;
return () => {
if (res === null) {
res = fn().catch((err) => {
res = null;
throw err;
});
}
return res;
};
}
export function getRenderer(ssrContext) {
return process.env.NUXT_NO_SSR || ssrContext.noSSR ? getSPARenderer() : getSSRRenderer();
}
export const getSSRStyles = lazyCachedFunction(() => import("#build/dist/server/styles.mjs").then((r) => r.default || r));
export const getEntryIds = () => getClientManifest().then((r) => Object.values(r).filter(
(r2) => (
// @ts-expect-error internal key set by CSS inlining configuration
r2._globalCSS
)
).map((r2) => r2.src));