Encapsulating SSR Rendering (Isolation Mode) #1117
Replies: 1 comment
-
So what are the options? OutcomesWell from an (long term) outcomes perspective I would like something that:
Worker Threads (Recommended - Short Term)We already use a worker thread pool for pre-rendering and have used Workers in the past for SSR, so I think this is probably the most ideal solution for right now, unless the other two cases reveal themselves to be more compelling that it warrants even refactoring the pre-rendering pipeline. Essentially it would be reverting our earlier work but making sure to honor point 3 above. Thoughts / Questions
VM ModulesAs recommended in the Lit docs, VM modules are an experimental feature in NodeJS, and related to v8
This requires a flag for Node ( // render-template.js
import {render} from '@lit-labs/ssr';
import {myTemplate} from './my-template.js';
export const renderTemplate = (someData) => {
return render(myTemplate(someData));
}; // server.js
import {ModuleLoader} from '@lit-labs/ssr/lib/module-loader.js';
import {RenderResultReadable} from '@lit-labs/ssr/lib/render-result-readable.js';
// ...
// within a Koa middleware, for example
app.use(async (ctx) => {
const moduleLoader = new ModuleLoader();
const importResult = await moduleLoader.importModule(
'./render-template.js', // Module to load in VM context
import.meta.url // Referrer URL for module
);
const {renderTemplate} = importResult.module.namespace;
const ssrResult = await renderTemplate({some: "data"});
ctx.type = 'text/html';
ctx.body = new RenderResultReadable(ssrResult);
}); Its arguable I suppose if this is more or less ergonomic than a worker. Thoughts / Questions
|
Beta Was this translation helpful? Give feedback.
-
Overview
The role of
Worker
s and threads has had a bit of history in the Greenwood project. As part of the move to ESM, it was necessary to use this for cache busting ESM for SSR pages in development as there is currently no good way to do this in NodeJS like there was for CommonJS. The same was needed for API routes too.This became the foundation of the Renderer plugin as well.
However, per #1088, the
Worker
implementation was refactored out of SSR builds to ship purely ESM files with no Worker overhead. The Renderer plugin was also refactored to not have to know anything about the runtime rendering context, be it in a Worker thread or not.Thoughts
Aside from still being used to throttle SSR builds and for development time cache busting, should Workers still be considered for the project in any capacity where they are not explicitly? One of the initial thoughts aside from cache busting was that this could also be a good way to encapsulate rendering to avoid leaking DOM shim code into the global runtime, however for a lot of these cases serverless and edge function deployments I think would mostly mitigate this?
Currently Greenwood doesn't support multiple renderer plugins as once so probably no real chance of collisions, but I suppose i could see the case being made to still opt-in if desired?
Beta Was this translation helpful? Give feedback.
All reactions