fastify / fastify/fastify-vite

Revisit React SSR performance optimizations

Open
#364 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.1k
Forks
103
Avg merge
6h 54m
Merged PRs (30d)
7

Description

## Background

There was an experimental branch (`react-performance`) with changes claiming ~20% performance improvement for `@fastify/react` SSR. The branch was incomplete and stale, but the ideas are worth revisiting.

## Optimization Ideas

### 1. Sparse serialization in `toJSON()`
Only include properties that have values, reducing payload size:

```js
// Instead of always including all properties:
toJSON() {
return {
actionData: this.actionData,
state: this.state,
data: this.data,
// ...
}
}

// Only include properties with values:
toJSON() {
const json = {}
if (this.actionData) json.actionData = this.actionData
if (this.state) json.state = this.state
if (this.data) json.data = this.data
// ...
return json
}
```

### 2. Hook consolidation
Move `preHandler` logic into `onRequest` to reduce hook overhead - fewer async boundaries.

### 3. Use `renderToString` for non-streaming SSR
When streaming isn't needed, `renderToString` may be simpler and faster than `renderToPipeableStream`.

### 4. Use native `PassThrough` over `Minipass`
Remove the `minipass` dependency in favor of Node's native `PassThrough` stream.

## Next Steps

- [ ] Benchmark current SSR performance as baseline
- [ ] Test each optimization individually to measure impact
- [ ] Implement optimizations that show measurable improvement

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.