feat: add_page(stylesheets=...) for stylesheets only one page loads

BE
1d ago
+497-52

App(stylesheets=...) loads every stylesheet on every page. A page that needs a stylesheet of its own has no good option today: a global stylesheet costs every route, and React 19's <link rel="stylesheet" precedence> hoisting puts the sheet at the top of <head>, before the app's global stylesheet, so the app's rules beat the page's at equal specificity (and a hoisted sheet is never removed).

This PR adds stylesheets= to add_page and @rx.page:

app.add_page(pricing, route="/pricing", stylesheets=["/pages/pricing.css"])

Entries are what App.stylesheets takes: files or directories in assets/ (css, sass, scss) or URLs. They compile into the page's route module as React Router's links export:

import reflexPageStylesheet0 from "$/styles/pages/pricing.css?url"
export const links = () => [({ ["rel"] : "stylesheet", ["href"] : reflexPageStylesheet0 })];

React Router then

  • renders them in <head> after the app's global stylesheet, in prerendered and exported HTML too, so page rules come after app rules;
  • loads them before it renders the page on client-side navigation (prefetchStyleLinks), so the page never shows unstyled;
  • removes them when the page is left.

Asset files go through the copy/sass step of the app's stylesheets, factored out of _compile_root_stylesheet (whose output is unchanged), and are imported with ?url, so Vite processes and fingerprints them like the global stylesheet. PageContext carries the page's stylesheets, page_template takes the links var, and the staged add_page a plugin receives in register_route accepts stylesheets=.

Follow-up for #6796: I18nPlugin.expand_routes re-adds each page per locale with explicit arguments, so it has to pass stylesheets=page.stylesheets along.