A custom partial React SSR renderer for prefetching and suspense
Installations
npm install react-ssr-prepass
Developer
FormidableLabs
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
18.20.4
NPM Version
10.7.0
Statistics
592 Stars
134 Commits
26 Forks
48 Watching
5 Branches
73 Contributors
Updated on 24 Nov 2024
Bundle Size
10.32 kB
Minified
3.72 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
27,000,447
Last day
-7.3%
15,579
Compared to previous day
Last week
2.1%
88,105
Compared to previous week
Last month
1.7%
380,609
Compared to previous month
Last year
-27.7%
5,844,126
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
1
Dev Dependencies
25
react-ssr-prepass
react-dom/server
does not have support for suspense yet.
react-ssr-prepass
offers suspense on the server-side today, until it does. ✨
react-ssr-prepass
is a partial server-side React renderer that does a prepass
on a React element tree and suspends when it finds thrown promises. It also
accepts a visitor function that can be used to suspend on anything.
You can use it to fetch data before your SSR code calls renderToString
or
renderToNodeStream
.
⚠️ Note: Suspense is unstable and experimental. This library purely exists since
react-dom/server
does not support data fetching or suspense yet. This two-pass approach should just be used until server-side suspense support lands in React.
The Why & How
It's quite common to have some data that needs to be fetched before server-side rendering and often it's inconvenient to specifically call out to random fetch calls to get some data. Instead Suspense offers a practical way to automatically fetch some required data, but is currently only supported in client-side React.
react-ssr-prepass
offers a solution by being a "prepass" function
that walks a React element tree and executing suspense. It finds all
thrown promises (a custom visitor can also be provided) and waits for
those promises to resolve before continuing to walk that particular
suspended subtree. Hence, it attempts to offer a practical way to
use suspense and complex data fetching logic today.
A two-pass React render is already quite common for in other libraries
that do implement data fetching. This has however become quite impractical.
While it was trivial to previously implement a primitive React renderer,
these days a lot more moving parts are involved to make such a renderer
correct and stable. This is why some implementations now simply rely
on calling renderToStaticMarkup
repeatedly.
react-ssr-prepass
on the other hand is a custom implementation
of a React renderer. It attempts to stay true and correct to the
React implementation by:
- Mirroring some of the implementation of
ReactPartialRenderer
- Leaning on React elements' symbols from
react-is
- Providing only the simplest support for suspense
Quick Start Guide
First install react-ssr-prepass
alongside react
and react-dom
:
1yarn add react-ssr-prepass 2# or 3npm install --save react-ssr-prepass
In your SSR code you may now add it in front of your usual renderToString
or renderToNodeStream
code:
1import { createElement } from 'react' 2import { renderToString } from 'react-dom/server' 3 4import ssrPrepass from 'react-ssr-prepass' 5 6const renderApp = async (App) => { 7 const element = createElement(App) 8 await ssrPrepass(element) 9 10 return renderToString(element) 11}
Additionally you can also pass a "visitor function" as your second argument. This function is called for every React class or function element that is encountered.
1ssrPrepass(<App />, (element, instance) => { 2 if (element.type === SomeData) { 3 return fetchData() 4 } else if (instance && instance.fetchData) { 5 return instance.fetchData() 6 } 7})
The first argument of the visitor is the React element. The second is
the instance of a class component or undefined. When you return
a promise from this function react-ssr-prepass
will suspend before
rendering this element.
You should be aware that react-ssr-prepass
does not handle any
data rehydration. In most cases it's fine to collect data from your cache
or store after running ssrPrepass
, turn it into JSON, and send it
down in your HTML result.
Prior Art
This library is (luckily) not a reimplementation from scratch of
React's server-side rendering. Instead it's mostly based on
React's own server-side rendering logic that resides in its
ReactPartialRenderer
.
The approach of doing an initial "data fetching pass" is inspired by:
Maintenance Status
Experimental: This project is quite new. We're not sure what our ongoing maintenance plan for this project will be. Bug reports, feature requests and pull requests are welcome. If you like this project, let us know!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
7 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 11/30 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/FormidableLabs/react-ssr-prepass/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/FormidableLabs/react-ssr-prepass/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/FormidableLabs/react-ssr-prepass/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/FormidableLabs/react-ssr-prepass/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:39: update your workflow using https://app.stepsecurity.io/secureworkflow/FormidableLabs/react-ssr-prepass/release.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: jobLevel 'packages' permission set to 'write': .github/workflows/release.yml:16
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/release.yml:11
- Warn: jobLevel 'deployments' permission set to 'write': .github/workflows/release.yml:15
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Reason
15 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-4x5v-gmq8-25ch
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
3.2
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-ssr-prepass
preact-ssr-prepass
react-ssr-prepass drop-in replacement for preact-x
@plasmicapp/react-ssr-prepass
A custom partial React SSR renderer for prefetching and suspense
next-plugin-preact
Preact plugin for Next.js
@plasmicapp/query
This is a mini isomorphic, _component-level_ data-fetching library. It lets you fetch data client-side or server-side. On the server, it lets you fetch data from any component, Suspense-style.