all repos — NoPaste @ 9af51915792e02b13dcb4ebcf482e6d72081e8d9

Resurrected - The PussTheCat.org fork of NoPaste

Improve service worker
Boris Kubiak kubiakboris@gmail.com
Tue, 01 Dec 2020 11:49:26 +0100
commit

9af51915792e02b13dcb4ebcf482e6d72081e8d9

parent

bf76bde1856415ab0cf0589eab261b061647f740

2 files changed, 33 insertions(+), 7 deletions(-)

jump to
M README.mdREADME.md

@@ -15,7 +15,7 @@ - ⏳ Your data will be accessible **forever** (as long as you have the link)

- 🔀 You can access your data on **every NoPaste clone**, including [your own](https://github.com/bokub/nopaste/wiki/Deploy-your-own-version-of-NoPaste) - 🔍 Google **will not index** your data, even if your link is public -> **Note:** This project is a copy of [Topaz's paste service][topaz-example], with a reworked design and a few additional features (syntax highlighting, line numbers, line wrapping, embedding...) +> **Note:** This project is a copy of [Topaz's paste service][topaz-example], with a reworked design and a few additional features (syntax highlighting, line numbers, offline usage, embedding...) ## How it works

@@ -29,7 +29,9 @@ This process is done entirely **in your browser**, and the web server hosting NoPaste [never has access to the fragment](https://en.wikipedia.org/wiki/Fragment_identifier)

For example, [this is the CSS code used by NoPaste][example] -## Embedded NoPaste snippets +## Other features + +### Embedded NoPaste snippets You can include NoPaste code snippets into your own website by clicking the _Embed_ button and using the generated HTML code.

@@ -47,6 +49,21 @@

[![iframe](https://raw.githubusercontent.com/bokub/nopaste/images/pagerank.png)](https://jsfiddle.net/cqr2kxf5/) Feel free to edit the `height` and `width` attributes, so they suit your needs + +### Offline usage + +When you visit NoPaste for the first time, its code is saved in your browser cache. After that, every NoPaste link you open +will load really quick, even if your internet connexion is slow. + +What if you have no internet connexion at all? No problem, NoPaste will still work perfectly! + +### Editor features + +- Syntax highlighting (use the language selector) +- Enable / disable line wrapping (use the button next to the language selector) +- Delete line (`Ctrl+D`) +- Multiple cursors (`Ctrl+Click`) +- Usual keyboard shortuts (`Ctrl+A`, `Ctrl+Z`, `Ctrl+Y`...) ## Maximum sizes for links
M sw.jssw.js

@@ -1,11 +1,16 @@

-const PRECACHE = 'precache-20201121c'; -const RUNTIME = 'runtime'; +const VERSION = '20201201'; +const PRECACHE = 'precache-' + VERSION; +const MODES = 'modes-' + VERSION; + +// A regexp to detect mode files +const MODE_REGEXP = /^https:\/\/cdn\.jsdelivr.net\/npm\/codemirror@.+?\/mode\//; // A list of local resources we always want to be cached. const PRECACHE_URLS = [ '/', 'script.js', 'style.css', + 'favicon.ico', 'https://cdn.jsdelivr.net/npm/lzma@2.3.2/src/lzma_worker.min.js', 'https://cdn.jsdelivr.net/combine/' + 'npm/lzma@2.3.2/src/lzma.min.js,' +

@@ -43,7 +48,7 @@ });

// The activate handler takes care of cleaning up old caches. self.addEventListener('activate', (event) => { - const currentCaches = [PRECACHE, RUNTIME]; + const currentCaches = [PRECACHE, MODES]; event.waitUntil( caches .keys()

@@ -61,7 +66,7 @@ .then(() => self.clients.claim())

); }); -// The fetch handler serves responses for same-origin resources from a cache. +// The fetch handler serves responses from a cache. // If no response is found, it populates the runtime cache with the response // from the network before returning it to the page. self.addEventListener('fetch', (event) => {

@@ -74,7 +79,11 @@ if (cachedResponse) {

return cachedResponse; } - return caches.open(RUNTIME).then((cache) => { + if (!event.request.url.match(MODE_REGEXP)) { + return fetch(event.request); + } + + return caches.open(MODES).then((cache) => { return fetch(event.request).then((response) => { // Put a copy of the response in the runtime cache. return cache.put(event.request, response.clone()).then(() => {