agentleFS
casooncasoon/ai-agent-config/skills/astro-client-scripts/SKILL.md

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.

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
  1. Distinctive:

    No global function-name collisions.

    line 14
  2. Very specific:

    Always use <script> unless you have a specific reason for is:inline.

    line 17
  3. Very specific:

    Add TypeScript casts where needed (as HTMLElement, as TouchEvent).

    line 53
  4. Very specific:

    Remove typeof window guards — modules only run in the browser.

    line 54
  5. Distinctive:

    Must execute synchronously before first paint:

    line 60
  6. Very specific:

    When <ClientRouter /> is active (Astro v6 SPA transitions):

    line 110
  7. Very specific:

    Use astro:after-swap for code that must re-run after SPA navigation.

    line 114
  8. Very specific:

    Use astro:page-load for code that should run on every page, including the initial load.

    line 115

≈ “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.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

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 (no DOMContentLoaded wrapper 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:

  1. Remove is:inline.
  2. Unwrap initXyz() — module code runs at top level.
  3. Drop DOMContentLoaded (modules are deferred by default).
  4. Add TypeScript casts where needed (as HTMLElement, as TouchEvent).
  5. Remove typeof window guards — 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:inline scripts re-run on every navigation by default.
  • Module <script> tags run once and persist across navigations.
  • Use astro:after-swap for code that must re-run after SPA navigation.
  • Use astro:page-load for 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

MistakeFix
is:inline "just to be safe"Remove it — modules are better in every way
DOMContentLoaded wrapper in a module scriptDrop it — modules are deferred
typeof window !== 'undefined' in a moduleRemove — browser-only by definition
Global function names in is:inlineUse scoped <script> or an IIFE
Multiple is:inline scripts that could mergeCombine into one scoped <script>
is:inline for Astro lifecycle listenersOnly needed in <head>, otherwise use a module

Download now (ZIP)

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
What did you use it for? e.g. Reviewing pull requests in a Rails app

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.