Skip to content

Commit bbe1417

Browse files
authored
Add docs about caching (#88)
1 parent 503af40 commit bbe1417

9 files changed

Lines changed: 28 additions & 12 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
2-
*.js.map
32
lcov.info

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ A PokeAPI wrapper intended for browsers. Comes fully asynchronous, zero dependen
1414
- [Example requests](#example-requests)
1515
- [Configuration](#configuration)
1616
- [Caching images](#caching-images)
17+
- [Caching methods](#caching-methods)
1718
- [Tests](#tests)
1819
- [Endpoints](#endpoints)
1920
- [Root Endpoints list](#root-endpoints-list)
@@ -31,7 +32,7 @@ console.log(await pokedex.getPokemonsList())
3132
```html
3233
<!-- Included in some HTML -->
3334
<script type="module">
34-
import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper@beta/src/index.js"
35+
import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper@2.0.2/src/index.js"
3536
const pokedex = await Pokedex.init();
3637
const version = await pokedex.getVersionByName(1)
3738
console.log(version)
@@ -65,7 +66,7 @@ pokedex.resource([
6566
## Configuration
6667

6768
Pass an Object to `Pokedex.init()` in order to configure it. Available options: `protocol`, `hostName`, `versionPath`, `cache`, `timeout`(ms), and `cacheImages`.
68-
Any option is optional :smile:. All the default values can be found [here](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/src/config.js#L3-L10)
69+
Any option is optional :smile:. All the default values can be found [here](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/src/config.js#L3-L11)
6970

7071
```js
7172
const customOptions = {
@@ -74,7 +75,8 @@ const customOptions = {
7475
versionPath: "/api/v2/",
7576
cache: true,
7677
timeout: 5 * 1000, // 5s
77-
cacheImages: true
78+
cacheImages: true,
79+
swLocation: '/'
7880
}
7981
const pokedex = await Pokedex.init(customOptions);
8082
```
@@ -86,14 +88,22 @@ Pokeapi.co serves its Pokemon images through [Github](https://github.com/PokeAPI
8688
`pokeapi-js-wrapper` enables browsers to cache all these images by:
8789

8890
1. enabling the config parameter `cacheImages`
89-
2. serving [our service worker](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/src/pokeapi-js-wrapper-sw.js) from the root of your project
91+
2. serving [our service worker](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/test/pokeapi-js-wrapper-sw.js) from the root of your project
9092

9193
In this way when `pokeapi-js-wrapper`'s `Pokedex` is initialized it will install and start the Service Worker you are serving at the root of your server. The Service Worker will intercept all the calls your HTML/CSS/JS are making to get PokeAPI's images and will cache them.
9294

9395
It's fundamental that you download the Service Worker [we provide](https://raw.githubusercontent.com/PokeAPI/pokeapi-js-wrapper/master/src/pokeapi-js-wrapper-sw.js)_(Right Click + Save As)_ and you serve it from the root of your project/server. Service Workers in fact cannot be installed from a domain different than yours.
9496

9597
A [basic example](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/test/example-sw.html) is hosted [here](https://pokeapi.github.io/pokeapi-js-wrapper/test/example-sw.html).
9698

99+
### Caching methods
100+
101+
```js
102+
await P.getCacheLength() // Get how many objects are cached
103+
await P.clearCache() // Remove all entries
104+
await P.invalidateCache() // Remove only stale entries
105+
```
106+
97107
## Tests
98108

99109
`pokeapi-js-wrapper` can be tested using two strategies. One is with Node, since this package works with Node, and the other with a browser.
@@ -102,7 +112,7 @@ A [basic example](https://github.com/PokeAPI/pokeapi-js-wrapper/blob/master/test
102112
npm test
103113
```
104114

105-
Or open `/test/test.html` in your browser. A live version can be found at [`gh-pages`](https://pokeapi.github.io/pokeapi-js-wrapper/test/test.html)
115+
Or open `test/test.html` in your browser after serving the project with `npm run serve`. A live version can be found on the project [`gh-pages`](https://pokeapi.github.io/pokeapi-js-wrapper/test/test.html)
106116

107117
## Endpoints
108118

src/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Config {
88
this.timeout = 10 * 1000 // 2 seconds
99
this.cache = true
1010
this.cacheImages = false
11+
this.swLocation = '/'
1112

1213
if (config.hasOwnProperty('protocol')) {
1314
this.protocol = config.protocol
@@ -33,6 +34,9 @@ class Config {
3334
if (config.hasOwnProperty('cacheImages')) {
3435
this.cacheImages = config.cacheImages
3536
}
37+
if (config.hasOwnProperty('swLocation')) {
38+
this.swLocation = config.swLocation
39+
}
3640
}
3741
}
3842
export { Config }

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class Pokedex {
5050
})
5151

5252
if (this.config.cacheImages) {
53-
import('./installSW.js').then(module=>module.installSW())
53+
import('./installSW.js').then(module=>module.installSW(this.config.swLocation))
5454
}
5555
}
5656

src/installSW.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { log } from './utils.js'
22

3-
export function installSW() {
3+
export function installSW(swLocation) {
44
if (navigator && window && 'serviceWorker' in navigator) {
55
window.addEventListener('load', function() {
6-
navigator.serviceWorker.register('./pokeapi-js-wrapper-sw.js', { scope: './' })
6+
navigator.serviceWorker.register(`${swLocation}pokeapi-js-wrapper-sw.js`, { scope: './' })
77
.catch(error => {
88
log('SW installation failed with the following error:')
99
log(error)

test/cdn.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// var PJSW_DEBUG=1
77
</script>
88
<script type="module">
9-
//import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper@beta/src/index.js"
9+
//import {Pokedex} from "https://cdn.jsdelivr.net/gh/pokeapi/pokeapi-js-wrapper/src/index.js"
1010
import {Pokedex} from "../src/index.js"
1111
const pokedex = await Pokedex.init();
1212
const version = await pokedex.getVersionByName(1)

test/example-sw.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>SW test</title>
55
<script type="module">
66
import {Pokedex} from '../src/index.js'
7-
const P = await Pokedex.init({ cacheImages: true });
7+
const P = await Pokedex.init({ cacheImages: true, swLocation: '/pokeapi-js-wrapper/test/' });
88
</script>
99
</head>
1010

test/test.html.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ describe("service worker", function () {
88
this.timeout(10000);
99

1010
before(async function() {
11-
P = await Pokedex.init({ cacheImages: true });
11+
P = await Pokedex.init({
12+
cacheImages: true,
13+
swLocation: '/test/'
14+
});
1215
});
1316

1417
it("should be activated on second run", async function () {

0 commit comments

Comments
 (0)