bahmutov / todomvc-express-and-service-worker

Server-side TodoMVC rendered with ExpressJS but running inside ServiceWorker demo
https://express-service.herokuapp.com/
13 stars 1 forks source link

Can we cache service worker script #3

Open bahmutov opened 8 years ago

bahmutov commented 8 years ago

To be able to make the stand alone desktop application using nativefier

To build

nativefier --name TodoMVC https://express-service.gleb-demos.com/
Packaging app for platform darwin x64 using electron v1.1.3
App built to /private/tmp/TodoMVC-darwin-x64

Currently, HTML, CSS and client.js are cached inside the app and load, but the service worker script is not. Probably because it is loaded dynamically from the client.js. When starting the app in offline mode for the very first time it fails to load the service worker js. If it can load it the first time then it works in offline mode after that.

GET https://express-service.gleb-demos.com/express-service.js net::ERR_INTERNET_DISCONNECTEDfs.existsSync @ fs.js:258fs.existsSync @ ELECTRON_ASAR.js:361injectScripts @ /private/tmp/TodoMVC-darwin-x64/TodoMVC.app/Contents/Resources/app/lib/static/preload.js:85(anonymous function) @ /private/tmp/TodoMVC-darwin-x64/TodoMVC.app/Contents/Resources/app/lib/static/preload.js:42
client.js:36 express service worker error DOMException: Failed to register a ServiceWorker: An unknown error occurred when fetching the script.
bahmutov commented 8 years ago

Relevant client.js code

function getCurrentScriptFolder () {
    var scriptEls = document.getElementsByTagName('script')
    var thisScriptEl = scriptEls[scriptEls.length - 1]
    var scriptPath = thisScriptEl.src
    return scriptPath.substr(0, scriptPath.lastIndexOf('/') + 1)
  }

  var serviceScriptUrl = getCurrentScriptFolder() + 'express-service.js'
  var scope = '/'
root.navigator.serviceWorker.register(serviceScriptUrl, { scope: scope })
    .then(registeredWorker)
    .catch(onError)
aje4u2i commented 7 years ago

While using service worker for my web app.I am caching a file named gadget.js file. But inside that file is inserting some script files like, doc =document.createElement('iframe'); doc.write(`<!DOCTYPE html>

`); doc.close(); when i am trying to load the app in offline mode the script file('xxx/xxx/abc.js') is not getting proxied by service-worker.Since it is not getting detected(proxied) by service-worker i am getting a 404 error. Please help to solve this.....