@tanstack/solid-router
Version:
Modern and scalable routing for Solid applications
43 lines • 1.85 kB
JSX
import * as Solid from '@solidjs/web';
import { makeSsrSerovalPlugin } from '@tanstack/router-core';
import clientAssetsManifest from './clientAssetsManifest';
export const renderRouterToString = ({ router, responseHeaders, children, manifest, }) => {
try {
const serializationAdapters = router.options?.serializationAdapters ||
router.options.ssr?.serializationAdapters;
const serovalPlugins = serializationAdapters?.map((adapter) => {
const plugin = makeSsrSerovalPlugin(adapter, { didRun: false });
return plugin;
});
let html = Solid.renderToString(children, {
nonce: router.options.ssr?.nonce,
plugins: serovalPlugins,
// Prefer the bundler-provided client-assets bridge (module-keyed, the
// shape Solid resolves lazy() assets from); the router's route-keyed
// manifest is a last resort that cannot answer lazy module lookups.
manifest: manifest ?? clientAssetsManifest ?? router.ssr?.manifest,
});
router.serverSsr.setRenderFinished();
const injectedHtml = router.serverSsr.takeBufferedHtml();
if (injectedHtml) {
html = html.replace(`</body>`, () => `${injectedHtml}</body>`);
}
return new Response(`<!DOCTYPE html>${html}`, {
status: router._serverResult?.type === 'render'
? router._serverResult.status
: 200,
headers: responseHeaders,
});
}
catch (error) {
console.error('Render to string error:', error);
return new Response('Internal Server Error', {
status: 500,
headers: responseHeaders,
});
}
finally {
router.serverSsr?.cleanup();
}
};
//# sourceMappingURL=renderRouterToString.jsx.map