Skip to content

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

Open
benedikt-bartscher wants to merge 20 commits into
reflex-dev:mainfrom
benedikt-bartscher:page-stylesheets
Open

benedikt-bartscher wants to merge 20 commits into
reflex-dev:mainfrom
benedikt-bartscher:page-stylesheets

Conversation

@benedikt-bartscher

@benedikt-bartscher benedikt-bartscher commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

View guided diff

@FarhanAliRaza FarhanAliRaza added documentation Improvements or additions to documentation enhancement Anything you want improved labels Nov 25, 2026 — with ChatGPT Codex Connector
@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable issue was found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issue was found.

Summary

Adds page-only stylesheets through add_page and @rx.page, with React Router managing their links.

  • Pages load their own stylesheets only while their route is active.
  • Same-stem Sass files keep separate compiled names.

Reviews (6) · Last reviewed commit: "Merge commit '24d84bcd1a' into page-styl..." · Reviewed by Greptile

Comment thread reflex/app.py Resolved
Comment thread reflex/compiler/compiler.py Resolved
Comment thread tests/units/compiler/test_compiler.py Resolved
@codspeed

codspeed Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 11 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 146 untouched benchmarks
⏩ 8 skipped benchmarks1


Comparing benedikt-bartscher:page-stylesheets (59c3190) with main (6f5c39c)

Open in CodSpeed

Footnotes

  1. 8 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@benedikt-bartscher
benedikt-bartscher marked this pull request as ready for review November 26, 2026 08:49
@benedikt-bartscher
benedikt-bartscher requested review from a team and Alek99 as code owners November 26, 2026 08:49
@FarhanAliRaza FarhanAliRaza added the bug Something isn't working label Nov 26, 2026 — with ChatGPT Codex Connector

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 15 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread reflex/app.py Resolved
Comment thread reflex/compiler/compiler.py Resolved
Comment thread reflex/compiler/compiler.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Improvements or additions to documentation enhancement Anything you want improved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants