Skip to content

Commit

Permalink
add public method respond
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed Oct 5, 2024
1 parent 567e17f commit 495b4f8
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.18.0
### Features
* add public method `respond` where you can use your own `Response` object

## 0.17.0
### Features
* allow using glob as domain name
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=0.17.0
VERSION=0.18.0
DATE=`date -uR`
YEAR=`date +%Y`

Expand Down
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
alt="Logo of Wayne library - it represents construction worker helmet and text with the name of the library" />
</h1>

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

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

### PWA

To use with PWA and cache you need to use a custom middleware:

```javascript
const app = new Wayne();

app.use(async (req, res, next) => {
const url = new URL(req.url);

const cache = await get_cache();
const cache_response = await cache.match(req);

if (cache_response) {
if (navigator.onLine) {
const net_response = await fetch(req);
cache.put(req, net_response.clone());
res.respond(net_response);
} else {
res.respond(cache_response);
}
} else {
next();
}
});

const cache_url = [
'/',
'https://cdn.jsdelivr.net/npm/@jcubic/wayne/index.umd.min.js',
'https://cdn.jsdelivr.net/npm/idb-keyval@6/dist/umd.js'
];

self.addEventListener('install', (event) => {
event.waitUntil(cache_all());
});

function get_cache() {
return caches.open('pwa-assets');
}

async function cache_all() {
const cache = await get_cache();
return cache.addAll(cache_url);
}
```

This approach is recommended by the answer to this StackOverflow question:

* [Service-worker force update of new assets](https://stackoverflow.com/a/33266296/387194)

It always fetch a new value of the assets when there is internet connection and serve cached value
when user is offline. When there are no cached value it do default action (which can be normal
fetch outside of cache or Wayne route).

## First load

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

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

Additional methods:
* `redirect()` - accept URL or optional first argument that is the number of HTTP code
* `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).
* `sse([options])` - function creates Server-Sent Event stream, the return object has a method `send` that sends a new event.
* `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.
* `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.
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Wayne - Server Worker Routing library (v. 0.17.0)
* Wayne - Server Worker Routing library (v. 0.18.0)
*
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
* Released under MIT license
Expand Down Expand Up @@ -101,6 +101,9 @@ export class HTTPResponse {
json(data, init) {
this.send(JSON.stringify(data), { type: 'application/json', ...init });
}
respond(response) {
this._resolve(response);
}
blob(blob, init = {}) {
this._resolve(new Response(blob, init));
}
Expand Down
6 changes: 3 additions & 3 deletions index.min.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions index.umd.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* Wayne - Server Worker Routing library (v. 0.17.0)
* Wayne - Server Worker Routing library (v. 0.18.0)
*
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
* Released under MIT license
*
* Fri, 26 Jul 2024 23:46:08 +0000
* Sat, 05 Oct 2024 17:28:37 +0000
*/
(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){
"use strict";
Expand All @@ -19,7 +19,7 @@ exports.Wayne = void 0;
exports.rpc = rpc;
exports.send = send;
/*
* Wayne - Server Worker Routing library (v. 0.17.0)
* Wayne - Server Worker Routing library (v. 0.18.0)
*
* Copyright (c) 2022-2024 Jakub T. Jankiewicz <https://jcubic.pl/me>
* Released under MIT license
Expand Down Expand Up @@ -116,6 +116,9 @@ class HTTPResponse {
...init
});
}
respond(response) {
this._resolve(response);
}
blob(blob, init = {}) {
this._resolve(new Response(blob, init));
}
Expand Down
6 changes: 3 additions & 3 deletions index.umd.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jcubic/wayne",
"version": "0.17.0",
"version": "0.18.0",
"description": "Service Worker Routing for in browser HTTP requests",
"type": "module",
"main": "index.min.js",
Expand Down

0 comments on commit 495b4f8

Please sign in to comment.