Replies: 2 comments 1 reply
-
What happens if someone does: <script is:inline src="https://somecdn.com/myscript.js"></script> |
Beta Was this translation helpful? Give feedback.
1 reply
-
Completely agree. The worst part is that setting Basically I can't ever be 100% sure that I can use typescript if I suddenly find myself in need of a server defined var. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Adding support for minification and TypeScript to
<script is:inline>
would improve authoring experience and output performance.Background & Motivation
Astro has an
is:inline
directive for script tags that prevents a tag from being bundled and hoisted. Currently, the contents of an inline script are not processed at all and shipped verbatim to the browser.One common use case for inline scripts is to ensure some code executes at a specific point in the page load lifecycle. The classic example is colour scheme preference, where an inline script should execute and set a dark/light mode preference as early as possible to avoid a flash of the incorrect colour scheme.
Astro’s current behaviour means you are forced to write your inline script in plain JavaScript and (ideally) author it in a fairly terse way, because all comments etc. will be shipped to users as is. This makes for a less good authoring/maintenance experience, but also for a worse end-user experience if they have to download larger than necessary script tags.
As an example, Starlight has multiple places where it uses inline scripts to control page behaviour during the page load lifecycle and each of these needs to be authored carefully and in a less maintainable way to avoid shipping overly verbose inline code.
Goals
Example
The proposal here would be to process inline scripts without bundling and hoisting them. Ideally this would include support for TypeScript types as well.
For example, a user might author:
And the build output could look something like:
This would enable people to author with inline comments, expressive variable naming, types, etc., while still shipping a minimal script with the same functionality to the browser. (This would need to use a minification pipeline that respects the behaviours of global HTML scripts, e.g. in the example above the
timesFive
identifier will be attached toglobalThis
, so should not be minified in case another script somewhere is relying on it.)Other restrictions of inline scripts would still apply. For example an
import
statement would not be bundled.Beta Was this translation helpful? Give feedback.
All reactions