Skip to content

Commit b89a4be

Browse files
committed
Update docs about prefetching
1 parent 01d3910 commit b89a4be

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ In your `package.json`, add the following:
6666
```
6767

6868
Create a folder called `src` in your project's root. For demo purposes, create
69-
two React components in `src/Home.js` and `src/About.js`
69+
two React components in `./src/Home.js` and `./src/About.js`
7070

7171
```js
72-
// src/Home.js
72+
// ./src/Home.js
7373
import React from 'react';
7474
import { NavLink } from 'react-router-dom';
7575

@@ -89,7 +89,7 @@ export default Home;
8989
```
9090

9191
```js
92-
// src/About.js
92+
// ./src/About.js
9393
import React from 'react';
9494
import { NavLink } from 'react-router-dom';
9595

@@ -108,11 +108,11 @@ class About extends React.Component {
108108
export default About;
109109
```
110110

111-
Now create a file `src/_routes.js` and export an array of React Router 4
111+
Now create a file `./src/_routes.js` and export an array of React Router 4
112112
compatible `<Route component>` _objects_ that export the 2 pages we just made.
113113

114114
```js
115-
// src/_routes.js
115+
// ./src/_routes.js
116116
import Home from './Home';
117117
import About from './About';
118118

@@ -152,9 +152,9 @@ This will be called on both initial server render, and then client mounts.
152152
Results are made available on `this.props`.
153153

154154
```js
155-
// src/About.js
155+
// ./src/About.js
156156
import React from 'react';
157-
import NavLink from 'react-router-dom/NavLink';
157+
import { NavLink } from 'react-router-dom';
158158

159159
class About extends React.Component {
160160
static async getInitialProps({ req, res, match }) {
@@ -186,11 +186,12 @@ the client and the server:
186186
* `res?: Request`: (server-only) An Express.js response object
187187
* `match`: React Router 4's `match` object.
188188
* `history`: React Router 4's `history` object.
189-
* `location`: (client-only) React Router 4's `match` object.
189+
* `location`: (client-only) React Router 4's `location` object.
190190

191191
### Injected Page Props
192192

193193
* Whatever you have returned in `getInitialProps`
194+
* `prefetch: (pathname: string) => void` - Impertively prefetch _and cache_ data for a path. Under the hood this will map through your route tree, call the matching route's `getInitialProps`, store it, and then provide it to your page component. If the user ultimately navigates to that path, the data and component will be ready ahead of time. In the future, there may be more options to control cache behavior in the form of a function or time in milliseconds to keep that data around.
194195
* `refetch: (nextCtx?: any) => void` - Imperatively call `getInitialProps` again
195196

196197
## Routing
@@ -201,7 +202,7 @@ routing. You can use any and all parts of RR4.
201202
### Parameterized Routing
202203

203204
```js
204-
// src/_route.js
205+
// ./src/_route.js
205206
import Home from './Home';
206207
import About from './About';
207208
import Detail from './Detail';
@@ -228,7 +229,7 @@ export default routes;
228229
```
229230

230231
```js
231-
// src/Detail.js
232+
// ./src/Detail.js
232233
import React from 'react';
233234
import NavLink from 'react-router-dom/NavLink';
234235

@@ -277,7 +278,7 @@ the same exact way.
277278
After,js lets you easily define lazy-loaded or code-split routes in your `_routes.js` file. To do this, you'll need to modify the relevant route's `component` definition like so:
278279

279280
```js
280-
// src/_routes.js
281+
// ./src/_routes.js
281282
import React from 'react';
282283
import Home from './Home';
283284
import asyncComponent from '@jaredpalmer/after/asyncComponent';
@@ -307,10 +308,10 @@ While After.js comes with the battery pack included, you can customize and confi
307308

308309
### Custom `<Document>`
309310

310-
After.js works similarly to Next.js with respect to overriding HTML document structure. This comes in handy if you are using a CSS-in-JS library or just want to collect data out of react context before or after render. To do this, create a file in `./src/_document.js` like so:
311+
After.js works similarly to Next.js with respect to overriding HTML document structure. This comes in handy if you are using a CSS-in-JS library or just want to collect data out of react context before or after render. To do this, create a file in `././src/_document.js` like so:
311312

312313
```js
313-
// ./src/_document.js
314+
// ././src/_document.js
314315
import React from 'react';
315316

316317
class Document extends React.Component {
@@ -365,7 +366,7 @@ export default Document;
365366
If you were using something like `styled-components`, and you need to wrap you entire app with some sort of additional provider or function, you can do this with `renderPage()`.
366367

367368
```js
368-
// ./src/_document.js
369+
// ././src/_document.js
369370
import React from 'react';
370371
import { ServerStyleSheet } from 'styled-components'
371372

@@ -476,7 +477,7 @@ after comes with [Create React App's ESLint configuration](https://github.com/fa
476477
* `process.env.HOST`: default is `0.0.0.0`
477478
* `process.env.NODE_ENV`: `'development'` or `'production'`
478479
* `process.env.BUILD_TARGET`: either `'client'` or `'server'`
479-
* `process.env.PUBLIC_PATH`: Only in used in `after build`. You can alter the `webpack.config.output.publicPath` of the client assets (bundle, css, and images). This is useful if you plan to serve your assets from a CDN. Make sure to _include_ a trailing slash (e.g. `PUBLIC_PATH=https://cdn.example.com/`). If you are using React and altering the public path, make sure to also [include the `crossorigin` attribute](https://reactjs.org/docs/installation.html#using-a-cdn) on your `<script>` tag in `src/server.js`.
480+
* `process.env.PUBLIC_PATH`: Only in used in `after build`. You can alter the `webpack.config.output.publicPath` of the client assets (bundle, css, and images). This is useful if you plan to serve your assets from a CDN. Make sure to _include_ a trailing slash (e.g. `PUBLIC_PATH=https://cdn.example.com/`). If you are using React and altering the public path, make sure to also [include the `crossorigin` attribute](https://reactjs.org/docs/installation.html#using-a-cdn) on your `<script>` tag in `./src/server.js`.
480481

481482
You can create your own custom build-time environment variables. They must start
482483
with `AFTER_`. Any other variables except the ones listed above will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

packages/after/README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ In your `package.json`, add the following:
6666
```
6767

6868
Create a folder called `src` in your project's root. For demo purposes, create
69-
two React components in `src/Home.js` and `src/About.js`
69+
two React components in `./src/Home.js` and `./src/About.js`
7070

7171
```js
72-
// src/Home.js
72+
// ./src/Home.js
7373
import React from 'react';
7474
import { NavLink } from 'react-router-dom';
7575

@@ -89,7 +89,7 @@ export default Home;
8989
```
9090

9191
```js
92-
// src/About.js
92+
// ./src/About.js
9393
import React from 'react';
9494
import { NavLink } from 'react-router-dom';
9595

@@ -108,11 +108,11 @@ class About extends React.Component {
108108
export default About;
109109
```
110110

111-
Now create a file `src/_routes.js` and export an array of React Router 4
111+
Now create a file `./src/_routes.js` and export an array of React Router 4
112112
compatible `<Route component>` _objects_ that export the 2 pages we just made.
113113

114114
```js
115-
// src/_routes.js
115+
// ./src/_routes.js
116116
import Home from './Home';
117117
import About from './About';
118118

@@ -152,9 +152,9 @@ This will be called on both initial server render, and then client mounts.
152152
Results are made available on `this.props`.
153153

154154
```js
155-
// src/About.js
155+
// ./src/About.js
156156
import React from 'react';
157-
import NavLink from 'react-router-dom/NavLink';
157+
import { NavLink } from 'react-router-dom';
158158

159159
class About extends React.Component {
160160
static async getInitialProps({ req, res, match }) {
@@ -186,11 +186,12 @@ the client and the server:
186186
* `res?: Request`: (server-only) An Express.js response object
187187
* `match`: React Router 4's `match` object.
188188
* `history`: React Router 4's `history` object.
189-
* `location`: (client-only) React Router 4's `match` object.
189+
* `location`: (client-only) React Router 4's `location` object.
190190

191191
### Injected Page Props
192192

193193
* Whatever you have returned in `getInitialProps`
194+
* `prefetch: (pathname: string) => void` - Impertively prefetch _and cache_ data for a path. Under the hood this will map through your route tree, call the matching route's `getInitialProps`, store it, and then provide it to your page component. If the user ultimately navigates to that path, the data and component will be ready ahead of time. In the future, there may be more options to control cache behavior in the form of a function or time in milliseconds to keep that data around.
194195
* `refetch: (nextCtx?: any) => void` - Imperatively call `getInitialProps` again
195196

196197
## Routing
@@ -201,7 +202,7 @@ routing. You can use any and all parts of RR4.
201202
### Parameterized Routing
202203

203204
```js
204-
// src/_route.js
205+
// ./src/_route.js
205206
import Home from './Home';
206207
import About from './About';
207208
import Detail from './Detail';
@@ -228,7 +229,7 @@ export default routes;
228229
```
229230

230231
```js
231-
// src/Detail.js
232+
// ./src/Detail.js
232233
import React from 'react';
233234
import NavLink from 'react-router-dom/NavLink';
234235

@@ -277,7 +278,7 @@ the same exact way.
277278
After,js lets you easily define lazy-loaded or code-split routes in your `_routes.js` file. To do this, you'll need to modify the relevant route's `component` definition like so:
278279

279280
```js
280-
// src/_routes.js
281+
// ./src/_routes.js
281282
import React from 'react';
282283
import Home from './Home';
283284
import asyncComponent from '@jaredpalmer/after/asyncComponent';
@@ -307,10 +308,10 @@ While After.js comes with the battery pack included, you can customize and confi
307308

308309
### Custom `<Document>`
309310

310-
After.js works similarly to Next.js with respect to overriding HTML document structure. This comes in handy if you are using a CSS-in-JS library or just want to collect data out of react context before or after render. To do this, create a file in `./src/_document.js` like so:
311+
After.js works similarly to Next.js with respect to overriding HTML document structure. This comes in handy if you are using a CSS-in-JS library or just want to collect data out of react context before or after render. To do this, create a file in `././src/_document.js` like so:
311312

312313
```js
313-
// ./src/_document.js
314+
// ././src/_document.js
314315
import React from 'react';
315316

316317
class Document extends React.Component {
@@ -365,7 +366,7 @@ export default Document;
365366
If you were using something like `styled-components`, and you need to wrap you entire app with some sort of additional provider or function, you can do this with `renderPage()`.
366367

367368
```js
368-
// ./src/_document.js
369+
// ././src/_document.js
369370
import React from 'react';
370371
import { ServerStyleSheet } from 'styled-components'
371372

@@ -476,7 +477,7 @@ after comes with [Create React App's ESLint configuration](https://github.com/fa
476477
* `process.env.HOST`: default is `0.0.0.0`
477478
* `process.env.NODE_ENV`: `'development'` or `'production'`
478479
* `process.env.BUILD_TARGET`: either `'client'` or `'server'`
479-
* `process.env.PUBLIC_PATH`: Only in used in `after build`. You can alter the `webpack.config.output.publicPath` of the client assets (bundle, css, and images). This is useful if you plan to serve your assets from a CDN. Make sure to _include_ a trailing slash (e.g. `PUBLIC_PATH=https://cdn.example.com/`). If you are using React and altering the public path, make sure to also [include the `crossorigin` attribute](https://reactjs.org/docs/installation.html#using-a-cdn) on your `<script>` tag in `src/server.js`.
480+
* `process.env.PUBLIC_PATH`: Only in used in `after build`. You can alter the `webpack.config.output.publicPath` of the client assets (bundle, css, and images). This is useful if you plan to serve your assets from a CDN. Make sure to _include_ a trailing slash (e.g. `PUBLIC_PATH=https://cdn.example.com/`). If you are using React and altering the public path, make sure to also [include the `crossorigin` attribute](https://reactjs.org/docs/installation.html#using-a-cdn) on your `<script>` tag in `./src/server.js`.
480481

481482
You can create your own custom build-time environment variables. They must start
482483
with `AFTER_`. Any other variables except the ones listed above will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. Changing any environment variables will require you to restart the development server if it is running.

0 commit comments

Comments
 (0)