diff --git a/.gitignore b/.gitignore index 775e611..7aaf6eb 100644 --- a/.gitignore +++ b/.gitignore @@ -73,4 +73,6 @@ firebase.json # vercel build output api # https://vercel.com/docs/build-output-api/v3 -.vercel \ No newline at end of file +.vercel + +pnpm-lock.yaml diff --git a/README.md b/README.md index 8259974..c0e6f88 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Status: Meme -A 2kB zero-config router and prefetcher that makes a static site feel like a blazingly fast SPA. +A 3kB zero-config router and prefetcher that makes a static site feel like a blazingly fast SPA. ## Why? @@ -49,6 +49,10 @@ window.addEventListener('flamethrower:router:end', hideLoader); // Disable it router.enabled = false; + +// Subscribe/unsubscribe specific routes to function +router.subscribe([''], ); +router.unsubscribe('', ); ``` Opt-out of specific links for full page load. @@ -85,6 +89,40 @@ Prefecthing is disabled by default. const router = flamethrower({ prefetch: 'visible' }); ``` +### route subscriptions + +the router class extends the routerdata class, which has the subscribe method + +see example/main.js for some more complex use cases + +```js +router.subscribe(['/logme/', '/'], ); +router.unsubscribe('/', ); + +function logMe() { + console.log('hi mom'); +} + +// Subscribe all routes to function +router.subscribe(['*'], logMe); + +// only call function once +// ie to create a closure over multiple subscriptions +router.subscribe(['**'], closeMe); +function closeMe() { + router.subscribe(['/false/'], falseMe); + router.subscribe(['/true/'], trueMe); + let val = true; + function falseMe() { + val = false; + } + function trueMe() { + val = true; + } +} +``` + + ### Misc **Supported in all browsers?** Yes. It will fallback to standard navigation if `window.history` does not exist. diff --git a/example/about/index.html b/example/about/index.html index 31662b4..c0e2425 100644 --- a/example/about/index.html +++ b/example/about/index.html @@ -4,15 +4,18 @@ - + About + + @@ -40,7 +43,7 @@

About

This text should be preserved when starting from about

-
+
@@ -57,6 +60,10 @@

Scripts

Scripts from body or head with data-reload attr should always run

+

+

+

+

default text

diff --git a/example/bar.js b/example/bar.js new file mode 100644 index 0000000..01749a0 --- /dev/null +++ b/example/bar.js @@ -0,0 +1,27 @@ + +export class Bar { + constructor(val) { + this._value = val; + } + + logVal() { + console.log(this.value); + } + + setText() { + let el = document.getElementById("global-check"); + el.innerText = this.value; + } + + get value() { + return this._value; + } + + set value(val) { + if (this._value !== val) { + this._value = val; + this.logVal(); + this.setText(); + } + } +} diff --git a/example/foo.js b/example/foo.js new file mode 100644 index 0000000..94c203b --- /dev/null +++ b/example/foo.js @@ -0,0 +1,11 @@ + +export class Foo { + constructor(id, value) { + this.el = document.getElementById(id); + this.val = value; + } + + apply() { + this.el.innerText = this.val; + } +} diff --git a/example/index.html b/example/index.html index 0b6b2bd..622dbb5 100644 --- a/example/index.html +++ b/example/index.html @@ -7,10 +7,13 @@ Flamethrower + +