Skip to content

RFC 30: SSR: the app takes its request #31

Open

Description

@yumemi-thomas

One of a series of small proposals, in order; the tracking issue lists them. Depends on: none beyond upstream PR solidjs#69 (the test's router example also uses RFCs 15–22).

Summary

On the server, the host passes the request it is serving to the app: createApp({ request }). The app's render then runs inside Solid's request event for that request, so a JS library that looks up "the request being rendered" through Solid sees the real one. @solidjs/router uses this to match the page's URL.

Motivation

Under SSR (server-side rendering: the server streams the page's HTML and the client hydrates it), an app is a request (SV-4): each request gets its own app and its own module state. Upstream, though, the server runtime's stream takes no request. It wraps the render in a request event with a placeholder, new Request("http://127.0.0.1/"), and the runtime README says so ("Not yet: the server build's stream takes no request").

Solid's server libraries locate a render through that event (getRequestEvent() from @solidjs/web). @solidjs/router reads the URL to match from it. With the placeholder, every server render of a routed .rx program is matched against /: a request for /people/2 is rendered as the home page, which is not the page the client then hydrates for that URL.

Proposal

SV-4a (The request). The host gives the app its request, and the render runs in Solid's request event for it.

// the server entry
import { createApp, generateHydrationScript } from "@reactivescript/runtime";   // the server build
import main from "./main.rx";
export { generateHydrationScript };
export function render(request, sink) {
  const app = createApp({ request });            // one app per request (SV-4), for its URL (SV-4a)
  app.stream(main(app), sink);
}
  • request is a standard Request. stream provides { request, locals: {} } as the request event around renderToStream.
  • Without request the behaviour is upstream's: a placeholder request for /.
  • A program's own code does not read the request. What it needs from it, such as a route's params, comes through the library (@solidjs/router here).

Nothing changes on the client: createApp({ root }) and app.mount(main(app), true) as before.

Drawbacks and alternatives

  • It is one more thing a host must remember to pass. Forgetting it is silent: the page renders as /. A warning when a router is present and no request was given is possible but not proposed.
  • locals starts empty per app. A framework that already has its own request event (Start, SolidStart) would rather have the app join that event than make a new one. RFC 31 does that for host-rendered components (it reads getRequestEvent()); for a program the host mounts, this RFC only covers hosts that call stream themselves.
  • Alternative: let the program read the request (a built-in request value). Rejected: it would tie .rx code to the server and give it a second way to read the URL besides the router.

Prototype

Branch claude/magical-bell-pfhb1w of yumemi-thomas/reactivescript: commits 3a72905 (createApp({ request }) in ref/compile/rt-server.ts, README, spec, compiler/test/ssr-router.mjs) and 2cab64b (the same through Vite). Spec rule SV-4a in spec/10-server.md. compiler/test/ssr-router.mjs compiles examples/router for both sides with rxc app, serves each URL with one app per request, hydrates it in Chromium, checks /, /people/1 and /people/2 render their own pages with no console errors, and that a link then navigates on the client in the same document. The router-ssr case of compiler/test/vite.mjs builds client and server with solid({ ssr: true }), renders each request with createApp({ request }), hydrates, and navigates.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions