Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions build/server-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ if (!indexModulePath) {
try {
/** @type {import("../entry.ssr.js")} */
const indexModule = await import(pathToFileURL(indexModulePath).href);
const html = await indexModule?.render(reqPath, context, {
client: compilationStats.find((x) => x.name === "client") || {},
legacy: compilationStats.find((x) => x.name === "legacy") || {},
});
const html = process.env.FRED_SIMPLE_HTML
? `<!doctype html><meta charset="UTF-8">${await indexModule?.renderSimple(reqPath, context)}`
: await indexModule?.render(reqPath, context, {
client: compilationStats.find((x) => x.name === "client") || {},
legacy: compilationStats.find((x) => x.name === "legacy") || {},
});
parentPort?.postMessage({ html });
} catch (error) {
parentPort?.postMessage({ error });
Expand Down
10 changes: 9 additions & 1 deletion components/doc/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { ServerComponent } from "../server/index.js";
export class Doc extends ServerComponent {
/**
* @param {import("@fred").Context<import("@rari").DocPage>} context
*/ render(context) {
*/
render(context) {
context.pageTitle = context.doc.pageTitle;
return PageLayout.render(context, ReferenceLayout.render(context));
}

/**
* @param {import("@fred").Context<import("@rari").DocPage>} context
*/
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: explain what this method omits (even if it's comparably trivial from looking at render/renderSimple side-by-side

renderSimple(context) {
return ReferenceLayout.render(context);
}
}
3 changes: 3 additions & 0 deletions components/outer-layout/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ export class OuterLayout extends ServerComponent {
if (!asyncStore) {
throw new Error("asyncLocalStorage missing");
}
if ("renderSimple" in asyncStore) {
throw new Error("OuterLayout called from renderSimple function");
}
const { componentsUsed, componentsWithStylesInHead, compilationStats } =
asyncStore;

Expand Down
14 changes: 14 additions & 0 deletions components/reference-layout/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ export class ReferenceLayout extends ServerComponent {
</div>
`;
}

/**
* @param {import("@fred").Context<import("@rari").DocPage>} context
*/
Comment on lines +48 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: explain what this simplified/minimal rendering omits

renderSimple(context) {
const { doc } = context;
const sections =
doc.body?.map((section) => ContentSection.render(context, section)) || [];

return html`
<h1>${doc.title}</h1>
${BaselineIndicator.render(context)} ${sections}
`;
}
}
11 changes: 10 additions & 1 deletion components/server/async-local-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { AsyncLocalStorage } from "node:async_hooks";

/** @type {AsyncLocalStorage<import("./types.js").AsyncLocalStorageContents>} */
/**
* Store for internal context passed around components.
*
* Generally only used within the `ServerComponent` class itself,
* or very special server components (such as the `OuterLayout`).
Comment on lines +4 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you give an example?

*
* Populated in `entry.ssr.js`.
*
* @type {AsyncLocalStorage<import("./types.js").FredLocalContents>}
*/
export const asyncLocalStorage = new AsyncLocalStorage();
17 changes: 16 additions & 1 deletion components/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export class ServerComponent {
if (!asyncStore) {
throw new Error("asyncLocalStorage missing");
}

const component = new this();

if ("renderSimple" in asyncStore) {
return component.renderSimple(...args);
}

const { componentsUsed, componentsWithStylesInHead, compilationStats } =
asyncStore;
const componentUsedBefore = componentsUsed.has(this.name);
Expand All @@ -35,7 +42,7 @@ export class ServerComponent {
componentsUsed.add("legacy");
}

let res = new this().render(...args);
let res = component.render(...args);

if (!res || res === nothing) {
if (!componentUsedBefore) {
Expand Down Expand Up @@ -73,4 +80,12 @@ export class ServerComponent {
render(..._args) {
throw new Error("Must be implemented by subclass");
}

/**
* @param {...any} args
* @returns {any}
*/
Comment on lines +84 to +87
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add description of this method

renderSimple(...args) {
return this.render(...args);
}
}
14 changes: 9 additions & 5 deletions components/server/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
export interface AsyncLocalStorageContents {
componentsUsed: Set<string>;
componentsWithStylesInHead: Set<string>;
compilationStats: import("@fred").CompilationStats;
}
export type FredLocalContents =
| {
componentsUsed: Set<string>;
componentsWithStylesInHead: Set<string>;
compilationStats: import("@fred").CompilationStats;
}
| {
renderSimple: true;
};
34 changes: 33 additions & 1 deletion entry.ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { render as r } from "@lit-labs/ssr";
import { collectResult } from "@lit-labs/ssr/lib/render-result.js";

import { nothing } from "lit";

import { Advertising } from "./components/advertising/server.js";
import { BlogIndex } from "./components/blog-index/server.js";
import { BlogPost } from "./components/blog-post/server.js";
Expand Down Expand Up @@ -54,7 +56,7 @@ export async function render(path, partialContext, compilationStats) {
...(await addFluent(locale)),
...partialContext,
};
/** @type {import("./components/server/types.js").AsyncLocalStorageContents} */
/** @type {import("./components/server/types.js").FredLocalContents} */
const storageContents = {
componentsUsed: new Set(),
componentsWithStylesInHead: new Set(),
Expand Down Expand Up @@ -130,3 +132,33 @@ export async function render(path, partialContext, compilationStats) {
}),
);
}

/**
* @param {string} path
* @param {import("@fred").PartialContext} partialContext
*/
export async function renderSimple(path, partialContext) {
const locale = path.split("/")[1] || "en-US";
const context = {
path,
...(await addFluent(locale)),
...partialContext,
};
/** @type {import("./components/server/types.js").FredLocalContents} */
const storageContents = {
renderSimple: true,
};
return asyncLocalStorage.run(storageContents, () =>
runWithContext({ locale }, async () => {
const component = await (async () => {
switch (context.renderer) {
case "Doc":
return Doc.render(context);
default:
return nothing;
}
})();
return await collectResult(r(component));
}),
);
}
Loading