Skip to content

Commit 7d00f96

Browse files
committed
Added more documentation. Fixes #78
1 parent 964ac66 commit 7d00f96

6 files changed

+97
-12
lines changed

Readme.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
Pre-renders web app into static HTML. Uses headless chrome to crawl all available links starting from the root. Heavily inspired by [prep](https://github.com/graphcool/prep) and [react-snapshot](https://github.com/geelen/react-snapshot), but written from scratch. Uses best practices to get best loading performance.
44

5-
**Does not depend on React**. The name is inspired by `react-snapshot` and because the initial goal was to enable seamless integration with `create-react-app`. Actually, it works with any technology. Considering to change the name.
6-
75
## Features
86

9-
- Enables SEO (google, duckduckgo...) and SMO (twitter, facebook...) for SPA.
10-
- Works out-of-the-box with [create-react-app](https://github.com/facebookincubator/create-react-app) - no code-changes required.
11-
- Uses real browser behind the scene, so no issue with unsupported HTML5 features, like WebGL or Blobs.
12-
- Crawls all pages starting from the root, no need to list pages by hand, like in `prep`.
13-
- With prerendered HTML and inlined critical CSS you will get fast first paint, like with [critical](https://github.com/addyosmani/critical).
7+
- Enables **SEO** (google, duckduckgo...) and **SMO** (twitter, facebook...) for SPA.
8+
- **Works out-of-the-box** with [create-react-app](https://github.com/facebookincubator/create-react-app) - no code-changes required.
9+
- Uses **real browser** behind the scene, so no issue with unsupported HTML5 features, like WebGL or Blobs.
10+
- Does a lot of load **performance optimization**. [Here are details](doc/load-performance-optimizations.md), if you are curious.
11+
- **Does not depend on React**. The name is inspired by `react-snapshot`. Works with any technology.
12+
- npm package does not have compilation step, so **you can fork** it, change what you need and install it with GitHub URL.
1413

15-
Please note: some features are experimental, but prerendering is considered stable enough.
14+
**Zero configuration** is the main feature. You do not need to worry how does it work or how to configure it. But if you are curious [here are details](doc/behind-the-scenes.md).
1615

1716
## Basic usage with create-react-app
1817

@@ -48,7 +47,7 @@ That's it!
4847
## ✨ Examples
4948

5049
- [Load performance optimization](doc/an-almost-static-stack-optimization.md)
51-
- [recipes](Recipes.md)
50+
- [recipes](doc/recipes.md)
5251
- [stereobooster/an-almost-static-stack](https://github.com/stereobooster/an-almost-static-stack)
5352

5453
## ⚙️ Customization

doc/an-almost-static-stack-optimization.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Next problem: Lighthouse complains about render-blocking stylesheets.
116116

117117
## Round 4: Link headers
118118

119-
Next optimisation is pretty trivial and does not require code modification, but it requires the support of custom headers from your hosting. You will need to be able to set `Link` header.
119+
Next optimization is pretty trivial and does not require code modification, but it requires the support of custom headers from your hosting. You will need to be able to set `Link` header.
120120

121121
`react-snap` can generate Link headers in [superstatic](https://github.com/firebase/superstatic) format, like this:
122122

@@ -243,7 +243,7 @@ File sizes after gzip:
243243

244244
## Round 9: vanilla create-react-app
245245

246-
This is for comparison - original create-react-app without any additional optimisations.
246+
This is for comparison - original create-react-app without any additional optimizations.
247247

248248
🔖 [round-9](https://github.com/stereobooster/an-almost-static-stack/compare/round-8...stereobooster:round-9?expand=1)
249249

doc/behind-the-scenes.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Behind the scenes
2+
3+
1. copies index.html as 200.html
4+
2. starts local web server with your application (by default uses `/build` as a root)
5+
3. visits `/` or any other pages listed in `include` configuration
6+
4. find all links on the page with the same domain, add them to queue
7+
5. If there is more than one page in the queue it also adds `/404.html` to the queue
8+
6. renders the page with the help of puppeteer
9+
7. waits till there are no active network requests for more than 0.5 second
10+
8. removes webpack chunks, if needed
11+
9. removes styles with blob URLs, if needed
12+
10. recreates text for style tags for CSS-in-JS solutions, if needed
13+
11. inlines critical CSS, if configured
14+
12. collects assets for http2 push manifest, if configured
15+
13. minifies HTML and saves it to the disk
16+
14. if `route` ends with `.html` it will be used as is, otherwise `route/index.html` is used
17+
18+
## Other features
19+
20+
- `react-snap` works concurrently, by default it uses 4 tabs in the browser. Can be configured with `concurrency` option.

doc/load-performance-optimizations.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Load performance optimizations
2+
3+
## Works out of the box
4+
5+
### Prerendered HTML
6+
7+
`react-snap` prerenders HTML, so the browser can start to render content or download required resources ASAP.
8+
9+
### preconnect for third-party resources
10+
11+
`react-snap` tracks all third-party connections during rendering and will place appropriate preconnect links in the `header`.
12+
13+
### If you are using code splitting feature of Webpack
14+
15+
`react-snap` will remove chunk scripts from the HTML and instead will place preload links in the `header`.
16+
17+
### If you are using CSS-in-JS solution
18+
19+
`react-snap` will prerender all styles and save in the HTML.
20+
21+
## Requires configuration
22+
23+
This is a brief overview of what described in Readme and [recipes](recipes.md).
24+
25+
### inlineCss
26+
27+
With this configuration enabled `react-snap` will inline critical CSS and stylesheet links will be loaded in a nonblocking manner with the help of [loadCss](https://www.npmjs.com/package/fg-loadcss).
28+
29+
### cacheAjaxRequests
30+
31+
If you are doing AJAX requests (to the same domain), `react-snap` can cache this data in the window. Think of it as a poor man's Redux rehydration.
32+
33+
### http2PushManifest
34+
35+
`react-snap` can record all resources (scripts, styles, images) required for the page and write down this data to the JSON file. You can use this JSON file to generate HTTP2 server pushes or Link headers.
36+
37+
### If you are using Redux
38+
39+
Use `window.snapSaveState` callback to store Redux state, so it can be used to rehydrate on the client side.
40+
41+
### If you are using loadable-components
42+
43+
Use `window.snapSaveState` callback to store `loadable-components` state, so it can be used to rehydrate on the client side.
44+
45+
### If you are using Apollo
46+
47+
Use `window.snapSaveState` callback to store `Apollo` state, so it can be used to rehydrate on the client side.
48+
49+
**Caution**: I didn't test this one. If you use it please let me know.
50+
51+
```js
52+
// Grab the state from a global variable injected into the server-generated HTML
53+
const preloadedState = window.__APOLLO_STORE__
54+
55+
// Allow the passed state to be garbage-collected
56+
delete window.__APOLLO_STORE__
57+
58+
const client = new ApolloClient({
59+
initialState: preloadedState,
60+
});
61+
62+
// Tell react-snap how to save state
63+
window.snapSaveState = () => ({
64+
"__APOLLO_STORE__": client.store.getState()
65+
});
66+
```
File renamed without changes.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"typescript": "^2.7.0-dev.20171026"
3535
},
3636
"scripts": {
37-
"toc": "yarn run markdown-toc -i Recipes.md",
37+
"toc": "yarn run markdown-toc -i doc/recipes.md",
3838
"test": "echo \"Error: no test specified\" && exit 1",
3939
"precommit": "prettier --write {*,src/*}.{js,json,css}",
4040
"tsc": "tsc -p ."

0 commit comments

Comments
 (0)