-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ssr wip #6173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
benedikt-bartscher
wants to merge
12
commits into
reflex-dev:main
Choose a base branch
from
benedikt-bartscher:ssr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
ssr wip #6173
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2272cc0
wip
benedikt-bartscher f120b60
Merge remote-tracking branch 'upstream/main' into ssr
benedikt-bartscher 0606e2b
fix: add fixture to clean state subclasses after tests
benedikt-bartscher 32bd4a7
make ssr deps optional
benedikt-bartscher 1158c3c
add integration test
benedikt-bartscher 0287832
fix typing
benedikt-bartscher 263d3eb
codespell
benedikt-bartscher fe877a6
fix
benedikt-bartscher 9369532
fix hashes
benedikt-bartscher 2feca15
fix js formatting
benedikt-bartscher 0274b8e
cache this
benedikt-bartscher d3d9c74
Merge remote-tracking branch 'upstream' into ssr
benedikt-bartscher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Post-build script: generates a static SPA shell (build/client/index.html). | ||
| * | ||
| * With ssr:true, `react-router build` does not emit index.html because all | ||
| * HTML is rendered at request time. The production server (ssr-serve.js) | ||
| * serves this pre-built shell to regular users for instant load with zero | ||
| * SSR overhead; only bots go through the SSR path. | ||
| * | ||
| * The X-Reflex-Shell-Gen header tells the root loader to short-circuit and | ||
| * return { state: null } without contacting the Python backend. | ||
| */ | ||
| import { createRequestHandler } from "react-router"; | ||
| import { writeFileSync } from "node:fs"; | ||
| import { dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| // Resolve paths relative to this file, not process.cwd(). | ||
| const __dirname = | ||
| import.meta.dirname ?? dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const build = await import(join(__dirname, "build", "server", "index.js")); | ||
| const handler = createRequestHandler(build, "production"); | ||
|
|
||
| const request = new Request("http://localhost/", { | ||
| headers: { | ||
| "User-Agent": "Mozilla/5.0 Chrome/120 (Shell Generator)", | ||
| "X-Reflex-Shell-Gen": "1", | ||
| }, | ||
| }); | ||
|
|
||
| const response = await handler(request); | ||
| const html = await response.text(); | ||
|
|
||
| const outPath = join(__dirname, "build", "client", "index.html"); | ||
| writeFileSync(outPath, html); | ||
| console.log("Generated build/client/index.html"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * Bot-aware SSR production server for Reflex apps. | ||
| * | ||
| * - Crawlers/bots receive fully server-side rendered HTML (SEO). | ||
| * - Regular users receive the static SPA shell (fast, zero SSR overhead). | ||
| * | ||
| * Used when `runtime_ssr=True` is set in the Reflex config. | ||
| */ | ||
| import { createRequestHandler } from "@react-router/express"; | ||
| import express from "express"; | ||
| import compression from "compression"; | ||
| import { isbot } from "isbot"; | ||
| import { existsSync, readFileSync } from "node:fs"; | ||
| import { dirname, join } from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
|
|
||
| // Resolve all paths relative to *this file*, not process.cwd(). | ||
| const __dirname = | ||
| import.meta.dirname ?? dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const clientDir = join(__dirname, "build", "client"); | ||
| const serverEntry = join(__dirname, "build", "server", "index.js"); | ||
|
|
||
| const buildModule = await import(serverEntry); | ||
|
|
||
| const app = express(); | ||
| app.disable("x-powered-by"); | ||
| app.use(compression()); | ||
|
|
||
| // Static assets with content-hash filenames — cache immutably. | ||
| app.use( | ||
| "/assets", | ||
| express.static(join(clientDir, "assets"), { immutable: true, maxAge: "1y" }), | ||
| ); | ||
|
|
||
| // Other static files (favicon, sitemap, etc.) — short cache. | ||
| app.use(express.static(clientDir, { maxAge: "1h" })); | ||
|
|
||
| // SSR request handler (React Router). | ||
| const ssrHandler = createRequestHandler({ build: buildModule }); | ||
|
|
||
| // Read the static SPA shell into memory at startup (generated by generate-shell.mjs). | ||
| // Serving from memory avoids per-request filesystem access. | ||
| const shellPath = join(clientDir, "index.html"); | ||
| const shellHtml = existsSync(shellPath) | ||
| ? readFileSync(shellPath, "utf-8") | ||
| : null; | ||
| if (!shellHtml) { | ||
| console.warn( | ||
| "[ssr-serve] build/client/index.html not found — all requests will use SSR.", | ||
| ); | ||
| } | ||
|
|
||
| app.all("*", (req, res, next) => { | ||
| const ua = req.headers["user-agent"] || ""; | ||
|
|
||
| // Bots always get full server-side rendered HTML with state data. | ||
| // Also used as fallback when the static shell is unavailable. | ||
| if (isbot(ua) || !shellHtml) { | ||
| return ssrHandler(req, res, next); | ||
| } | ||
|
|
||
| // For regular users: only serve the SPA shell for initial document requests | ||
| // (browser navigating to a URL). React Router's .data requests (used for | ||
| // client-side navigations) and other non-document fetches must go through | ||
| // the SSR handler so the root loader can run and return JSON state data. | ||
| const accept = req.headers["accept"] || ""; | ||
| if (accept.includes("text/html") && !req.url.endsWith(".data")) { | ||
| return res | ||
| .setHeader("Content-Type", "text/html; charset=utf-8") | ||
| .send(shellHtml); | ||
| } | ||
|
|
||
| // .data requests, API calls, etc. → SSR handler (runs loaders, returns JSON). | ||
| return ssrHandler(req, res, next); | ||
| }); | ||
|
|
||
| const requestedPort = parseInt(process.env.PORT || "3000", 10); | ||
| const server = app.listen(requestedPort, () => { | ||
| // Emit the actual port (important when PORT=0 for auto-assignment). | ||
| // Message format matches Reflex's PROD_FRONTEND_LISTENING_REGEX. | ||
| const actualPort = server.address().port; | ||
| console.log(`[ssr-serve] http://localhost:${actualPort}`); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.