-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(render): avoid script dedup state consumption in inert template c… #16527
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
Merged
matthewp
merged 6 commits into
withastro:main
from
enjoyandlove:fix/hydration-script-dedup-inert-context
Apr 30, 2026
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
61097dc
fix(render): avoid script dedup state consumption in inert template c…
enjoyandlove 504663b
fix(render): normalize inert script dedup test outputs to strings
enjoyandlove 3c6cf61
fix: add missing hydration metadata fields in inert-script-dedup tests
enjoyandlove a3d2b3d
test(app): add component-level inert template script dedup coverage
enjoyandlove a2fe09c
test(app): stabilize inert script dedup regression coverage
enjoyandlove 7acb991
fix(astro): move inert template dedup logic into hydration/directive …
enjoyandlove 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'astro': patch | ||
| --- | ||
|
|
||
| Prevents script deduplication state from being consumed while rendering inert `<template>` contexts. |
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
120 changes: 120 additions & 0 deletions
120
packages/astro/test/units/app/inert-script-dedup.test.ts
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,120 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { App } from '../../../dist/core/app/app.js'; | ||
| import { createRenderInstruction } from '../../../dist/runtime/server/render/instruction.js'; | ||
| import { createComponent, render, templateEnter, templateExit } from '../../../dist/runtime/server/index.js'; | ||
| import { createManifest, createRouteInfo } from './test-helpers.ts'; | ||
|
|
||
| const hydrationRouteData = { | ||
| route: '/inert-hydration', | ||
| component: 'src/pages/inert-hydration.astro', | ||
| params: [], | ||
| pathname: '/inert-hydration', | ||
| distURL: [], | ||
| pattern: /^\/inert-hydration\/?$/, | ||
| segments: [[{ content: 'inert-hydration', dynamic: false, spread: false }]], | ||
| type: 'page' as const, | ||
| prerender: false, | ||
| fallbackRoutes: [], | ||
| isIndex: false, | ||
| origin: 'project' as const, | ||
| }; | ||
|
|
||
| const serverIslandRouteData = { | ||
| route: '/inert-server-island-runtime', | ||
| component: 'src/pages/inert-server-island-runtime.astro', | ||
| params: [], | ||
| pathname: '/inert-server-island-runtime', | ||
| distURL: [], | ||
| pattern: /^\/inert-server-island-runtime\/?$/, | ||
| segments: [[{ content: 'inert-server-island-runtime', dynamic: false, spread: false }]], | ||
| type: 'page' as const, | ||
| prerender: false, | ||
| fallbackRoutes: [], | ||
| isIndex: false, | ||
| origin: 'project' as const, | ||
| }; | ||
|
|
||
| const hydrationInstruction = createRenderInstruction({ | ||
| type: 'directive', | ||
| hydration: { | ||
| directive: 'load', | ||
| value: '', | ||
| componentUrl: '', | ||
| componentExport: { value: '' }, | ||
| }, | ||
| }); | ||
|
|
||
| const serverIslandInstruction = createRenderInstruction({ type: 'server-island-runtime' }); | ||
|
|
||
| const hydrationPage = createComponent((result: any) => { | ||
| return render` | ||
| <template id="inert-hydration-template"> | ||
| ${templateEnter(result)} | ||
| ${hydrationInstruction} | ||
| ${templateExit(result)} | ||
| </template> | ||
| <div id="hydration-runtime">${hydrationInstruction}</div> | ||
| `; | ||
| }); | ||
|
|
||
| const serverIslandPage = createComponent((result: any) => { | ||
| return render` | ||
| <template id="inert-server-island-template"> | ||
| ${templateEnter(result)} | ||
| ${serverIslandInstruction} | ||
| ${templateExit(result)} | ||
| </template> | ||
| <div id="server-island-runtime">${serverIslandInstruction}</div> | ||
| `; | ||
| }); | ||
|
|
||
| const pageMap = new Map([ | ||
| [ | ||
| hydrationRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: hydrationPage, | ||
| }), | ||
| }), | ||
| ], | ||
| [ | ||
| serverIslandRouteData.component, | ||
| async () => ({ | ||
| page: async () => ({ | ||
| default: serverIslandPage, | ||
| }), | ||
| }), | ||
| ], | ||
| ]); | ||
|
|
||
| const app = new App( | ||
| createManifest({ | ||
| routes: [ | ||
| createRouteInfo(hydrationRouteData), | ||
| createRouteInfo(serverIslandRouteData), | ||
| ], | ||
| clientDirectives: new Map([['load', 'console.log("directive")']]), | ||
| pageMap: pageMap as any, | ||
| }) as any, | ||
| ); | ||
|
|
||
| describe('Inert template script deduplication', () => { | ||
| it('keeps hydration prescripts available after template content', async () => { | ||
| const response = await app.render(new Request('http://example.com/inert-hydration')); | ||
| const html = await response.text(); | ||
|
|
||
| assert.equal(countOccurrences(html, 'console.log("directive")'), 2); | ||
| }); | ||
|
|
||
| it('does not consume server-island runtime dedup inside template content', async () => { | ||
| const response = await app.render(new Request('http://example.com/inert-server-island-runtime')); | ||
| const html = await response.text(); | ||
|
|
||
| assert.equal(countOccurrences(html, 'replaceServerIsland('), 2); | ||
| }); | ||
| }); | ||
|
|
||
| function countOccurrences(content: string, needle: string) { | ||
| return content.split(needle).length - 1; | ||
| } |
106 changes: 106 additions & 0 deletions
106
packages/astro/test/units/render/inert-script-dedup.test.ts
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,106 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { chunkToString } from '../../../dist/runtime/server/render/common.js'; | ||
| import { createRenderInstruction } from '../../../dist/runtime/server/render/instruction.js'; | ||
|
|
||
| function createStubResult() { | ||
| return { | ||
| clientDirectives: new Map([['load', 'console.log("directive")']]), | ||
| _metadata: { | ||
| hasHydrationScript: false, | ||
| rendererSpecificHydrationScripts: new Set<string>(), | ||
| hasRenderedHead: false, | ||
| renderedScripts: new Set<string>(), | ||
| hasDirectives: new Set<string>(), | ||
| hasRenderedServerIslandRuntime: false, | ||
| headInTree: false, | ||
| extraHead: [], | ||
| extraStyleHashes: [], | ||
| extraScriptHashes: [], | ||
| propagators: new Set(), | ||
| templateDepth: 0, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| describe('inert context dedup behavior', () => { | ||
| it('does not consume directive or hydration dedup inside templates', () => { | ||
| const result = createStubResult(); | ||
| result._metadata.templateDepth = 1; | ||
|
|
||
| const instruction = createRenderInstruction({ | ||
| type: 'directive', | ||
| hydration: { | ||
| directive: 'load', | ||
| value: '', | ||
| componentUrl: '', | ||
| componentExport: { value: '' }, | ||
| }, | ||
| }); | ||
|
|
||
| const inertOutput = chunkToString(result as any, instruction).toString(); | ||
| assert.match(inertOutput, /<script>/); | ||
| assert.equal(result._metadata.hasHydrationScript, false); | ||
| assert.equal(result._metadata.hasDirectives.has('load'), false); | ||
|
|
||
| result._metadata.templateDepth = 0; | ||
| const executableOutput = chunkToString(result as any, instruction).toString(); | ||
| assert.match(executableOutput, /<script>/); | ||
| assert.equal(result._metadata.hasHydrationScript, true); | ||
| assert.equal(result._metadata.hasDirectives.has('load'), true); | ||
| }); | ||
|
|
||
| it('does not emit inert directive scripts when already deduplicated', () => { | ||
| const result = createStubResult(); | ||
| const instruction = createRenderInstruction({ | ||
| type: 'directive', | ||
| hydration: { | ||
| directive: 'load', | ||
| value: '', | ||
| componentUrl: '', | ||
| componentExport: { value: '' }, | ||
| }, | ||
| }); | ||
|
|
||
| result._metadata.hasHydrationScript = true; | ||
| result._metadata.hasDirectives.add('load'); | ||
| result._metadata.templateDepth = 1; | ||
|
|
||
| const inertOutput = chunkToString(result as any, instruction); | ||
| assert.equal(inertOutput, ''); | ||
| }); | ||
|
|
||
| it('does not consume renderer hydration dedup inside templates', () => { | ||
| const result = createStubResult(); | ||
| const rendererInstruction = createRenderInstruction({ | ||
| type: 'renderer-hydration-script', | ||
| rendererName: 'react', | ||
| render: () => '<script>window.__react = true;</script>', | ||
| }); | ||
|
|
||
| result._metadata.templateDepth = 1; | ||
| const inertOutput = chunkToString(result as any, rendererInstruction).toString(); | ||
| assert.match(inertOutput, /__react/); | ||
| assert.equal(result._metadata.rendererSpecificHydrationScripts.has('react'), false); | ||
|
|
||
| result._metadata.templateDepth = 0; | ||
| const executableOutput = chunkToString(result as any, rendererInstruction).toString(); | ||
| assert.match(executableOutput, /__react/); | ||
| assert.equal(result._metadata.rendererSpecificHydrationScripts.has('react'), true); | ||
| }); | ||
|
|
||
| it('does not consume server-island runtime dedup inside templates', () => { | ||
| const result = createStubResult(); | ||
| const instruction = createRenderInstruction({ type: 'server-island-runtime' }); | ||
|
|
||
| result._metadata.templateDepth = 1; | ||
| const inertOutput = chunkToString(result as any, instruction).toString(); | ||
| assert.match(inertOutput, /replaceServerIsland/); | ||
| assert.equal(result._metadata.hasRenderedServerIslandRuntime, false); | ||
|
|
||
| result._metadata.templateDepth = 0; | ||
| const executableOutput = chunkToString(result as any, instruction).toString(); | ||
| assert.match(executableOutput, /replaceServerIsland/); | ||
| assert.equal(result._metadata.hasRenderedServerIslandRuntime, true); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.