Skip to content

Commit 495b4f8

Browse files
committed
add public method respond
1 parent 567e17f commit 495b4f8

File tree

9 files changed

+80
-15
lines changed

9 files changed

+80
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.18.0
2+
### Features
3+
* add public method `respond` where you can use your own `Response` object
4+
15
## 0.17.0
26
### Features
37
* allow using glob as domain name

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION=0.17.0
1+
VERSION=0.18.0
22
DATE=`date -uR`
33
YEAR=`date +%Y`
44

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
alt="Logo of Wayne library - it represents construction worker helmet and text with the name of the library" />
44
</h1>
55

6-
[![npm](https://img.shields.io/badge/npm-0.17.0-blue.svg)](https://www.npmjs.com/package/@jcubic/wayne)
6+
[![npm](https://img.shields.io/badge/npm-0.18.0-blue.svg)](https://www.npmjs.com/package/@jcubic/wayne)
77
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://makeapullrequest.com)
88
[![jSDelivr](https://data.jsdelivr.com/v1/package/npm/@jcubic/wayne/badge)](https://www.jsdelivr.com/package/npm/@jcubic/wayne)
99

@@ -528,6 +528,59 @@ import require$$0 from"/npm/[email protected]/+esm"
528528
And needs to be imported from jsDelivr, the same if you import CSS file.
529529
See [example of loading jQuery Terminal](https://jcubic.github.io/wayne/esm/) where this code is used.
530530

531+
### PWA
532+
533+
To use with PWA and cache you need to use a custom middleware:
534+
535+
```javascript
536+
const app = new Wayne();
537+
538+
app.use(async (req, res, next) => {
539+
const url = new URL(req.url);
540+
541+
const cache = await get_cache();
542+
const cache_response = await cache.match(req);
543+
544+
if (cache_response) {
545+
if (navigator.onLine) {
546+
const net_response = await fetch(req);
547+
cache.put(req, net_response.clone());
548+
res.respond(net_response);
549+
} else {
550+
res.respond(cache_response);
551+
}
552+
} else {
553+
next();
554+
}
555+
});
556+
557+
const cache_url = [
558+
'/',
559+
'https://cdn.jsdelivr.net/npm/@jcubic/wayne/index.umd.min.js',
560+
'https://cdn.jsdelivr.net/npm/idb-keyval@6/dist/umd.js'
561+
];
562+
563+
self.addEventListener('install', (event) => {
564+
event.waitUntil(cache_all());
565+
});
566+
567+
function get_cache() {
568+
return caches.open('pwa-assets');
569+
}
570+
571+
async function cache_all() {
572+
const cache = await get_cache();
573+
return cache.addAll(cache_url);
574+
}
575+
```
576+
577+
This approach is recommended by the answer to this StackOverflow question:
578+
579+
* [Service-worker force update of new assets](https://stackoverflow.com/a/33266296/387194)
580+
581+
It always fetch a new value of the assets when there is internet connection and serve cached value
582+
when user is offline. When there are no cached value it do default action (which can be normal
583+
fetch outside of cache or Wayne route).
531584

532585
## First load
533586

@@ -554,6 +607,7 @@ by [Jake Archibald](https://twitter.com/jaffathecake).
554607
* [Download demo](https://jcubic.github.io/wayne/download/).
555608
* [Source Code Syntax highlight demo](https://jcubic.github.io/wayne/code/).
556609
* [Using with React and Vite](https://jcubic.github.io/react-wayne-auth/)
610+
* [PWA/Cache](https://jcubic.github.io/wayne/pwa/)
557611

558612
The source code for the demos can be found
559613
[in the docs' directory at the gh-pages branch](https://github.com/jcubic/wayne/tree/gh-pages/docs).
@@ -604,6 +658,7 @@ each of those methods accepts string as the first argument. The second argument
604658

605659
Additional methods:
606660
* `redirect()` - accept URL or optional first argument that is the number of HTTP code
661+
* `respond(res)` - accept Response object in case you want to use a different [Response object](https://developer.mozilla.org/en-US/docs/Web/API/Response).
607662
* `sse([options])` - function creates Server-Sent Event stream, the return object has a method `send` that sends a new event.
608663
* `fetch(url | Request)` - method will send a normal HTTP request to the server and return the result to the client. You can use the default Request object from the route.
609664
* `download(data, { filename })` - a method that can be used to trigger file download. The data can be a `string` or `arrayBuffer` you can use native fetch API and call `await res.text()` or `await res.arrayBuffer()` and pass the result as data.

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Wayne - Server Worker Routing library (v. 0.17.0)
2+
* Wayne - Server Worker Routing library (v. 0.18.0)
33
*
44
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
55
* Released under MIT license
@@ -101,6 +101,9 @@ export class HTTPResponse {
101101
json(data, init) {
102102
this.send(JSON.stringify(data), { type: 'application/json', ...init });
103103
}
104+
respond(response) {
105+
this._resolve(response);
106+
}
104107
blob(blob, init = {}) {
105108
this._resolve(new Response(blob, init));
106109
}

index.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.umd.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* Wayne - Server Worker Routing library (v. 0.17.0)
2+
* Wayne - Server Worker Routing library (v. 0.18.0)
33
*
44
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
55
* Released under MIT license
66
*
7-
* Fri, 26 Jul 2024 23:46:08 +0000
7+
* Sat, 05 Oct 2024 17:28:37 +0000
88
*/
99
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.wayne = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
1010
"use strict";
@@ -19,7 +19,7 @@ exports.Wayne = void 0;
1919
exports.rpc = rpc;
2020
exports.send = send;
2121
/*
22-
* Wayne - Server Worker Routing library (v. 0.17.0)
22+
* Wayne - Server Worker Routing library (v. 0.18.0)
2323
*
2424
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
2525
* Released under MIT license
@@ -116,6 +116,9 @@ class HTTPResponse {
116116
...init
117117
});
118118
}
119+
respond(response) {
120+
this._resolve(response);
121+
}
119122
blob(blob, init = {}) {
120123
this._resolve(new Response(blob, init));
121124
}

index.umd.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jcubic/wayne",
3-
"version": "0.17.0",
3+
"version": "0.18.0",
44
"description": "Service Worker Routing for in browser HTTP requests",
55
"type": "module",
66
"main": "index.min.js",

0 commit comments

Comments
 (0)