fix: prevent script dedup inside <template>#15980
fix: prevent script dedup inside <template>#15980seroperson wants to merge 1 commit intowithastro:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 6202b78 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 |
8a2e551 to
a11ac94
Compare
866a50e to
949a1e1
Compare
packages/astro/src/runtime/server/render/astro/render-template.ts
Outdated
Show resolved
Hide resolved
matthewp
left a comment
There was a problem hiding this comment.
This is a very expensive change, for something that is a bit of a corner case. I don't want to make this change this way. Will explain an alternative approach in a comment.
|
Basically we need to know that we are "inside of a template" in order to maybe not emit the script. I think that's really only possible through a compiler change. If you have that, you would know when you've exited all possible templates and then could emit the script. cc @Princesseuh |
949a1e1 to
6202b78
Compare
|
@matthewp Thank you for the review! Oh, I wasn't aware about such ability at all. Yea, compiler-based approach looks much better. I filled the PR to compiler and did necessary changes here to support it. Well, then waiting compiler to be merged and then I'll update the version here. P.S. I actually "moved" this render instruction to be inlined by compiler, so logic within this PR left mostly unchanged (except placing instructions of course). |
Closes #15389
When a component with a script was first used inside a
<template>, Astro's deduplication prevented the script from rendering outside the template, breaking non-template instances. This PR makes such scripts are not deduplicated. Should be merged after withastro/compiler#1156.Changes
templateEnter/templateExitfunctions to be inlined by compiler inside of<template>tags. These functions add instructions that track<template>nesting depth during rendering.stringifyChunkskips dedup for scripts whentemplateDepth > 0. The script still renders inside the template but is also rendered outside for non-template instances.Testing
Added
astro-script-template-dedupintegration test with four cases:<template>first, then outside: verifies script appears in both places<template>elements: verifies depth tracking handles nestingDocs
Docs aren't required as it's a bugfix.