Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/styfle/react-server-example-tsx
⚛️ Boilerplate for isomorphic web app with React server-side rendering in TypeScript
https://github.com/styfle/react-server-example-tsx
boilerplate isomorphic jsx react react-server server-side-rendering tsx typescript
Last synced: 20 days ago
JSON representation
⚛️ Boilerplate for isomorphic web app with React server-side rendering in TypeScript
- Host: GitHub
- URL: https://github.com/styfle/react-server-example-tsx
- Owner: styfle
- License: mit
- Created: 2016-06-11T03:21:47.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2021-07-01T20:33:11.000Z (over 3 years ago)
- Last Synced: 2024-10-12T23:40:20.037Z (23 days ago)
- Topics: boilerplate, isomorphic, jsx, react, react-server, server-side-rendering, tsx, typescript
- Language: TypeScript
- Homepage: https://react-tsx.vercel.app/
- Size: 2.37 MB
- Stars: 277
- Watchers: 4
- Forks: 39
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# react-server-example-tsx
[![npm](https://badgen.net/npm/v/react-server-example-tsx)](https://www.npmjs.com/package/react-server-example-tsx)
[![Downloads](https://badgen.net/npm/dt/react-server-example-tsx)](https://www.npmjs.com/package/react-server-example-tsx)
[![Dependency Status](https://badgen.net/david/dep/styfle/react-server-example-tsx)](https://david-dm.org/styfle/react-server-example-tsx)
[![devDependency Status](https://badgen.net/david/dev/styfle/react-server-example-tsx)](https://david-dm.org/styfle/react-server-example-tsx?type=dev)
[![LGTM Quality](https://badgen.net/lgtm/grade/javascript/g/styfle/react-server-example-tsx)](https://lgtm.com/projects/g/styfle/react-server-example-tsx/)
[![Build Status](https://badgen.net/travis/styfle/react-server-example-tsx)](https://travis-ci.org/styfle/react-server-example-tsx)A complex example of how to do server-side rendering with
[React](http://facebook.github.io/react/) and [TypeScript](https://www.typescriptlang.org/) so that component code can be shared between server and browser (also known as isomorphic javascript).Server-Side Rendering (SSR) leads to fast initial page loads, search-engine-friendly pages, and of course...its all type safe!
## Getting Started
Clone the repo, change directory, install dependencies, build the code, and run it!
```sh
git clone https://github.com/styfle/react-server-example-tsx.git
cd react-server-example-tsx
npm install
npm run vercel-build
npm run test
npm start
```Then navigate to http://localhost:3007 and click on the buttons to see some reactive events in action.
## Preventing XSS
The original code from `mhart` attempts to [sanitize the props](https://github.com/mhart/react-server-example/blob/feada6183fe2fbb1a746492e157febe49eeafdcd/server.js#L106) by escaping and then inserting into a `` tag.
I avoided this by performing *1 extra http request* to fetch the props as json before initializing React in the browser. This means that click handlers will not be initialized until the data is returned and React can pick up where it left off after the server-side render and attach the click even handlers. This is the purpose of `ReactDOM.hydrate`.
See [browser.tsx](https://github.com/styfle/react-server-example-tsx/blob/main/src/browser.tsx) for the client-side code.
See [server.ts](https://github.com/styfle/react-server-example-tsx/blob/main/src/server.ts) for the server-side code.
## Prior art
Based on prior work found at [mhart/react-server-example](https://github.com/mhart/react-server-example). It's very good so you should check it out :)
Additionally, I use a combination between [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/react-&-webpack.html#create-a-webpack-configuration-file) and [awesome-typescript-loader](https://github.com/s-panferov/awesome-typescript-loader#configuration) when I switched from browserify v1.4.x to webpack v2.0.x (now webpack v4.x).
## Author
Developed by [styfle](https://styfle.dev), the author of [Package Phobia](https://github.com/styfle/packagephobia)