Skip to content
Best Practiceswarn by defaultstrict presetastro-doctor/require-client-router-script-lifecycle

require-client-router-script-lifecycle

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

Why

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.

Examples

Incorrect

Only initializes after the first document load

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

Correct

Initializes after every routed page load

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

Configuration

Project audits are configured in doctor.config.ts:

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

All rules