# @badrap/preload

> Data preloading for vue-router, similar to Sapper's preload or Nuxt.js's asyncData

Latest version **1.0.2** (published 2021-01-25) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @badrap/preload
pnpm add @badrap/preload
yarn add @badrap/preload
bun add @badrap/preload
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-01-25 |
| First published | 2018-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 34.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 51 |
| Author | Joachim Viide |
| Maintainers | badrapbot, jviide |
| Keywords | vue, router, routing, preload |

## Links

- npm: https://www.npmjs.com/package/@badrap/preload
- Repository: https://github.com/badrap/preload
- Homepage: https://github.com/badrap/preload#readme
- Issues: https://github.com/badrap/preload/issues
- npm.io page: https://npm.io/package/@badrap/preload

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2021-01-25
- 1.0.1 — 2019-05-11
- 1.0.0 — 2019-05-10
- 0.0.9 — 2019-05-10
- 0.0.8 — 2019-05-10
- 0.0.7 — 2019-03-17
- 0.0.6 — 2018-12-10
- 0.0.5 — 2018-12-07
- 0.0.4 — 2018-12-05
- 0.0.3 — 2018-11-15
- 0.0.2 — 2018-11-15
- 0.0.1 — 2018-11-15

## README

# @badrap/preload [![tests](https://github.com/badrap/preload/workflows/tests/badge.svg)](https://github.com/badrap/preload/actions?query=workflow%3Atests) [![npm](https://img.shields.io/npm/v/@badrap/preload.svg)](https://www.npmjs.com/package/@badrap/preload)

Add a `preload` function to your [vue-router](https://router.vuejs.org/) route components, used for prepopulating data before those routes get rendered. Mostly modeled after Sapper's [`preload`](https://sapper.svelte.technology/guide#preloading), but also similar to Nuxt.js's [`asyncData`](https://nuxtjs.org/guide/async-data) and Next.js's [`getInitialProps`](https://nextjs.org/docs/#fetching-data-and-component-lifecycle).

## Installation

```sh
$ npm i @badrap/preload
```

## Usage

A modified of the following examples is available at [CodeSandbox](https://codesandbox.io/s/zywnmy35x?initialpath=%23%2Ffoo).

### Basic Setup

This module exports a single function. Use this function to decorate your route definitions before passing them to vue-router:

```js
import Vue from "vue";
import VueRouter from "vue-router";
import preload from "@badrap/preload"; // Import preload.
import Foo from "./Foo.vue"; // Import a couple of route components which
import Bar from "./Bar.vue"; // we decorate with preload.

Vue.use(VueRouter);

const routes = preload([
  // Use preload here to decorate the route components...
  { path: "/foo", component: Foo },
  { path: "/bar", component: Bar },
]);

const router = new VueRouter({
  routes, // ...and pass them to vue-router.
});

new Vue({
  router,
  template: "<router-view />",
}).$mount("#app");
```

### Adding Preloading to Components

After this setup dance the route components **Foo** and **Bar** can define a new method `preload` that is used to prepopulate their data whenever their route gets rendered - on initial render as well as route changes.

Let's define **Foo** in **Foo.vue**:

```vue
<template>
  <div>{{ greeting }}, {{ ip }}!</div>
</template>

<script>
import axios from "axios";

export default {
  async preload() {
    const { data } = await axios.get("https://api.ipify.org");
    return { ip: data };
  },
  data() {
    return { greeting: "Hello" };
  },
};
</script>
```

Rendering the route **/foo** would then show a div with the text _"Hello, 127.0.0.1!"_, or whatever your IP address happens to be instead of 127.0.0.1. This demonstrates two things:

- The properties returned by `preload` get combined with the properties returned by `data`.
- `preload` can be asynchronous (it doesn't have to, though).

### Context

The `preload` method gets a context object that contains useful information and helpers:

| Context property | Meaning                                                                                                                                                                                                                  |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `route`          | The [route object](https://router.vuejs.org/api/#the-route-object) for the route that's currently being rendered.                                                                                                        |
| `redirect`       | A function whose return value you can return from `preload` to redirect the router to. Takes a [location descriptor](https://router.vuejs.org/guide/essentials/navigation.html#router-push-location-oncomplete-onabort). |
| `error`          | A function whose return value you can return from `preload` to signal a status error.                                                                                                                                    |

Here's an example that uses all of the above:

```vue
<script>
export default {
  async preload({ route, redirect, error }) {
    const { search } = route.query;
    if (!search) {
      return error(400, "?search= missing");
    }
    return redirect(
      "https://google.com/search?q=" + encodeURIComponent(search)
    );
  },
};
</script>
```

In addition to these properties you can mix in your own when decorating the route components:

```js
const routes = preload(..., {
  context: {
    appName: "My sweet app"
  }
);
```

After this `appName` will be a part of every context object passed to `preload` methods of the decorated route components.

### Hooks

In addition to extra context properties you can pass in two hooks. The `beforePreload` hook is executed before a route change causes `preload` methods to be called. The `afterPreload` hook gets executed when all of the `preload` calls are done.

```js
const routes = preload(..., {
  beforePreload(() => {
    // Start a progress indicator here.
  }),
  afterPreload(err => {
    // Stop and hide the progress indicator here.
  })
);
```

### Error Component

The default components that gets shown whenever `preload` returns a `context.error(...)` value can be replaced:

```js
const routes = preload(..., {
  errorComponent: ErrorComponent
});
```

## License

This library is licensed under the MIT license. See [LICENSE](./LICENSE).

---
_Source: https://npm.io/package/@badrap/preload · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
