Hysen Labs
Open-source project
errorception/snapshotify avatar
errorception

snapshotify

Creates a static HTML snapshot for your `create-react-app` app, to improve initial page load times.

100 stars5 forksJavaScriptLicense varies
01
DEEP OPEN-SOURCE ANALYSIS

snapshotify: fast first load for create-react-app apps

snapshotify renders a create-react-app app with a headless browser and saves a static HTML snapshot, trimming CSS, preloading fonts and scripts, so the first paint stops waiting on JavaScript.

02
DEEP OPEN-SOURCE ANALYSIS

What problem does it solve?

A create-react-app app ships as a pile of JavaScript, and the first paint waits for it. snapshotify attacks that. It generates a static HTML snapshot of the built app, aiming for blazing fast load times and better SEO, at least for crawlers that do not run JS. The README is upfront that the module is a work in progress, with production features still missing, and it takes inspiration from react-snapshot and react-snap.

03
DEEP OPEN-SOURCE ANALYSIS

How does the snapshot get built?

The build runs in a few stages. A temporary express server serves the built app, puppeteer drives a headless browser to render it, and postcss pulls out only the CSS the page actually needs, keeping @font-face rules, pseudo selectors like ::before and :hover, and media queries for on page nodes while dropping the rest. The minimal CSS is minified with CSSO and inlined as a style tag. Fonts for the first render get identified and preloaded, external scripts and stylesheets are deferred, code split chunks are preloaded in parallel, and an inline loader injects the main script after page load, with a fallback for browsers without JS. A CSP meta tag gets hashes added so the inline pieces are allowed. The whole HTML is minified, links are crawled recursively for client side routes, and the files land in the build folder.

04
DEEP OPEN-SOURCE ANALYSIS

What can you configure?

Configuration comes through a JSON file, snapshot.json by default, or whatever you pass with --config. inlineCSS, preloadScripts, and preloadFonts default to true and control the main optimisations. addCSPHashes and cspAlgo manage Content Security Policy, and two flags help debugging: a dry run that writes nothing and just reports, and one that prints console logs from puppeteer.

05
DEEP OPEN-SOURCE ANALYSIS

How do you know you are in a snapshot?

A global window.SNAPSHOT flag is set to true while a snapshot runs, so the app can serve different content if needed. The README notes the module works well with CSS in JS libraries, having tried glamor, and that async components loaded through dynamic import are handled automatically.

06
DEEP OPEN-SOURCE ANALYSIS

What is still missing?

The TODO list names the gaps. Routes that cannot be crawled, like content behind a login form, are a v1 blocker, with a plan to at least load index.html correctly on the first request. Flicker from async components and getting along with ServiceWorkers are still open questions.

07
DEEP OPEN-SOURCE ANALYSIS

Editorial conclusion

snapshotify is an in progress tool that turns a create-react-app build into a fast loading static snapshot by inlining critical CSS and preloading scripts and fonts. Routes behind a login are still unfinished.

08
DEEP OPEN-SOURCE ANALYSIS

Official sources

09
Community notes

Community notes