-
Notifications
You must be signed in to change notification settings - Fork 55
feat(render): section-controlled JSON output via renderJson export #1209
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
Open
marcoferreiradev
wants to merge
4
commits into
deco-cx:main
Choose a base branch
from
oficina-dev:pr/render-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6c6b3a8
feat(render): add ?format=json branch with serializeResolvedSection
marcoferreiradev bbadf98
feat(serialize): sections control their JSON rendering via renderJso…
marcoferreiradev 61d9563
feat(hooks): export computeRenderCb — shared cache-bust recipe
marcoferreiradev b409595
fix(render): address review feedback on renderJson serialization
marcoferreiradev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import { Context } from "../../deco.ts"; | ||
| import { FieldResolver } from "../../engine/core/resolver.ts"; | ||
|
|
||
| const LAZY_SECTION_PATH = "/Rendering/Lazy.tsx"; | ||
|
|
||
| export interface ResolvedSection { | ||
| Component?: unknown; | ||
| props?: Record<string, unknown>; | ||
| metadata?: { | ||
| component?: string; | ||
| resolveChain?: FieldResolver[]; | ||
| }; | ||
| } | ||
|
|
||
| export type SerializedSection = | ||
| | { component: string; props: Record<string, unknown> } | ||
| | { component: string; lazyUrl: string }; | ||
|
|
||
| /** | ||
| * Module-level control of how a section renders to JSON. | ||
| * | ||
| * - `false` — the section has no JSON rendering; it is dropped from the | ||
| * serialized output entirely. | ||
| * - function — a projection applied to the resolved props before | ||
| * serialization. Annotate the parameter with `SectionProps<typeof loader>` | ||
| * (or the section's own Props) to keep it compile-checked. | ||
| * | ||
| * ```ts | ||
| * export const renderJson = false; | ||
| * | ||
| * export const renderJson = ( | ||
| * { internalOnly, ...rest }: SectionProps<typeof loader>, | ||
| * ) => rest; | ||
| * ``` | ||
| */ | ||
| export type RenderJson = | ||
| // deno-lint-ignore no-explicit-any | ||
| | ((props: any) => Record<string, unknown>) | ||
| | false; | ||
|
|
||
| export interface SectionJsonModule { | ||
| renderJson?: RenderJson; | ||
| } | ||
|
|
||
| export interface LazyUrlContext { | ||
| href: string; | ||
| pathTemplate: string; | ||
| renderSalt?: string; | ||
| cb?: string; | ||
| } | ||
|
|
||
| export interface SerializeContext extends LazyUrlContext { | ||
| /** | ||
| * Resolves a section module by its component name (resolveType) so the | ||
| * serializer can honor its `renderJson` export. When omitted, every | ||
| * section serializes with its full resolved props. | ||
| */ | ||
| getSectionModule?: (component: string) => SectionJsonModule | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a `getSectionModule` lookup over the active context's merged | ||
| * manifest (site + apps), keyed by resolveType. | ||
| */ | ||
| export const sectionModuleLookup = async (): Promise< | ||
| (component: string) => SectionJsonModule | undefined | ||
| > => { | ||
| const runtime = await Context.active().runtime; | ||
| const sections = (runtime?.manifest as unknown as { | ||
| sections?: Record<string, SectionJsonModule>; | ||
| })?.sections ?? {}; | ||
| return (component) => sections[component]; | ||
| }; | ||
|
|
||
| export function buildLazyUrl( | ||
| resolveChain: FieldResolver[], | ||
| ctx: LazyUrlContext, | ||
| ): string { | ||
| const params = new URLSearchParams([ | ||
| ["format", "json"], | ||
| ["props", JSON.stringify({ loading: "eager" })], | ||
| ["href", ctx.href], | ||
| ["pathTemplate", ctx.pathTemplate], | ||
| [ | ||
| "resolveChain", | ||
| JSON.stringify(FieldResolver.minify(resolveChain.slice(0, -1))), | ||
| ], | ||
| ]); | ||
| if (ctx.renderSalt) params.set("renderSalt", ctx.renderSalt); | ||
| if (ctx.cb) params.set("__cb", ctx.cb); | ||
| return `/deco/render?${params}`; | ||
| } | ||
|
|
||
| function isSectionShape(value: unknown): value is ResolvedSection { | ||
| if (!value || typeof value !== "object") return false; | ||
| const meta = (value as ResolvedSection).metadata; | ||
| return typeof meta?.component === "string"; | ||
| } | ||
|
|
||
| function isLazyComponent(component: string | undefined): boolean { | ||
| return !!component?.endsWith(LAZY_SECTION_PATH); | ||
| } | ||
|
|
||
| function getInnerSection(node: ResolvedSection): ResolvedSection | undefined { | ||
| const inner = (node.props as { section?: unknown } | undefined)?.section; | ||
| return isSectionShape(inner) ? inner : undefined; | ||
| } | ||
|
|
||
| function getLoading(node: ResolvedSection): string | undefined { | ||
| return (node.props as { loading?: string } | undefined)?.loading; | ||
| } | ||
|
|
||
| function renderJsonOf( | ||
| ctx: SerializeContext, | ||
| component: string, | ||
| ): RenderJson | undefined { | ||
| return ctx.getSectionModule?.(component)?.renderJson; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes a resolved section honoring its `renderJson` export. | ||
| * Returns `null` when the section opted out (`renderJson === false`) — | ||
| * callers must drop it from the output. | ||
| * | ||
| * A `renderJson` projection cannot run for lazy placeholders (their props | ||
| * are not resolved yet); it applies on the lazy fetch itself, when | ||
| * `/deco/render?format=json` serializes the resolved section. | ||
| */ | ||
| export function serializeResolvedSection( | ||
| node: ResolvedSection, | ||
| ctx: SerializeContext, | ||
| ): SerializedSection | null { | ||
| let current = node; | ||
| while ( | ||
| isLazyComponent(current.metadata?.component) && | ||
| getLoading(current) === "eager" | ||
| ) { | ||
| const inner = getInnerSection(current); | ||
| if (!inner) break; | ||
| current = inner; | ||
| } | ||
|
|
||
| if ( | ||
| isLazyComponent(current.metadata?.component) && | ||
| getLoading(current) === "lazy" | ||
| ) { | ||
| const inner = getInnerSection(current); | ||
| const component = inner?.metadata?.component ?? | ||
| current.metadata!.component!; | ||
| if (renderJsonOf(ctx, component) === false) return null; | ||
| return { | ||
| component, | ||
| // The wrapper's resolveChain is what reconstructs this slot on the lazy | ||
| // fetch — it re-resolves the inner section and applies the inner's | ||
| // renderJson there. The inner is not independently resolvable, so its own | ||
| // chain would not rebuild this position; the wrapper's chain is correct. | ||
| lazyUrl: buildLazyUrl(current.metadata!.resolveChain ?? [], ctx), | ||
| }; | ||
| } | ||
|
|
||
| const component = current.metadata!.component!; | ||
| const renderJson = renderJsonOf(ctx, component); | ||
| if (renderJson === false) return null; | ||
|
|
||
| const props = typeof renderJson === "function" | ||
| ? renderJson(current.props ?? {}) | ||
| : current.props ?? {}; | ||
|
|
||
| return { | ||
| component, | ||
| props: walkValue(props, ctx) as Record<string, unknown>, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| }; | ||
| } | ||
|
|
||
| function walkValue(value: unknown, ctx: SerializeContext): unknown { | ||
| if (Array.isArray(value)) { | ||
| // Dropped sections (renderJson === false) are removed from arrays so | ||
| // consumers iterate a dense list; non-section nulls pass through. | ||
| const out: unknown[] = []; | ||
| for (const v of value) { | ||
| if (isSectionShape(v)) { | ||
| const serialized = serializeResolvedSection(v, ctx); | ||
| if (serialized !== null) out.push(serialized); | ||
| } else { | ||
| out.push(walkValue(v, ctx)); | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
| if (value && typeof value === "object") { | ||
| if (isSectionShape(value)) { | ||
| return serializeResolvedSection(value, ctx); | ||
| } | ||
| const out: Record<string, unknown> = {}; | ||
| for (const [k, v] of Object.entries(value)) { | ||
| if (k === "Component" && typeof v === "function") continue; | ||
| out[k] = walkValue(v, ctx); | ||
| } | ||
| return out; | ||
| } | ||
| return value; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Potential unintended breaking change:
Murmurhash3re-export removed from the public barrel (mod.ts) without deprecation path or PR description mention.Prompt for AI agents