ReviewOS

also looking at this

stacks/stx

fix(layouts): keep a page's @push content when its layout extends another layout

#2105
Open mkantautas wants to merge fix/nested-layout-push into main
2 files +106 -3
packages/stx/src/process.tsmodified+18-3
Changes to packages/stx/src/process.ts
@@ -962,6 +962,13 @@ async function processDirectivesInternal(
962962
963963 let output = template
964964
965 // What the page and the layouts so far pushed, when this call renders the
966 // layout that a layout extends (see the nested-layout branch). Taken off the
967 // context before anything else runs, so view composers, middleware,
968 // components and includes never see it.
969 const inheritedStacks = context.__stx_layout_stacks as { stacks: Record<string, string[]>, htmlComments: string[] } | undefined
970 delete context.__stx_layout_stacks
971
965972 await runViewComposersForTemplate(filePath, context)
966973 prepareDirectiveContext(context, opts)
967974
@@ -1052,8 +1059,16 @@ async function processDirectivesInternal(
10521059 // and: <link href="./file.css" stx-inline />
10531060 output = await processInlineAssets(output, filePath, dependencies)
10541061
1055 // Store stacks for @push/@stack directives
1062 // Store stacks for @push/@stack directives, starting with the inherited
1063 // ones. Their HTML comments are masked against the caller's table, so each
1064 // one moves into this call's table under a new index.
10561065 const stacks: Record<string, string[]> = {}
1066 for (const [name, entries] of Object.entries(inheritedStacks?.stacks ?? {})) {
1067 stacks[name] = entries.map(entry => entry.replace(/\x00STX_HTML_COMMENT_(\d+)\x00/g, (_, i) => {
1068 htmlComments.push(inheritedStacks!.htmlComments[Number(i)] ?? '')
1069 return `\x00STX_HTML_COMMENT_${htmlComments.length - 1}\x00`
1070 }))
1071 }
10571072
10581073 // Process @push and @prepend directives first
10591074 output = processStackPushDirectives(output, stacks)
@@ -1461,8 +1476,8 @@ async function processDirectivesInternal(
14611476 }
14621477 }
14631478
1464 // Create a new context with merged sections for the nested layout
1465 const nestedContext = { ...context, __sections: sections }
1479 // Create a new context with merged sections and the pushes so far for the nested layout
1480 const nestedContext = { ...context, __sections: sections, __stx_layout_stacks: { stacks, htmlComments } }
14661481
14671482 // Process the nested layout by recursively calling processDirectives
14681483 // with the modified template that includes the @layout directive