Serve SPA fallback with 200 for routable paths in prod static serving

Merged
FA
2mo ago
+490-12

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

In self-hosted prod (reflex run --env prod), a direct load of a valid dynamic-route
URL (e.g. /articles/7) returned HTTP 404 with the SPA-fallback body, making valid
dynamic URLs indistinguishable from genuinely unknown paths โ€” bad for SEO, uptime
monitors, and anything trusting status codes.

PrecompressedStaticFiles now accepts a route matcher: when Starlette's html-mode
404.html fallback is hit for a path that matches the app's route table, it is served
with status 200; unroutable paths keep the 404 status. The backend-mounted frontend
passes app.router directly; for the standalone prod static server (frontend-only
mode), the compiler persists the route table to .web/routes.json at compile time and
the mount builds a matcher from it, falling back to the previous behavior when no
manifest exists (the manifest lives outside build/client, so it is not publicly
served). Configured frontend_path prefixes are restored before matching since the
mount strips them from request paths.

Covered by new unit tests (routable โ†’ 200 incl. precompressed sidecars, unknown โ†’ 404,
real files unaffected, manifest loading, frontend_path handling) and an extended
tests/integration/test_precompressed_frontend.py with an articles/[id] page.

closes #6983