Giter Site home page Giter Site logo

mgechev / guess-next Goto Github PK

View Code? Open in Web Editor NEW
526.0 10.0 8.0 2.71 MB

๐Ÿ”ฎ Demo application showing the integration of Guess.js with Next.js

Home Page: https://guess-mzwkathwpv.now.sh/

JavaScript 50.11% CSS 49.89%
next-js guess-js predictive-prefetching ml web-performance

guess-next's Introduction

๐Ÿ”ฎ Guess.js + Next.js

Guess.js is a collection of libraries & tools for enabling data-driven user-experience on the web.

In this particular example, we combine Guess.js with Next.js to introduce predictive prefetching of JavaScript bundles. Based on user navigation patterns collected from Google Analytics or other source, Guess.js builds a machine-learning model to predict and prefetch JavaScript that will be required in each subsequent page.

Based on early benchmarks, this can improve the perceived page load performance with 20%.

For more information on Guess.js, take a look at the following links:

Usage

Here's how you can try the demo:

git clone [email protected]:mgechev/guess-next
cd guess-next && npm i
npm run build && npm start

Demo

Integration

Guess.js (0.1.5 and above) works with Next.js with only two points of integration. All you need to do is add the GuessPlugin to next.config.js and introduce a snippet for prefetching the pages which are likely to be visited next.

The following sections describe both points in details.

Webpack Config

All you need is to extend the webpack config of your Next.js application is to add the GuessPlugin to next.config.js file, located in the root of your project. If the file does not exist, create it and add the following content:

const { GuessPlugin } = require('guess-webpack');

module.exports = {
  webpack: function(config, { isServer }) {
    if (isServer) return config;
    config.plugins.push(
      new GuessPlugin({
        GA: 'XXXXXX'
      })
    );
    return config;
  }
};

We set the value of the webpack property of the object literal we set as value to module.exports. We set it to a function which alters the GuessPlugin to the config.plugins array. Notice that we check if Next.js has invoked webpack on the server and we return.

As a value of the GA property, we set a Google Analytics View ID. At build time, Guess.js will open a browser and try to get read-only access to extract a report and use it for the predictive analytics.

Note that Google Analytics is not the only provider you can use to provide the user navigation report that Guess.js uses. In this example application we provide the report from a JSON file.

Prefetch Pages

The final piece of the integration is performing the actual prefetching. In your layout component (see components/layout.js) add:

import { guess } from 'guess-webpack/api';

// ...

  if (typeof window !== 'undefined') {
    Object.keys(guess()).forEach(p => router.prefetch(p));
  }

// ...

Keep in mind that we check if window is "undefined". This is required because we don't want to run Guess.js on the server. When we invoke guess(), we'll return a set of routes where each route will have an associated probability for the user to visit it.

The routes that guess() returns depend on the Google Analytics report that it has extracted, together with the user's effective connection type.

License

MIT

guess-next's People

Contributors

dependabot[bot] avatar mgechev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

guess-next's Issues

Doesn't fetch!

When I open the localhost:3000 in the browser, the next.js is working and I can navigate to different pages, but the problem is that it doesn't prefetch the other pages.

My cache is disabled.

In the /components/layouts.js file, I even added this line below to check if it works statically:

router.prefetch('/index.js')
router.prefetch('/example.js')
router.prefetch('/about.js')
router.prefetch('/media.js')

No luck.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.