# nuxt-cache-payload

> Cache payload on Static Site Generate by Nuxt.js

Latest version **0.1.2** (published 2019-07-14) · MIT license · 0 weekly downloads

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

## Install

```sh
npm install nuxt-cache-payload
pnpm add nuxt-cache-payload
yarn add nuxt-cache-payload
bun add nuxt-cache-payload
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2019-07-14 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 8.5 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kazumi Inada |
| Maintainers | nandenjin |

## Links

- npm: https://www.npmjs.com/package/nuxt-cache-payload
- Repository: https://github.com/nandenjin/nuxt-cache-payload
- npm.io page: https://npm.io/package/nuxt-cache-payload

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.19.0

## Recent versions

- 0.1.2 (latest) — 2019-07-14
- 0.1.1 — 2019-02-24
- 0.1.0 — 2019-02-24

## README

# 🗄 nuxt-cache-payload

[![npm version](https://badge.fury.io/js/nuxt-cache-payload.svg)](https://badge.fury.io/js/nuxt-cache-payload)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
[![CircleCI](https://circleci.com/gh/nandenjin/nuxt-cache-payload.svg?style=shield)](https://circleci.com/gh/nandenjin/nuxt-cache-payload)
[![Maintainability](https://api.codeclimate.com/v1/badges/9ecd9efd584ef69a51cc/maintainability)](https://codeclimate.com/github/nandenjin/nuxt-cache-payload/maintainability)
[![Dependencies](https://david-dm.org/nandenjin/nuxt-cache-payload.svg)](https://david-dm.org/nandenjin/nuxt-cache-payload)
[![devDependencies](https://david-dm.org/nandenjin/nuxt-cache-payload/dev-status.svg)](https://david-dm.org/nandenjin/nuxt-cache-payload?type=dev)

## What's this
This is a module to generate cache file for `nuxt generate`ed sites.

By default, Nuxt provides payload object from `config.generate.routes()` only on first one page of the session. Other pages that are navigated programmably such as by `<nuxt-link>` cannot get payload objects.

```js
// nuxt.config.js
module.exports = {
  generate: {
    routes() {
      return [
        {
          route: '/dynamic/routed/page',
          payload: await fetchPagePayload(), // Get something from network resource, etc.
        }
      ];
    }
  }
};

// /pages/dynamic/routed/_page.vue
exports default {
  async asyncData( { payload } ) {
    if( payload ) return { payload }; // For as first one page (production) this one is used.
    else return { payload: async fetchPagePayload() }; // For other pages...
  }
};
```
If `fetchPagePayload()` contains heavy process (Ex: with long latency), it delays page loading.

This modules generate cache files of payload object from `config.generate()` and make it to be able to **reduce payload generate requests.**

## Usage
Install with `npm install nuxt-cache-payload` and [register in `nuxt.config.js`](https://nuxtjs.org/api/configuration-modules).

```js
// /pages/dynamic/routed/_page.vue
exports default {
  async asyncData( { getPayload, payload, route } ) {
    return {
      payload:    payload                         // For as the first one page
               || await getPayload( route.path )  // For as the 2nd (or later) page: fetch cached payload
               || await fetchPagePayload()        // Fallback & for development mode
    };
  }
};
```

Module generate cache files with `nuxt generate` and it can be fetched by using `context.getPayload()` inside `asyncData()`.

**Note:** Cache files will be generated only with `nuxt generate` and `context.getPayload()` will returns `null` when called in non-static environment. You should fetch payload data manually from original source in devMode or at server-side rendering.

## Options
```js
// nuxt.config.js
module.exports = {
  modules: [
    [ 'nuxt-cache-payload', {

      // Custom file for cache files (optional).
      // Default: '_payload.${randomHash}.json'
      filename: 'cachefile.json',

      // Encoding of cache files (optional).
      // Default: 'utf8'
      encoding: 'utf8',

    } ];
  ]
};
```

## Contributing
Pull requests or feature requests are welcome. Please submit them from GitHub.

## License
MIT License: Copyright (c) Kazumi Inada

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