Browse Source

add service workers

main
Mikey Oz 3 years ago
parent
commit
7eb1068807
  1. 7
      app/client.imba
  2. 26
      app/index.html
  3. 1
      app/utils/idb_wrapper.imba
  4. 8
      package.json
  5. 50
      sw.imba

7
app/client.imba

@ -1,6 +1,7 @@
let p = console.log let p = console.log
import { sortBy } from 'lodash' import { sortBy } from 'lodash'
import { version } from '../package.json'
import fzy from './utils/fzy' import fzy from './utils/fzy'
import download_json_file from './utils/download' import download_json_file from './utils/download'
import upload_json_file from './utils/upload' import upload_json_file from './utils/upload'
@ -13,7 +14,7 @@ db.open!
global css body global css body
d:flex fld:column jc:center ai:center d:flex fld:column jc:center ai:center
m:0 w:100% h:100% bg:#20222f m:0 w:100% h:100% bg:#20222f
ff:Open Sans ff:sans-serif
tag app tag app
@ -89,7 +90,7 @@ tag app
css button, label css button, label
d:flex fld:column jc:center ai:center d:flex fld:column jc:center ai:center
bg:none c:purple4 bd:none cursor:pointer fl:1 bg:none c:purple4 bd:none cursor:pointer fl:1
fs:14px font-weight:bold ff:Open Sans fs:14px ff:sans-serif
css $input css $input
bd:1px solid purple4 bd:1px solid purple4
@ -97,6 +98,7 @@ tag app
bc:purple4 outline:none c:blue3 caret-color:blue3 px:20px bc:purple4 outline:none c:blue3 caret-color:blue3 px:20px
transition:background 0.5s transition:background 0.5s
@focus bg:purple4/10 @focus bg:purple4/10
@placeholder fs:10px c:blue3
css .links css .links
d:flex fld:column jc:flex-start d:flex fld:column jc:flex-start
@ -119,6 +121,7 @@ tag app
<input$input <input$input
@hotkey('mod+k').capture=$input..focus @hotkey('mod+k').capture=$input..focus
bind=state.query bind=state.query
placeholder="v{version}"
@hotkey('return').capture=handle_return @hotkey('return').capture=handle_return
@hotkey('shift+return').capture=handle_shift_return @hotkey('shift+return').capture=handle_shift_return
@hotkey('esc').capture=$input..blur @hotkey('esc').capture=$input..blur

26
app/index.html

@ -1,12 +1,18 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>fuzzyhome</title> <title>fuzzyhome</title>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet"> <style src='*'></style>
<style src='*'></style> </head>
</head> <body>
<body> <script type="module">
<script type="module" src="./client.imba"></script> if ('serviceWorker' in navigator) {
</body> navigator.serviceWorker.register('./sw.js').then(function(reg){
reg.update()
})
}
</script>
<script type="module" src="./client.imba"></script>
</body>
</html> </html>

1
app/utils/idb_wrapper.imba

@ -83,7 +83,6 @@ class idb_wrapper
resolve no resolve no
def #get_store permission="readwrite" def #get_store permission="readwrite"
p permission
let db = openRequest.result let db = openRequest.result
let transaction = db.transaction(table_name, permission) let transaction = db.transaction(table_name, permission)
transaction.objectStore(table_name) transaction.objectStore(table_name)

8
package.json

@ -1,10 +1,14 @@
{ {
"name": "fuzzyhome", "name": "fuzzyhome",
"version": "0.0.5",
"scripts": { "scripts": {
"start": "imba run --baseurl . -w server.imba", "start": "imba run -SMH --baseurl . -w server.imba",
"build": "imba build --baseurl . server.imba", "build-app": "imba build -SMH --baseurl . server.imba",
"build-sw": "imbac sw.imba -o dist/public",
"build": "npm run build-app && npm run build-sw",
"nojekyll": "npx touch dist/public/.nojekyll", "nojekyll": "npx touch dist/public/.nojekyll",
"pages": "npx gh-pages --no-history --dotfiles --dist dist/public", "pages": "npx gh-pages --no-history --dotfiles --dist dist/public",
"static": "cd dist/public && echo http://localhost:8000 && python3 -m http.server &>/dev/null",
"deploy": "npm run build && npm run nojekyll && npm run pages" "deploy": "npm run build && npm run nojekyll && npm run pages"
}, },
"dependencies": { "dependencies": {

50
sw.imba

@ -0,0 +1,50 @@
let p = console.log
const app_prefix = "fuzzyhome_cache"
const version = "0.0.5"
const cache_name = "{app_prefix}-{version}"
let urls = [
'./',
'./__assets__/all.css'
'./__assets__/app/client.js'
'./__assets__/app/client.css'
]
self.addEventListener('fetch', &) do |e|
def intercept request
if request
p `{cache_name} responding with cache : {e.request.url}`
request
else
p `{cache_name} not cached, fetching : {e.request.url}`
fetch e.request
e.respondWith(caches.match(e.request.url).then(intercept))
self.addEventListener('install', &) do |e|
p `{cache_name} install`
def add_urls_to_cache cache
p `{cache_name} installing cache : {cache_name}`
cache.addAll urls
skipWaiting! # [TODO]: Export this and import to settings component and have manual update button
e.waitUntil(caches.open(cache_name).then(add_urls_to_cache))
self.addEventListener('activate', &) do |e|
p `{cache_name} activate`
def delete_cached keys
let temp = keys.map(&) do |key, i|
if key !== cache_name
p `{cache_name} deleting cache : {keys[i]}`
caches.delete keys[i]
Promise.all(temp)
e.waitUntil(caches.keys().then(delete_cached))
Loading…
Cancel
Save