Skip to content

perf(playground): render the language list on the server - #6

Merged
0xmhha merged 2 commits into
mainfrom
perf/server-language-list
Oct 29, 2026
Merged

0xmhha merged 2 commits into
mainfrom
perf/server-language-list

Conversation

@0xmhha

@0xmhha 0xmhha commented Oct 29, 2026

Copy link
Copy Markdown
Member

Summary

The playground page now gets the language list and the default language's starter code on the server, so the first HTML already contains the header, the language select and the starter program.

  • Before: the browser hydrated a skeleton and only then called ListLanguages and GetTemplate.
  • After: the browser makes neither call on first load. In a production check it sent only GetSession.
  • Fallback: if the server cannot get the data in time, the client page fetches it exactly as before.

LCP, production build (next build and next start) against the local gateway, median of 7 fresh loads

main this branch change
No throttling 724 ms 580 ms −144 ms (−20%)
Fast 4G (9 Mbit/s, 60 ms RTT) and 4x CPU slowdown 2980 ms 2644 ms −336 ms (−11%)

In both cases the LCP element is the first line of the Monaco editor. Monaco still loads from jsDelivr, which is the largest remaining cost and is the subject of a separate change.

Design

  • Page split.
    • src/app/[locale]/page.tsx is now a server component. It calls loadInitialLanguages() and renders PlaygroundClient.
    • PlaygroundClient.tsx is the previous page, unchanged apart from passing the data to useLanguages(initial).
    • useLanguages(initial) starts ready, with loading false, the default language selected and the starter code set. It sends no request until retry().
  • src/lib/rpc/server.ts (the only new caller of @connectrpc, kept inside lib/rpc):
    • Time budget. One 700 ms budget (AbortSignal.timeout) covers both calls. A failed template still returns the list, and the client then uses its built-in sample.
    • Caching. Results are cached in memory: successes for 60 s, failures for 15 s. Concurrent renders share one in-flight request. Every server request comes from the frontend server's IP, and the gateway's rate limit for non-run RPCs (rl:other:ip:*) is per IP. Calling it on every page view would drain that bucket.
    • No session data. No cookies and no Origin header are sent. The gateway's originGuard accepts requests with neither, and nothing user-specific is fetched.
    • Gateway address. PLAYGROUND_API_INTERNAL_URL (read at run time, documented in the README and .env.example) lets the server reach the gateway by an internal address. The default is NEXT_PUBLIC_API_URL.
    • Fake backend. It is skipped when the development fake backend is on, so the ?fake= modes keep working.
  • Serialization. ListLanguagesResponse crosses to the client as base64 binary protobuf (lib/rpc/initial.ts), because language limits contain 64-bit integers. Invalid or empty data falls back to the client fetch.
  • Layering. pickDefaultLanguage and defaultTemplateId moved to a React-free features/editor/language-defaults.ts so the server page can use them. useLanguages re-exports them.
  • CSP. The nonce model is unchanged. The layout already reads the per-request nonce, so the page was already dynamic, and Next.js puts the nonce on the inline RSC payload scripts. No CSP violations were logged in the production check.

Testing

  • npx vitest run: 263 tests pass. New tests cover:
    • the payload round trip, with 64-bit limits intact;
    • a failed template;
    • the time budget, with a hung ListLanguages rejected after 20 ms;
    • the success cache TTL, the failure cache TTL and request sharing;
    • the choice of base URL;
    • the fake-backend bypass;
    • invalid payloads;
    • useLanguages(initial) starting ready without a request, and its built-in sample fallback.
  • npx playwright test tests/e2e/fake: 15 tests pass.
  • Production check (next start with the local gateway):
    • the HTML contains the language select;
    • the browser makes no ListLanguages or GetTemplate request;
    • no console errors or CSP violations.
  • TTFB. The first request after a server start took 164 ms, which includes the gateway calls and server warm-up. Cached requests take about 8 ms. With the gateway unreachable (connection refused), the cold request also took 168 ms, and the client then showed the usual backend-unavailable state.
  • npm run typecheck: passes.
  • npm run lint: 0 errors and 12 warnings, as on main.
  • npm run build: passes.
  • npm audit --audit-level=low: 0 vulnerabilities.

Not verified

  • I did not run the @stack Playwright suite. The local backend containers were in use by other work.
  • I did not test a gateway that accepts connections but never answers. By design, a cold page then waits up to 700 ms before falling back, and that result is cached for 15 s.
  • The LCP figures come from one machine, against a gateway on localhost. Network distance to a real gateway adds to the cold server request but not to cached ones.

The playground page waited for two browser requests, ListLanguages and
the default language's GetTemplate, before it showed anything but a
skeleton. The page is now a server component that fetches both and
passes them to the client page. The header, the language select and
the starter code are in the first HTML response.

- One 700 ms budget covers both calls. On timeout or error the page
  renders without the data and the browser fetches as before.
- Results are cached in memory: successes for 60 s, failures for 15 s.
  Concurrent renders share one request. Server requests all come from
  the frontend's IP, so calling the gateway on every page view would
  use up that IP's rate-limit bucket.
- No cookies and no Origin header are sent; the gateway accepts such
  requests. PLAYGROUND_API_INTERNAL_URL can point the server at an
  internal gateway address. The default is NEXT_PUBLIC_API_URL.
- The response is passed as base64 protobuf, because language limits
  hold 64-bit integers.
- With the development fake backend the server stays out, so fake data
  is still used.
- The CSP nonce model is unchanged. The page was already rendered per
  request, and Next.js adds the nonce to its own inline scripts.

Production build, local gateway, median of 7 loads: LCP fell from
724 ms to 580 ms unthrottled, and from 2980 ms to 2644 ms with Fast 4G
and 4x CPU throttling. The LCP element is the first editor line in both
cases.
@0xmhha
0xmhha force-pushed the perf/server-language-list branch from 0872696 to b794ca4 Compare October 29, 2026 03:28
@0xmhha
0xmhha merged commit 4dc7309 into main Oct 29, 2026
2 checks passed
@0xmhha
0xmhha deleted the perf/server-language-list branch October 29, 2026 03:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant