Skip to content
On this page

WARNING

This documentation is for the v0 axios and axios-cache-interceptor branch

Click here to go to our latest version

Getting Started

Prerequisites

  • Node.js (>= 12) Lower version requires a custom build with polyfills.

  • Axios (>= 0.21 and < 1) Lower version requires a custom build with polyfills.

Quick Start

The fastest way to get axios with cache set up and running is to install it with npm or yarn

bash
yarn    add     axios@^0.27.2  axios-cache-interceptor@^0.10.7
npm   install   axios@^0.27.2  axios-cache-interceptor@^0.10.7
html
<!-- Development UMD build for ES2017+ (~12.6 KiB) -->
<script
  src="https://cdn.jsdelivr.net/npm/axios-cache-interceptor@0.10.7/dev/index.bundle.js"
  integrity="sha256-oTqUncNoX9DcUWIb5sLS2tscPHKqUPL0yLlOXSSXzSY="
  crossorigin="anonymous"
></script>

<!-- Production UMD build for ES5+ (~14.2 KiB) -->
<script
  src="https://cdn.jsdelivr.net/npm/axios-cache-interceptor@0.10.7/dist/index.bundle.js"
  integrity="sha256-Dc3BSxOZSDmoVoB11lhxkqH8VdBQjxWkHUmmDotiKJ4="
  crossorigin="anonymous"
></script>

After installing, you can import the package and apply the interceptor to your axios instance, as shown below:

ts
import Axios from 'axios';
// const Axios = require('axios');
// const Axios = window.axios;
// import Axios from 'https://cdn.skypack.dev/axios';

import { setupCache } from 'axios-cache-interceptor';
// const { setupCache } = require('axios-cache-interceptor');
// const { setupCache } = window.AxiosCacheInterceptor;
// import { setupCache } from 'https://cdn.skypack.dev/axios-cache-interceptor';

// same object, but with updated typings.
const axios = setupCache(Axios);

const req1 = axios.get('https://api.example.com/');
const req2 = axios.get('https://api.example.com/');

const [res1, res2] = await Promise.all([req1, req2]);

res1.cached; // false
res2.cached; // true

Just the above is sufficient for most use cases. However, you can also customiza each cache behavior by passing a configuration object to the setupCache function. And you can also customize some behaviors each request by using the cache option in the request config.

Support Table

Most of axios v0 breaking changes were about typing issues, so your version may work with one outside of this table. Axios and Axios Cache Interceptor v0 are not compatible with Axios and Axios Cache Interceptor v1

Note: Axios was not defined as a peerDependency for all v0 versions, because it had a non-stable semver version. See #145 (Comment)

AxiosAxios Cache Interceptor
>= v0.27 && < v1.0.0>= v0.10.3
>= v0.26>= v0.8.4
~ v0.25~ v0.8.4
~ v0.24>= v0.5 && <= 0.8.3
~ v0.23~ v0.4
~ v0.22~ v0.3
v0.21<= v0.2

Read More

Some useful links to get you more familiar with the library:

Made with ❤️