Guess.js Webpack Plugin

0.4.22 · active · verified Tue Apr 21

guess-webpack is a Webpack plugin that integrates Guess.js, an analytics-driven approach to improving web performance. It leverages machine learning models built from user behavior data (e.g., Google Analytics) to predict future navigation paths and prefetch JavaScript bundles accordingly. This optimizes application load times by ensuring resources are available before the user explicitly requests them. The current stable version is 0.4.22, indicating it is pre-1.0 and its API may still evolve. Unlike traditional static analysis bundlers, guess-webpack differentiates itself by dynamically adapting prefetching strategies based on real-world user data, aiming for more intelligent and efficient resource delivery.

Common errors

Warnings

Install

Imports

Quickstart

Configures `GuessPlugin` in `webpack.config.js` to enable analytics-driven bundling and demonstrates the runtime usage of the `guess` function for predictive prefetching based on current or specified routes.

import path from 'path';
import { GuessPlugin } from 'guess-webpack';

// webpack.config.js
// Assumes you have 'webpack' and 'webpack-cli' installed as dev dependencies
// and a 'package.json' initialized.
module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    publicPath: '/' // Important for route recognition
  },
  plugins: [
    new GuessPlugin({
      // Replace 'GOOGLE_ANALYTICS_VIEW_ID' with your actual GA View ID
      // For local development, you might mock this or use test data.
      GA: process.env.GOOGLE_ANALYTICS_VIEW_ID ?? 'UA-XXXXX-Y',
      // Example with JWT for server-side auth (optional)
      // jwt: require('./credentials.json'),
      // Example routeProvider for automatic prefetching (requires 'guess-parser')
      // For many React/Vue apps, custom mapping might be needed.
      // For this quickstart, we omit `routeProvider` to keep it simple, 
      // relying on manual `guess()` calls at runtime.
      runtime: {
        delegate: false // Delegate to framework-specific prefetching if available
      }
    })
  ],
  devServer: {
    static: './dist',
    historyApiFallback: true // Important for SPA routing
  }
};

// src/index.js (simplified application entry point)
import { guess } from 'guess-webpack/api';

console.log('Application started.');

// Simulate a route change or initial load
function simulateNavigation(currentRoute = '/') {
  const predictions = guess(currentRoute);
  console.log(`From ${currentRoute}, likely next pages:`, predictions);
  // In a real app, you would use 'predictions' to prefetch bundles
  // For example, fetch(predictions[0].url);
}

// Call on initial load
simulateNavigation();

// Simulate navigation after a delay
setTimeout(() => {
  simulateNavigation('/foo');
}, 3000);

// This quickstart demonstrates configuring the GuessPlugin in webpack
// and using the runtime `guess` function to get predictions for prefetching.

view raw JSON →