Replies: 2 comments 4 replies
-
|
I like the idea of getting rid of This could be just as easily achieved with just setting a data attribute on a div or any other element. Or even on the script itself and use ---
const data = {title: "My Page Title"}
---
<div id="my-data" data-data={JSON.stringify(data)}></div>
<script>
const dataElement = document.getElementById("my-data")
const data = JSON.parse(dataElement.dataset.data)
</script> |
Beta Was this translation helpful? Give feedback.
-
|
I don't think this should be removed without an ergonomic replacement. The dataset workaround is pretty awkward for cases other than custom elements. A new vars API that fixed the drawbacks of |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
Currently, Astro supports a
define:varsdirective on<style>and<script>elements. I propose to drop support fordefine:varson scripts. Support with styles would be unchanged.Related
Background & Motivation
define:varswas introduced as a way to help pass data from the server to the client. For example:However, it has many gotchas that confuse users and is arguably an anti-pattern with better options available (and documented by us).
define:varsforces a script to be inline and not bundled, which surprises users given this is a first-class feature, and pushes them to use a less performant technique.import, TypeScript usage etc. because inline scripts are rendered as-is without processing.define:varsis one reason we can’t improve the experience of usingis:inline: we can’t work on optimizing inline scripts, providing CSP support, etc. whendefine:varscan make scripts only knowable at render time (see e.g. Improved inline script support #995)By removing
define:varswe could stop confusing users and focus on our other techniques we document like our “Pass frontmatter variables to scripts” docs.Goals
define:varssupport on scripts.Non goals
Example
One migration path for users is to switch to the custom element approach we recommend in docs. In this case this has the added benefit of switching to a bundled script.
In this minimal example the additional boilerplate does stand out so is worth discussing. However, it is not a goal of this proposal to invent a replacement, and in real-world usage the boilerplate difference is minimal.
For users who want to keep the inline behaviour for some reason they can still do so and avoid the boilerplate:
^ For me, this is a good example of why our custom API may be superfluous. Our feature isn’t really adding something you can’t already do, but it promotes it even though it’s not the best approach.
Impact
There are almost twelve thousand example usages of
<script define:vars={on GitHub at time of writing, so no doubt this is a breaking change with significant impact, but a deprecation period of one major followed by removal in the next major could realistically mean this would be a two-year process, which is a long time for people to adjust.Beta Was this translation helpful? Give feedback.
All reactions