astro-client-scripts
Skill1 starsChanged 46 days agoMITView on GitHub
Client-side script patterns in Astro — when to use scoped <script> vs is:inline, ClientRouter behavior, define:vars, and when to reach for a Svelte island instead. Use when adding or reviewing <script> tags in .astro files.
In this repository81 other files it gives its agents.
- accessibility-action-planSkill
- accessibility-auditSkill
- accessibility-finding-explainerSkill
- accessibility-report-summarySkill
- agent-config-authoringSkill
- agents-sdkSkill
- ai-ux-patternsSkill
- alt-text-generatorSkill
- anti-ai-copySkill
- aria-usage-reviewSkill
- astro-architectureSkill
- astro-component-a11y-reviewSkill
- before-after-accessibility-commsSkill
- bfsg-risk-language-softenerSkill
- biomeSkill
- blog-from-transcriptSkill
- brand-voice-dnaSkill
- case-study-from-auditSkill
- client-email-after-auditSkill
- cloudflare-email-serviceSkill
- cloudflare-one-migrationsSkill
- cloudflare-oneSkill
- cloudflareSkill
- complexity-judgmentSkill
- component-doc-writerSkill
- content-clarity-a11y-checkSkill
- content-qaSkill
- conversation-retrospectiveSkill
- darkmodeSkill
- deep-researchSkill
- design-directionsSkill
- design-systemsSkill
- durable-objectsSkill
- faq-from-issuesSkill
- form-accessibility-reviewSkill
- frontend-designSkill
- handoffSkill
- i18nSkill
- image-to-webpSkill
- keyboard-navigation-reviewSkill
- knowledge-baseSkill
- landing-and-site-builderSkill
- landingpage-from-briefingSkill
- lighthouse-axe-result-explainerSkill
- linkedin-post-from-contentSkill
- llm-councilSkill
- local-business-seoSkill
- mcp-builderSkill
- mdx-contentSkill
- motion-designSkill
- native-html-uiSkill
- newsletter-from-auditSkill
- overlay-tool-risk-explainerSkill
- playwrightSkill
- post-auditSkill
- remotionSkill
- sandbox-migrate-to-nextSkill
- sandbox-nextSkill
- sandbox-sdkSkill
- sandbox-stableSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 2 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 14
No global function-name collisions.
- Very specific: line 17
Always use <script> unless you have a specific reason for is:inline.
- Very specific: line 53
Add TypeScript casts where needed (as HTMLElement, as TouchEvent).
- Very specific: line 54
Remove typeof window guards — modules only run in the browser.
- Distinctive: line 60
Must execute synchronously before first paint:
- Very specific: line 110
When <ClientRouter /> is active (Astro v6 SPA transitions):
- Very specific: line 114
Use astro:after-swap for code that must re-run after SPA navigation.
- Very specific: line 115
Use astro:page-load for code that should run on every page, including the initial load.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Astro client scripts
Core rule — prefer scoped <script> over is:inline
Astro's default <script> (without is:inline) is a module script that gets:
- Bundled, deduplicated, and tree-shaken by Vite.
- TypeScript support inside the block.
- No global function-name collisions.
- Automatic
defer(noDOMContentLoadedwrapper needed).
Always use <script> unless you have a specific reason for is:inline.
Migration pattern
Before (anti-pattern)
<script is:inline>
function initMyComponent() {
const el = document.querySelector('.my-el');
// ...
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initMyComponent);
} else {
initMyComponent();
}
</script>After
<script>
const el = document.querySelector('.my-el');
if (el) {
// ...logic directly at module top level
}
</script>Steps:
- Remove
is:inline. - Unwrap
initXyz()— module code runs at top level. - Drop
DOMContentLoaded(modules are deferred by default). - Add TypeScript casts where needed (
as HTMLElement,as TouchEvent). - Remove
typeof windowguards — modules only run in the browser.
When is:inline IS required
1. Theme / FOUC prevention in <head>
Must execute synchronously before first paint:
<script is:inline>
function getStoredTheme() {
try {
return document.cookie.match(/(?:^|;\s*)theme=(dark|light)(?:;|$)/)?.[1]
|| localStorage.getItem('theme');
} catch { return null; }
}
function applyTheme() {
document.documentElement.classList.remove('no-js');
const stored = getStoredTheme();
if (stored === 'dark' || (!stored && matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}
applyTheme();
if (window.__applyThemeHandler) {
document.removeEventListener('astro:after-swap', window.__applyThemeHandler);
}
window.__applyThemeHandler = applyTheme;
document.addEventListener('astro:after-swap', window.__applyThemeHandler);
</script>2. Scripts using define:vars
define:vars requires is:inline:
<script is:inline define:vars={{ baseColor, endpoint }}>
console.log(baseColor, endpoint);
</script>3. Third-party snippets that must not be bundled
Analytics, tracking pixels, classic scripts:
<script is:inline src="https://analytics.example.com/script.js"></script>ClientRouter (SPA) considerations
When <ClientRouter /> is active (Astro v6 SPA transitions):
is:inlinescripts re-run on every navigation by default.- Module
<script>tags run once and persist across navigations. - Use
astro:after-swapfor code that must re-run after SPA navigation. - Use
astro:page-loadfor code that should run on every page, including the initial load.
<script>
document.addEventListener('astro:page-load', () => {
const el = document.querySelector('.my-el');
if (el) {
// re-initialize on every navigation
}
});
</script>Prefer a Svelte island for real interactivity
For anything beyond simple DOM manipulation, reach for a Svelte 5 island:
<script lang="ts">
let dark = $state(document.documentElement.classList.contains('dark'));
function toggle() {
dark = !dark;
document.documentElement.classList.toggle('dark', dark);
const theme = dark ? 'dark' : 'light';
document.cookie = `theme=${theme}; path=/; max-age=31536000; SameSite=Lax`;
localStorage.setItem('theme', theme);
}
</script>
<button onclick={toggle} aria-label="Toggle theme">
{dark ? 'Light' : 'Dark'}
</button><ThemeToggle client:load />Decision tree
Need client-side JS?
├─ Simple DOM query / manipulation → <script> (no is:inline)
├─ Reactive state / complex UI → Svelte 5 island (client:load/idle/visible)
├─ Must run before paint (theme / FOUC) → <script is:inline> in <head>
├─ Needs define:vars → <script is:inline define:vars={…}>
└─ Third-party snippet → <script is:inline src="…">Common mistakes
| Mistake | Fix |
is:inline "just to be safe" | Remove it — modules are better in every way |
DOMContentLoaded wrapper in a module script | Drop it — modules are deferred |
typeof window !== 'undefined' in a module | Remove — browser-only by definition |
Global function names in is:inline | Use scoped <script> or an IIFE |
Multiple is:inline scripts that could merge | Combine into one scoped <script> |
is:inline for Astro lifecycle listeners | Only needed in <head>, otherwise use a module |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

