-
Notifications
You must be signed in to change notification settings - Fork 26
feat(ServerComponent): add method to render simplified html #1072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
02495b2
541548d
2c081e3
198df40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
caugner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,4 +44,18 @@ export class ReferenceLayout extends ServerComponent { | |
| </div> | ||
| `; | ||
| } | ||
|
|
||
| /** | ||
| * @param {import("@fred").Context<import("@rari").DocPage>} context | ||
| */ | ||
|
Comment on lines
+48
to
+50
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
| `; | ||
| } | ||
caugner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
|
@@ -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) { | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: add description of this method |
||
| renderSimple(...args) { | ||
| return this.render(...args); | ||
| } | ||
| } | ||
| 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; | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.