UNPKG

6.01 kBMarkdownView Raw
1# guess-webpack
2
3This package exports the `GuessPlugin`
4
5## Quick start (webpack)
6
7Install `GuessPlugin` - our webpack plugin:
8
9```js
10npm i guess-webpack --save-dev
11```
12
13Import `GuessPlugin` to your webpack config:
14
15```js
16const { GuessPlugin } = require('guess-webpack');
17```
18
19Add this to the end of your webpack config:
20
21```js
22new GuessPlugin({ GA: 'GOOGLE_ANALYTICS_VIEW_ID' });
23```
24
25## Usage
26
27```bash
28npm i guess-webpack --save-dev
29```
30
31This section introduces the configuration properties of the `GuessPlugin`
32
33## Basic Usage
34
35Import the `GuessPlugin`:
36
37```ts
38const { GuessPlugin } = require('guess-webpack');
39```
40
41Add the following snippet as last line in your webpack config file:
42
43```ts
44new GuessPlugin({ GA: 'GA_VIEW_ID' });
45```
46
47Where `GA_VIEW_ID` is the [Google Analytics view ID](https://ga-dev-tools.appspot.com/query-explorer/). The `guess-ga` plugin will extract report from Google Analytics for the last year. For custom period look at the section below.
48
49Finally, in your application use Guess.js as follows:
50
51```ts
52import { guess } from 'guess-webpack/api';
53
54guess();
55/**
56 {
57 '/foo': 0.1,
58 '/bar': 0.3,
59 '/baz': 0.6
60 }
61 */
62```
63
64In the snippet above, we first import `guess` from `guess-webpack/api`. After that we invoke the `guess` function and it returns an object with keys pages in our application and values probabilities the user to visit the corresponding page.
65
66This way, you can prefetch content associated with the pages which are likely to be visited on the next user navigation. There are plugins for popular frameworks which are going to do this for you! For examples look at the [demo section](#demos).
67
68For further information on how to use `guess`, look at the "Advanced Usage" section below.
69
70## Advanced Usage
71
72The `guess` function allows you to specify a few optional parameters:
73
74```ts
75import { guess } from 'guess-webpack/api';
76
77guess('/current/route', ['/link-1', '/link-2', '/unavailable']);
78```
79
80The first argument that we've passed to the function invocation above is a path. `guess` will return an object which contains the paths which are likely to be visited next from the path that we've specified. If we omit the first argument, `guess` will use `location.pathname`.
81
82The second argument of `guess` is a whitelist of paths. The returned object from `guess` will contain only keys which are listed in this array.
83
84If you skip the second argument of `guess` you'll receive an exhaustive list of routes which could be visited next.
85
86### Automatic Prefetching
87
88In case you're interested in automating the process of prefetching of bundles in your application, you can use the `guess-webpack` package together with `guess-parser`:
89
90```ts
91import { parseRoutes } from 'guess-parser';
92
93GuessPlugin({
94 GA: 'XXXXXX',
95 routeProvider() {
96 return parseRoutes('.');
97 },
98 runtime: {
99 delegate: false
100 }
101});
102```
103
104At build time, the snippet above will first create mapping between paths and lazy-loaded JavaScript bundles. At runtime, while the user is navigating in the application a small runtime will invoke `guess` to make predictions for the pages which are likely to be visited next. At each step, Guess.js will pick the top paths and prefetch their corresponding bundles.
105
106Keep in mind that `parseRoutes` might not be able to properly create the mapping between the routes and the bundles in applications with very dynamic route definition, for example most React and Vue applications are not supported. For further information on `guess-parser` look at the [package's documentation](https://github.com/guess-js/guess/tree/master/packages/guess-parser).
107
108### Custom Route Provider
109
110In case Guess.js cannot manage to parse the routes of your application and create mapping to the corresponding lazy-loaded bundles, you can provide a custom route provider. It should have the following type:
111
112```ts
113export type RouteProvider = () => Promise<RoutingModule[]>;
114```
115
116Where `RoutingModule` has the following interface:
117
118```ts
119export interface RoutingModule {
120 path: string;
121 modulePath: string;
122 parentModulePath: string | null;
123 lazy: boolean;
124}
125```
126
127### Custom report provider
128
129The `reportProvider` configuration property of `GuessPlugin` is a function which returns promise that resolves to the following data structure:
130
131```ts
132export interface PageTransitions {
133 [key: string]: number;
134}
135
136export interface Report {
137 [key: string]: PageTransitions;
138}
139```
140
141Here's an example:
142
143```ts
144{
145 "foo": {
146 "bar": 5,
147 "baz" 2
148 },
149 "bar": {
150 "baz": 3
151 }
152}
153```
154
155The meaning of the report above is:
156
157- There are two reported transitions from page `foo`:
158 - Transition to page `bar` which has occurred 5 times.
159 - Transition to page `baz` which has occurred 2 times.
160- There's one reported transition from page `bar`:
161 - Transition to `baz` which has occurred 3 times.
162
163## Demos
164
165A number of sample projects using `GuessPlugin` are available. These include:
166
167- [Gatsby Guess Wikipedia](https://github.com/guess-js/gatsby-guess) - a Wikipedia client built using Gatsby.js (the React static-site framework) and Guess.js. This is the closest example we have of a real-world demo application built using the project.
168- [`guess-js-react-demo`](https://github.com/mgechev/guess-js-react-demo) - a simple demo application using `GuessPlugin` and `create-react-app`
169- [`guess-js-angular-demo`](https://github.com/mgechev/guess-js-angular-demo) - a simple demo application using `GuessPlugin` and Angular CLI
170- [`guess-next`](https://github.com/mgechev/guess-next) - a sample application showing the integration between Next.js and Guess.js
171- [`guess-nuxt`](https://github.com/daliborgogic/guess-nuxt) - a sample application showing the integration between Nuxt.js and Guess.js
172
173**Note:** Predictive fetching relies heavily on the availability of data in a Google Analytics account to drive predictions. You may need to seed some data for this by navigating around your demo project to provide Guess with some early data to guide what to prefetch.
174
175## License
176
177MIT