fix(render): preserve script position in Astro.slots.render() output#15705
Open
umutkeltek wants to merge 1 commit intowithastro:mainfrom
Open
fix(render): preserve script position in Astro.slots.render() output#15705umutkeltek wants to merge 1 commit intowithastro:mainfrom
umutkeltek wants to merge 1 commit intowithastro:mainfrom
Conversation
…ithastro#15627) Scripts rendered through Astro.slots.render('default') with set:html were being hoisted to the top of the slot output because renderSlotToString() collected script RenderInstructions into a separate instructions array, and stringifyChunk() rendered all instructions before the content string. This fix uses positional placeholders (<!--astro:script:N-->) embedded in the content string at the script's original position, with the actual script instructions stored in a separate scriptInstructions map on SlotString. At stringification time, placeholders are replaced with the real script HTML, preserving both original position and deferred deduplication. Also updated server-islands.ts to resolve script placeholders when serializing slot content for island responses. Co-Authored-By: Claude Opus 4.6 <[email protected]>
🦋 Changeset detectedLatest commit: b5cde8a The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Merging this PR will improve performance by 10.85%
Performance Changes
Comparing |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #15627
Scripts rendered through
Astro.slots.render('default')withset:htmlwere being hoisted to the top of the slot output instead of rendering at their original position. This regression was introduced by PR #15147, which changedrenderScript()from returning a plain HTML string to returning aRenderInstructionobject.Root Cause
In
renderSlotToString(),RenderInstructionobjects (including scripts) were collected into a separateinstructionsarray. When theSlotStringwas stringified instringifyChunk(), all instructions were rendered before the content string — causing scripts to be hoisted to the top of the slot output regardless of their original position.Fix
Uses positional placeholders (
<!--astro:script:N-->) embedded in the content string at each script's original position. The actual script instructions are stored in ascriptInstructionsmap onSlotString. At stringification time, placeholders are replaced with the real script HTML.This preserves:
stringifyChunkwhich checksrenderedScriptsserver-islands.tsto resolve script placeholders in island responsesFiles Changed
packages/astro/src/runtime/server/render/slot.ts— AddedscriptInstructionsfield toSlotString, script instructions now use positional placeholders in content stringpackages/astro/src/runtime/server/render/common.ts— Replace script placeholders with stringified content duringSlotStringprocessingpackages/astro/src/runtime/server/render/server-islands.ts— Resolve script placeholders when serializing slot content for island responsesTest plan
<li>, not hoisted before<ol>