As a frontend project, stereobooster/react-snap has picked up 5.1k stars on GitHub (JavaScript). π» Zero-configuration framework-agnostic static prerendering for SPAs
Snapshot summary built from the project's own GitHub metadata β there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then β see the READ ME tab for the original README in full.
Pre-renders a web app into static HTML. Uses Headless Chrome to crawl all available links starting from the root. Heavily inspired by prep and react-snapshot, but written from scratch. Uses best practices to get the best loading performance.
π Features
Enables SEO (Google, DuckDuckGo...) and SMO (Twitter, Facebook...) for SPAs.
Works out-of-the-box with create-react-app - no code-changes required.
Uses a real browser behind the scenes, so there are no issues with unsupported HTML5 features, like WebGL or Blobs.
Does a lot of load performance optimization. Here are details, if you are curious.
Does not depend on React. The name is inspired by react-snapshot but works with any technology (e.g., Vue).
npm package does not have a compilation step, so you can fork it, change what you need, and install it with a GitHub URL.
Zero configuration is the main feature. You do not need to worry about how it works or how to configure it. But if you are curious, here are details.
source - output folder of webpack or any other bundler of your choice
Read more about minifyHtml caveats in #142.
Example: Switch from prerender-spa-plugin to react-snap
Caveats
Only works with routing strategies using the HTML5 history API. No hash(bang) URLs.
Vue uses the data-server-rendered attribute on the root element to mark SSR generated markup. When this attribute is present, the VDOM rehydrates instead of rendering everything from scratch, which can result in a flash.
window.snapSaveState is a callback to save the state of the application at the end of rendering. It can be used for Redux or async components. In this example, it is repurposed to alter the DOM, this is why I call it a "hack." Maybe in future versions of react-snap, I will come up with better abstractions or automate this process.
Vue 1.x
Make sure to use replace: false for root components
β¨ Examples
Emotion website load performance optimization
Load performance optimization
recipes
stereobooster/an-almost-static-stack
βοΈ Customization
If you need to pass some options for react-snap, you can do this in your package.json like this:
"reactSnap": {
"inlineCss": true
}
Not all options are documented yet, but you can check defaultOptions in index.js.
inlineCss
Experimental feature - requires improvements.
react-snap can inline critical CSS with the help of minimalcss and full CSS will be loaded in a non-blocking manner with the help of loadCss.
Use inlineCss: true to enable this feature.
TODO: as soon as this feature is stable, it should be enabled by default.
β οΈ Caveats
Async components
Also known as code splitting, dynamic import (TC39 proposal), "chunks" (which are loaded on demand), "layers", "rollups", or "fragments". See: Guide To JavaScript Async Components
An async component (in React) is a technique (typically implemented as a higher-order component) for loading components on demand with the dynamic import operator. There are a lot of solutions in this field. Here are some examples:
react.lazy
loadable-components
react-loadable
react-async-component
It is not a problem to render async components with react-snap, the tricky part happens when a prerendered React application boots and async components are not loaded yet, so React draws the "loading" state of a component, and later when the component is loaded, React draws the actual component. As a result, the user sees a flash:
Usually a code splitting library provides an API to handle it during SSR, but as long as "real" SSR is not used in react-snap - the issue surfaces, and there is no simple way to fix it.
Use react-prerendered-component. This library holds onto the prerendered HTML until the dynamically imported code is ready.
import loadable from "@loadable/component";
import { PrerenderedComponent } from "react-prerendered-component";
const prerenderedLoadable = dynamicImport => {
const LoadableComponent = loadable(dynamicImport);
return React.memo(props => (
// you can use the `.preload()` method from react-loadable or react-imported-component`
<PrerenderedComponent live={LoadableComponent.load()}>
<LoadableComponent {...props} />
</PrerenderedComponent>
));
};
const MyComponent = prerenderedLoadable(() => import("./MyComponent"));
MyComponent will use prerendered HTML to prevent the page content from flashing (it will find the required piece of HTML using an id attribute generated by PrerenderedComponent and inject it using dangerouslySetInnerHTML).
The same approach will work with React.lazy, but React.lazy doesn't provide a prefetch method (load or preload), so you need to implement it yourself (this can be a fragile solution).
loadable-components were deprecated in favour of @loadable/component, but @loadable/component dropped getState. So if you want to use loadable-components you can use old version (2.2.3 latest version at the moment of writing) or you can wait until React will implement proper handling of this case with asynchronous rendering and React.lazy.
Redux
See: Redux Server Rendering Section
// Grab the state from a global variable injected into the server-generated HTML
const preloadedState = window.__PRELOADED_STATE__;
// Allow the passed state to be garbage-collected
delete window.__PRELOADED_STATE__;
// Create Redux store with initial state
const store = createStore(counterApp, preloadedState || initialState);
// Tell react-snap how to save Redux state
window.snapSaveState = () => ({
__PRELOADED_STATE__: store.getState()
});
Caution: as of now, only basic "JSON" data types are supported: e.g. Date, Set, Map, and NaNwon't be handled correctly (#54).
Third-party requests: Google Analytics, Mapbox, etc.
You can block all third-party requests with the following config:
"skipThirdPartyRequests": true
AJAX
react-snap can capture all AJAX requests. It will store json requests in the domain in window.snapStore[<path>], where <path> is the path of the request.
Use "cacheAjaxRequests": true to enable this feature.
This feature can conflict with the browser cache. See #197 for details. You may want to disable cache in this case: "puppeteer": { "cache": false }.
Service Workers
By default, create-react-app uses index.html as a fallback:
navigateFallback: publicUrl + '/index.html',
You need to change this to an un-prerendered version of index.html - 200.html, otherwise you will see index.html flash on other pages (if you have any). See Configure sw-precache without ejecting for more information.
Containers and other restricted environments
Puppeteer (Headless Chrome) may fail due to sandboxing issues. To get around this,
you may use:
See this PR. At the moment of writing, Heroku doesn't support HTTP/2.
Semantic UI
Semantic UI is defined over class substrings that contain spaces
(e.g., "three column"). Sorting the class names, therefore, breaks the styling. To get around this,
use the following configuration:
"minifyHtml": { "sortClassName": false }
From version 1.17.0, sortClassName is false by default.
JSS
Once JS on the client is loaded, components initialized and your JSS styles are regenerated, it's a good time to remove server-side generated style tag in order to avoid side-effects
This basically means that JSS doesn't support rehydration. See #99 for a possible solutions.
react-router v3
See #135.
userAgent
You can use navigator.userAgent == "ReactSnap" to do some checks in the app code while snappingβfor example, if you use an absolute path for your API AJAX request. While crawling, however, you should request a specific host.
Does stereobooster/react-snap have a project website?
No homepage URL was recorded for stereobooster/react-snap in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How active is development on stereobooster/react-snap?
The most recent commit recorded on stereobooster/react-snap was 6 months ago, based on the GitHub push timestamp. The repository has 398 forks β one of the better signals of community interest.
How many stars does stereobooster/react-snap have?
stereobooster/react-snap has 5.1k GitHub stars β refresh the page for the live number, or check github.com/stereobooster/react-snap. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is stereobooster/react-snap open source?
Yes β stereobooster/react-snap ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/stereobooster/react-snap.
What else is in the Frontend space?
stereobooster/react-snap is tracked by TopGit under the Frontend category, alongside 8 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What topics is stereobooster/react-snap associated with?
GitHub's repository topics for stereobooster/react-snap: "prerender", "react", "seo", "server-side-rendering", "ssr", "static-site-generator", "vue", "zero-configuration". TopGit's editorial category is Frontend.
Where do I read more about stereobooster/react-snap?
This TopGit page is a snapshot β the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/stereobooster/react-snap is the definitive source.
Read full README in the tab above.
Want a second opinion on react-snap?
Ask an AI that can read this page β one click and you get its take on react-snap.