Skip to contentView the source on GitHub

require-client-router-script-lifecycle

Initialize scripts on astro:page-load when using ClientRouter.

Best Practiceswarning by default Project auditstrict audit

astro-doctor/require-client-router-script-lifecycle

Why it matters

ClientRouter navigation swaps page content without reloading the document, so DOMContentLoaded only initializes the first page. Astro emits astro:page-load after the initial load and every client-side navigation.

See the difference

Incorrect

Only initializes after the first document load

astroIncorrect example
<script>
  document.addEventListener('DOMContentLoaded', initialize)
</script>

Correct

Initializes after every routed page load

astroCorrect example
<script>
  document.addEventListener('astro:page-load', initialize)
</script>

Configure this check

Project audits are configured in doctor.config.ts:

typescriptdoctor.config.ts
export default {
  rules: {
    'astro-doctor/require-client-router-script-lifecycle': 'error',
  },
}

Use 'error', 'warn', or 'off'. The configuration guide covers presets and per-file overrides.

Keep exploring best practices

Browse every rule and project audit