Home

Awesome

📸 React Snapshot

⚠️⚠️⚠️ DEPRECATED: USE https://github.com/stereobooster/react-snap INSTEAD ⚠️⚠️

A zero-configuration static pre-renderer for React apps. Starting by targeting Create React App (because it's great)

The Premise

Server-side rendering is a big feature of React, but for most apps it can be more trouble than its worth. Personally, I think the sweet spot is taking static site snapshots of all your publicly-accessible pages & leaving anything requiring authentication as a normal, JS-driven Single Page App.

This is a project to do that. Automatically, without any configuration, just smart defaults. Retrospective progressive enhancement.

The snapshots still have the normal JS bundle included, so once that downloads the site will function exactly as before (i.e. instantaneous page transitions), but you serve real, functional HTML & CSS as soon as possible. It's good for SEO (yes Google crawls SPAs now but they still reward perf and this perfs like a banshee), it's good if your JS is broken or something render-blocking has a network fail, it's good for accessibility, it's good for Slackbot or Facebook to read your opengraph tags, it's just good.

The How To

- "build": "react-scripts build"
+ "build": "react-scripts build && react-snapshot"
- import ReactDOM from 'react-dom';
+ import { render } from 'react-snapshot';

- ReactDOM.render(
+ render(
    <App/>,
    document.getElementById('root')
  );

This calls ReactDOM.render in development and ReactDOMServer.renderToString when prerendering. If I can make this invisible I will but I can't think how at the moment.

Options

You can specify additional paths as entry points for crawling that would otherwise not be found. It's also possible to exclude particular paths from crawling. Simply add a section called "reactSnapshot" to your package.json.

  "reactSnapshot": {
    "include": [
      "/other-path",
      "/another/nested-path"
    ],
    "exclude": [
      "/signup",
      "/other-path/exclude-me/**"
    ],
    "snapshotDelay": 300
  }

Note that exclude can be passed a glob, but include cannot.

The default snapshot delay is 50ms, and this can be changed to suit your app's requirements.

The Demo

Check out create-react-app-snapshot.surge.sh for a live version or geelen/create-react-app-snapshot for how it was built, starting from create-react-app's awesome baseline. No ejecting necessary, either.

The diff from the original create-react-app code might be enlightening to you as well.

The Implementation

It's pretty simple in principle:

There's a few more steps to it, but not much.

React-snapshot will crawl all links that it finds. You can create "site map" page, which will contain links to all pages.

The Caveats

This is a hacky experiment at the moment. I would really like to see how far we can take this approach so things "just work" without ever adding config. Off the top of my head:

The Alternatives

This should work for simple cases. For less simple cases, go with:

License

MIT