Support synchronous script block #1079
Closed
KiritaniAyaka
started this conversation in
Proposal
Replies: 1 comment 1 reply
-
I suspect supporting bundling for inline scripts may be an anti-pattern: it makes it very easy to ship big chunks of JavaScript multiple times in a page by importing a hefty node module. That said, supporting TypeScript transpilation and inline script minification would be nice, I proposed that here: #995 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Support process script and inject it as a synchronous script block by an
is:sync
directive.Background & Motivation
Asynchronous script is a modern feature but synchronous script also plays an important role in some scenes.
For example, in theme switching, it has to check user's preference from local storage(or other places) synchronously; otherwise, async will cause a different theme flash. This is especially obvious when user's network is slow.
Goals
Example
Proposed API
In a .astro layout file:
Outputs:
Fallback
We can achieve this currently in Astro by
is:inline
directive and it could be a fallback if this proposal is not applied.Although
is:inline
works, we lose some features provided by Astro, such as optimize and bundle.In the example above, we have to maintain the logic of
getUserPreference
in two different places (sync script block and another theme switcher component) because importing local script is unavailable in anis:inline
script. And script minify is also impossible withis:inline
.Beta Was this translation helpful? Give feedback.
All reactions