[RFC Early Feedback] Wrapping up default <script> behavior #65
Replies: 5 comments 2 replies
-
This is definitely one that I'll need to revisit a couple times to think through all the implications, but I really like the approach in general 👍 Treating styles and scripts similarly to when they are/aren't compiled, bundled, and optimized makes a lot of sense to me I also like the idea of optimizing by default with an escape hatch when needed. Very curious how more people feel about that one though, I'm sure there are others that would expect or prefer optimization to be opt-in only |
Beta Was this translation helpful? Give feedback.
-
A key use case that this proposal doesn't cover is how to handle polyfills and legacy code, where should the polyfill code be placed to function properly, how to bundle legacy code using legacy configs, etc... |
Beta Was this translation helpful? Give feedback.
-
I like the alignment with style overall. Some things to think about here though, as I think scripts a little more complex:
|
Beta Was this translation helpful? Give feedback.
-
Does this mean every And I’d much rather |
Beta Was this translation helpful? Give feedback.
-
Ran into a very relevant use case today, for what it's worth! Use case The 11ty project has a shared In Astro Nothing revolutionary here, but I thought it was pretty interesting figuring out how a pretty clean way of doing this. Now I just need to write my own carousel web component so none of this is needed at all 😄 /** in src/components/blocks/ReviewsBlock.astro */
<section>
<div id='reviews'>
// builds the HTML for each review card
</div>
</section>
<script type="module" hoist>
import Siema from "siema"
const reviewsElem = document.getElementById("reviews");
// initialize the Siema scroller
const siema = new Siema({
selector: "#reviews",
loop: true,
perPage: {
640: 2,
1024: 3,
},
duration: 300,
});
// auto-scroll every 5 seconds
let timer = setInterval(() => siema.next(), 5000);
// disable scroll while hovering
reviewsElem.addEventListener("mouseenter", function () {
clearInterval(timer);
});
// enable scrolling again
reviewsElem.addEventListener("mouseleave", function () {
timer = setInterval(() => siema.next(), 5000);
});
</script> |
Beta Was this translation helpful? Give feedback.
-
In previous discussions, we have worked to finalize
<style>
and<script>
behavior in Astro components. I had first proposed opt-in magic via a special@component
attribute, but the feedback was largely negative on this idea and users were more in favor of how we currently do scoped, processed, and optimized styles by default.Armed with better feedback, I still want us to address the inconsistency between
<style>
and<script>
default behavior as a part of v1.0. Currently,<style>
has opt-out processing, but<script>
does not. This is still confusing and unintuitive, and many questions & support threads that I see are impacted by this.Proposal (draft)
I would like to propose the following as an RFC, and would love your early feedback:
tldr: Change
<script>
to use current<script hoist>
behavior, and add<script is:inline>
to use current<script>
behavior.hoist
behavior the default<script>
behavior. This aligns it with<style>
behavior that users love and encourages our "optimized by default" / "pit of success" mentality.is:inline
. This can be useful for scripts that must exist inlined into the page, like Google Analytics.src=
,type="module"
,async
) or client-directive (define:vars
) then emit a warning thatis:inline
is needed. This is because a bundled script is added to some other JS file, so the script element itself will not exist in the final build.Pros:
<style> @import
behavior.More details on behavior specifics found here in this accepted RFC: https://github.com/withastro/rfcs/blob/style-script-rfc/active-rfcs/0000-style-script-behavior.md#script
Open Questions:
is:inline
support for<style>
here as well? The same reasoning applies to styles as well, and currently there is no way to add a<style>
element directly to the page, inlined with the template.Beta Was this translation helpful? Give feedback.
All reactions