https://github.com/aquapi/react-ssr
React SSR
https://github.com/aquapi/react-ssr
Last synced: 17 days ago
JSON representation
React SSR
- Host: GitHub
- URL: https://github.com/aquapi/react-ssr
- Owner: aquapi
- Created: 2022-04-30T15:21:58.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-05-09T12:42:30.000Z (about 4 years ago)
- Last Synced: 2025-01-14T01:34:10.713Z (over 1 year ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# React SSR
A library for SSR using React 18
## Installation
```bash
# Using NPM
npm i react-ss
# Using yarn
yarn add react-ss
```
Make sure you have React and React DOM installed.
## Example usage
```javascript
import ssr from "react-ss";
import express from "express";
// Create the root to render
// Use this with nodemon to rebuild when anything change
await ssr.build();
// Create an express server
const app = express();
// Serve the hydrate file
app.use(express.static(".root"));
// Render the homepage on every request ("./pages/home.js")
app.use(async (req, res) => {
const content = await ssr.renderToHTML("/home");
res.end(content);
});
// Start the server
app.listen(8080);
```
# Documentation
- `build(): Promise`: Generate a `.root` directory. `renderToHTML` can't work before calling this.
- `renderToHTML(): Promise`: Render a page to HTML string. You need to serve the `.root` directory as a static for this to work.