Replies: 3 comments 3 replies
-
|
What happens if someone does: <script is:inline src="https://somecdn.com/myscript.js"></script> |
Beta Was this translation helpful? Give feedback.
-
|
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.
-
|
Adding another motivation for us to think about: CSP support. Currently, inline scripts are not handled by Astro's CSP feature. Moving these to be processed might give us the opportunity to automatically add hashes for people rather than forcing them to move to a very manual approach if they want to enable CSP. |
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.
-
Summary
Adding support for minification and TypeScript to
<script is:inline>would improve authoring experience and output performance.Background & Motivation
Astro has an
is:inlinedirective 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
timesFiveidentifier 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
importstatement would not be bundled.Beta Was this translation helpful? Give feedback.
All reactions