#Astro hot reload

3 messages · Page 1 of 1 (latest)

marsh harbor
#

Is there a way to prevent my browser from jumping back to the start of the page when I save in VSCode?

twin loomBOT
#

Hi @marsh harbor. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro’s docs only briefly mention how navigation scroll is handled with the <ClientRouter />, not general “save in editor → dev HMR” scroll behavior.

For client-side router navigations (not file saves), you can override scroll restoration by listening for the astro:after-swap lifecycle event and manually setting scroll, e.g. always back to top:

document.addEventListener("astro:after-swap", () =>
  window.scrollTo({ left: 0, top: 0, behavior: "instant" })
);

This fires after the new page has replaced the old one and after Astro’s own scroll handling has run, letting you override the default behavior. [astro:after-swap]

However, the provided docs do not describe any configuration or hook that specifically controls scroll position when the dev server hot-reloads due to a file save (HMR in astro dev). [astro dev]

So:

  • For scroll behavior on router navigations, use astro:after-swap as above.
  • For scroll jumps caused specifically by hot reload on save, the knowledge sources don’t document a way to change that behavior.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: