← Back to Frontend, page 2

frontend-0040 · Medium mid

In a single-page app, what does a screen reader user miss after a route change, and how do you fix it with focus and a live region?

A normal page load resets focus and announces the new page. A client-side route change does neither, so the user hears nothing. Update the document title, move focus to the new page heading or main region, and use a polite live region for status messages.

Full answer

A full page load gives a screen reader user several things for free: focus goes to the top of the document, the new title is announced, and the reading position resets. A single-page app replaces content without a load. Focus stays on the link that was clicked, which may no longer exist, and the screen reader says nothing. Keyboard users have to Tab through the navigation again to reach the new content.

A repair has three parts:

  1. Update document.title on every route change, so that history, tabs and screen readers have a distinct name for the view.
  2. Move focus to a sensible target after the new view is rendered. A common choice is the h1 of the new page, given tabindex="-1" so that it can receive programmatic focus without joining the Tab order. Another choice is the main element. Call focus() after the DOM update, for example in an effect after render.
  3. Announce state changes that do not move focus, using a live region.

Live regions (aria-live):

  • role="status" or aria-live="polite" waits until the user is idle. Use it for "3 results found", "Saved", or "Page loaded".
  • role="alert" or aria-live="assertive" interrupts. Use it rarely, for errors that block progress.
  • The region must exist in the DOM before its text changes. A region inserted together with its message is often not announced.
  • Keep messages short and replace the text, instead of adding endless child nodes.

Test with a real screen reader too. Support differs between combinations of browser and reader, and an automated check cannot hear the output.

The page below simulates a route change. It sets the title, swaps the content, focuses the heading, and writes a message to a status region. The test checks the active element and the region text. No screen reader was run.

html
<nav><a href="#/courses" id="go">Courses</a></nav>
<main id="view"><h1 tabindex="-1" id="heading">Home</h1></main>
<p id="live" role="status"></p>
<script>
  function navigate(title) {
    document.title = title;
    view.innerHTML = '<h1 tabindex="-1" id="heading"></h1>';
    document.getElementById('heading').textContent = title;
    document.getElementById('heading').focus();          // move focus after the DOM update
    live.textContent = `${title} page loaded`;           // polite announcement
  }
  go.addEventListener('click', (e) => { e.preventDefault(); navigate('Courses'); });
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The check clicked the link and read the focused element, the title and the live region text. No screen reader was run, so what is announced was not heard.

Key points

  • Client-side navigation does not move focus or announce the new view.
  • Update document.title on each route change.
  • Focus the new h1 with tabindex -1, or the main region, after render.
  • Use a polite status region for messages. Reserve alert for blocking errors.
  • The live region must exist before the text changes.

Common mistakes

  • Leaving focus on a link that was removed from the page.
  • Giving the heading tabindex 0, which adds it to the Tab order.
  • Creating the live region at the same time as its message.
  • Using assertive for every message.

Follow-ups

  • What does tabindex -1 do compared to 0?
  • How would you restore focus when the user goes back?
  • Why test with more than one screen reader?

Tips

  • Say what a full page load does for free, then rebuild it in three steps.
  • Mention testing with a real screen reader.