Guess.js Documentation
repository·master·Indexed 27 days ago
https://github.com/guess-js/guessA collection of libraries and tools for enabling data-driven user experiences through predictive fetching and bundling based on user navigation patterns. It includes guess-ga for Google Analytics data retrieval, guess-parser for extracting application routes, and a GuessPlugin for Webpack to automate bundle prefetching. It also provides a guess-static-sites experiment for non-Webpack sites to implement predictive loading workflows.
What's inside Guess.js
- Guess.js is a collection of libraries and tools designed to implement predictive, data-analytics-driven approaches to web performance. It aims to reduce the friction of applying machine learning or analytics (like Google Analytics) to modern web applications to improve user experience through techniques like predictive prefetching and bundling.
Quickstart for non-Webpack users
masterFor sites not using Webpack, you can implement a data-driven loading workflow using the
guess-static-sitesexperiment.This workflow uses Google Analytics data to determine the page a user is most likely to visit next. The process involves:
- Using Google Analytics data to identify navigation patterns.
- Adding a client-side script to your application.
- The script requests the next likely URL from a server and prefetches that resource.
Quickstart for Webpack users
masterTo enable data-driven bundling in a Webpack-based project, install and configure the
GuessPlugin. This plugin automates the setup process for predictive fetching by consuming thegaandparsermodules.If you prefer to use the modules individually instead of the plugin, you can use:
ga: Fetches structured data from the Google Analytics API to identify user navigation patterns.parser: Provides JavaScript framework parsing, which powers the route-parsing capabilities used by the webpack plugin.webpack: The plugin itself that manages predictive fetching configuration.
Configure Automatic Prefetching
masterYou can automate bundle prefetching by combining
guess-webpackwithguess-parser. This creates a mapping between paths and lazy-loaded JavaScript bundles at build time. At runtime, Guess.js will automatically pick top paths and prefetch their bundles.Security Note: When using a JWT token for Google authentication, ensure your
credentials.jsonis added to.gitignore.import { parseRoutes } from 'guess-parser'; const credentials = require('./credentials.json'); // Inside webpack config new GuessPlugin({ jwt: credentials, GA: 'XXXXXX', routeProvider() { return parseRoutes('.'); }, runtime: { delegate: false } });Integrate predictiveFetching.js into your application
masterTo enable client-side predictive fetching, addpredictiveFetching.jsto your pages. You must update the script to point to your running server endpoint.Build all packages
masterTo build all packages in the monorepo, run the build command. The packages are built in topological order to ensure dependencies are built before the packages that rely on them.
npm run buildReact route parsing requirements
masterBecause React lacks a standard route definition syntax,
guess-parseronly supports applications following specific conventions:- Syntax: Only JSX syntax is supported.
- Router: Only
react-router-like syntax is supported. - Route Paths: The
pathattribute of the<Route/>element must be a string literal. - Lazy Loading: Lazy-loaded components must use a dynamic import with a specific AST structure:
- A
CallExpression(e.g.,AsyncComponent) with a single argument. - The argument must be an
ArrowFunction. - The arrow function must have an expression body (e.g., a
CallExpression). - The
CallExpressionmust receive aStringLiteralpointing to the lazy-loaded module.
- A
<Router history={history}> <div className="App"> <Link to="/intro">Intro</Link> <Link to="/main">Main</Link> <div> <Switch> <Redirect exact={true} from="/" to="/intro" /> <Route path="/intro" component={AsyncComponent(() => import('./intro/Intro'))} /> <Route path="/main" component={Main} /> </Switch> </div> </div> </Router>Run the guess-static-sites server
masterStart the prediction server by running the
server.jsfile with Node.js.$ node server.jsRisks and Best Practices in Predictive Prefetching
masterWhen implementing predictive prefetching, consider the following risks:
- Data Consumption: Be mindful of users on restricted data plans. Respect the
Save-DataHTTP header. - Undesirable Pages: Avoid prefetching pages that trigger immediate actions (e.g., logout pages, one-click purchase buttons). Use a blacklist of URLs to prevent this.
- Web Standards: Be aware of the transition from
<link rel=prerender>toNoStatePrefetch. Whilerel=prefetchfetches a single resource,NoStatePrefetchruns the preload scanner on the resource to discover and prefetch subresources.
- Data Consumption: Be mindful of users on restricted data plans. Respect the
Install and configure GuessPlugin for Webpack
masterTo use Guess.js with Webpack, install the
guess-webpackpackage and add theGuessPluginto your Webpack configuration. The plugin requires a Google Analytics View ID to extract transition reports.Note: The
guess-gaplugin will extract reports from Google Analytics for the last year by default.Automate prediction updates with cron jobs
masterIt is recommended to set up a cron job to periodically run
generatePredictions.js. This ensures prefetch links remain accurate as your site content and traffic patterns change.Note on Data Range: By default, the system uses the last 30 days of traffic. You can modify this value in
src/queryParams.js. High-traffic sites may benefit from a shorter range (1-7 days), while low-traffic sites should stick to a longer range (30 days).Configure Google Analytics and Service Account credentials
masterTo allow Guess.js to consume your Google Analytics data, follow these steps:
- Create a Service Account: In the Google APIs console, create a new Service Account with the role
Service Account Userand selectP12 keyas the key type. - Generate PEM certificate: Move the downloaded
.p12file to the project directory and run:openssl pkcs12 -in *.p12 -out key.pem -nodes -clcerts - Add Service Account to Google Analytics: In Google Analytics (Admin > User Management), add the service account email address with
Read & Analyzepermissions. - Enable API: Enable the Google Analytics Reporting API.
- Create a Service Account: In the Google APIs console, create a new Service Account with the role