Improved JSX transform
for Server-Side Rendering
#57468
Labels
Out of Scope
This idea sits outside of the TypeScript language design constraints
Suggestion
An idea for TypeScript
π Search Terms
jsx precompile jsxAttr jsxEscape jsxTemplate Fastest Fast(est) JSX transform preact react jsxs jsxDEV typescript node deno runtime custom configuration ts
β Viability Checklist
β Suggestion
I'd like to propose the implementation of a new JSX transform optimized specifically for server-side rendering (SSR).
This proposal draws inspiration from a recent release in the Deno runtime, which introduced a highly efficient JSX transform for SSR purposes.
Traditionally, JSX has relied heavily on the Garbage Collector (GC) due to its origins as a templating engine primarily designed for client-side rendering (CSR). However, for SSR scenarios where performance and memory efficiency are critical, this reliance on the GC can lead to suboptimal performance and increased overhead.
The proposed JSX transform works by serializing the HTML parts of a JSX template into static string arrays at compile time. This approach significantly reduces the number of short-lived object allocations and minimizes the workload on the GC during runtime.
Expected benefits as observed in the official deno blog post:
Library adoption:
Currently, only Preact has integrated support for this optimized JSX transformation, as evidenced by its recent pull request (and Flesh which doesn't count as it uses preact under the hood).
Given TypeScript's potential adoption of this feature, widespread support across various libraries is foreseeable, including popular frameworks like React, all of which stand to gain significant performance benefits.
Moreover, extending this optimization to other JSX runtimes could yield substantial improvements across the ecosystem.
As this feature is implemented, it could be highlighted as a major improvement in future releases, potentially under titles like: "New Typescript JSX for server-side rendering -> 20x Performance Boost." and other things the marketing team needs and likes to hear.
π Motivating Example
Consider the following input:
Current output
With the new
"jsx": "precompile"
(output copied from deno bundle for this repository)
π» Use Cases
Server-side rendering (SSR) applications: Improve performance and reduce garbage collection overhead in SSR applications with TypeScript and Node.js.
Large-scale applications: Enhance performance for projects handling extensive JSX content, like complex UIs or dynamic content generation.
Microservices architecture: Boost overall system performance by optimizing JSX rendering across multiple SSR services.
Real-time applications: Achieve smoother user experiences in applications requiring frequent updates or real-time data rendering.
Custom JSX runtimes: Allow developers to integrate with custom JSX runtimes for specific project needs, enhancing flexibility and modularity.
Good reads:
Original Deno blog post:
https://deno.com/blog/v1.38#fastest-jsx-transform
A
precompile
repository usingDeno
see how it works:https://github.com/marvinhagemeister/deno-jsx-precompile-preact-demo
Bun's feature request to the same feature:
Improve JSX transform using tsconfig jsx precompile for server side renderingΒ oven-sh/bun#6878
React RFCs about
createElement
stuff:https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
https://github.com/reactjs/rfcs/blob/createlement-rfc/text/0000-create-element-changes.md#motivation
Preact implementation:
feat: support precompiled JSX transformΒ preactjs/preact#4177
feat: add support for precompiled JSXΒ preactjs/preact-render-to-string#322
The text was updated successfully, but these errors were encountered: