Skip to main content

App setup

fragmentFocusScript

The inline script that keeps a person's focus where it is on a page whose address names a place on it (#reviews). Safari jumps back to that place each time a stylesheet loads, until the person scrolls, once the page finished parsing or a link to a place on it was followed while a stylesheet was still loading: on a slow connection, or with a route's CSS that the app's script adds as the page starts. Each jump takes focus from wherever it was: out of an open menu, or a field mid-word. The script stops it. The parser holds an inline script until the stylesheets above it have loaded; a stylesheet a script added that is still loading as parsing ends is set aside for that moment, then carries on; and a link to a place on the page, clicked while a stylesheet is loading, goes there once it has loaded (two seconds at most), unless the page has moved on. Other browsers don't jump back, so it leaves them alone. Put it first in <body>, after every stylesheet, as an inline classic script: not a module, and not async or defer. Painting waits for those stylesheets anyway, so nothing shows later. It is plain text, so a Server Component can render it. Use nothing instead on a page with no stylesheet.

import { fragmentFocusScript } from "@/lib/ouro/fragment-focus-script";

Example

<html lang="en">
  <head>{stylesheets}</head>
  <body>
    <script dangerouslySetInnerHTML={{ __html: fragmentFocusScript() }} />
    {children}
  </body>
</html>

Returns

string