# cache-holepuncher

> A toolkit for pulling dynamic snippets in fully cached sites, aka Cache Hole Punching

Latest version **0.6.2** (published 2018-12-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install cache-holepuncher
pnpm add cache-holepuncher
yarn add cache-holepuncher
bun add cache-holepuncher
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2018-12-30 |
| First published | 2018-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Oliver Stark |
| Maintainers | ostark |

## Links

- npm: https://www.npmjs.com/package/cache-holepuncher
- Repository: https://github.com/ostark/cache-holepuncher
- npm.io page: https://npm.io/package/cache-holepuncher

## Recent versions

- 0.6.2 (latest) — 2018-12-30
- 0.6.1 — 2018-12-30
- 0.6.0 — 2018-12-30
- 0.5.0 — 2018-12-14
- 0.4.0 — 2018-12-11
- 0.3.0 — 2018-12-11
- 0.2.0 — 2018-12-11
- 0.1.0 — 2018-12-11

## README

# cache-holepuncher.js

A lightweight dependency-free library for cache hole punching. 

Pull dynamic HMTL snippets in fully cached sites with a single line of code. By default the response is cached browser to avoid unnecessary requests. 


## Install

```bash
npm install cache-holepuncher
```

or

```bash
yarn add cache-holepuncher
```

If you are constained to a non-module environment (e.g. no bundlers such as webpack) - don’t fret. 
Simply pull the latest version of `cache-holepuncher` from `jsdelivr`.

```html
<!-- ES6 module -->
<script type="module" src="https://cdn.jsdelivr.net/npm/cache-holepuncher/dist/holepuncher.min.js"></script>

<!-- legacy fallback -->
<script nomodule src="https://cdn.jsdelivr.net/npm/cache-holepuncher/dist/holepuncher.legacy.min.js"></script>
```



## Basic usage

Once loaded you 

### Get single
```html
<div id="single_placeholder_id">
    empty state
</div>

<script>
    punch_one("/some/endpoint", "single_placeholder_id");
</script>
```

### Get multiple
```html
<ul>
    <li class="placeholder" data-callback-url="/some/endpoint/foo">empty state</li>
    <li class="placeholder" data-callback-url="/some/endpoint/bar">empty state</li>
</ul>

<script>
    punch_all(".placeholder", "data-callback-url");
</script>
```

### Flush local cache
```js
document.querySelector("form").addEventListener('submit', e => punch_flush());
```

## Advanced usage

```js
import { HolePuncher } from 'holepuncher';

let puncher     = new HolePuncher(options);
let placeholder = document.getElementById(elementId);
let url         = '/some/endpoint';
    
// do something before fetch  
puncher.events.on('before_fetch', e => { 
    const placeholder = e.detail.context;
    placeholder.classList.add('loading');
});

// do something when the data is avaiable
puncher.events.on('fetch_data', e => { 
    // hide loader 
    const placeholder = e.detail.context;
    const response = e.detail.response.clone();
    response.json().then(data => {
        placeholder.innerHTML = data.someAttribute;
        placeholder.classList.remove('loading');
        console.log(data.anotherAttribute)
    }); 
});
    
// fetch html    
puncher.fetch(url, placeholder);   

// fetch json    
puncher.fetch(url, placeholder, { 
    credentials: 'same-origin',
    headers: new Headers({
        'Content-Type': 'application/json'
    })
});      
        
// register listener that flushes the local cache 
document.querySelector("form").addEventListener('submit', e => {
    puncher.flush({verbose: true})
});
    
```

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