Skip to content

ServiceWorker does not allow 'file://' origin #2831

Closed

Description

@rogeru

I am trying to intercept image load request from my HTML to https requests. Specifically my app has a URL "images/test.png" in the HTML and electron tries to load that with a file scheme "file:///images/test.png". I would like to rewrite this request to https://mydomain.com/images/test.png

For this I was trying to use a service worker since they allow to intercept the network requests and modify the URL.

The problem is that electron (well chromium) does not allow "file://" origin. Chromium states that Service Workers require a secure origin, such as HTTPS.

The exact error is:
"Failed to register a ServiceWorker: The URL protocol of the current origin ('file://') is not supported."

Is there a way to get around that with electron?

A somewhat related Chromium submit is https://codereview.chromium.org/1191793003/#ps40001

Activity

  1. zcbenz commented on Sep 21, 2015

    @zcbenz
    Contributor

    We can provide a webFrame.registerURLSchemeAsAllowingServiceWorkers API for this.

  2. anaisbetts commented on Sep 21, 2015

    @anaisbetts
    Contributor

    It seems as if many Chrome APIs are moving to HTTPS-only, it'd be great to have a single "registerURLSchemeAsPrivileged" method to encompass all of these features instead of having to one-off all of them

  3. zcbenz commented on Sep 21, 2015

    @zcbenz
    Contributor

    It seems as if many Chrome APIs are moving to HTTPS-only, it'd be great to have a single "registerURLSchemeAsPrivileged" method to encompass all of these features instead of having to one-off all of them

    👍

  4. deepak1556 commented on Sep 21, 2015

    @deepak1556
    Member

    registerURLSchemeAsPrivileged sounds good, but should it allow file and like scheme or we can just allow custom schemes which are registered as standard ? Otherwise will file like schemes elevated as privileged lead to origin issues ?

  5. zcbenz commented on Sep 21, 2015

    @zcbenz
    Contributor

    @deepak1556 A simpe wrapper around blink APIs should be enough, we don't need to make the API do too much things unless it is proven that we have to.

  6. PhilJ commented on Dec 20, 2015

    @PhilJ

    Any updates here?

    Currently I receiving this error on the console, if I try to register a service worker from a filepath: error when registering sw DOMException: Failed to register a ServiceWorker: A bad HTTP response code (-1) was received when fetching the script.

    I tried to register the file protocol at every method, but still no luck so far

    electron.webFrame.registerURLSchemeAsPrivileged('file');
    electron.webFrame.registerURLSchemeAsSecure('file');
    electron.webFrame.registerURLSchemeAsBypassingCSP('file');
    
    app.on('ready', () => {
        electron.protocol.registerServiceWorkerSchemes(['file:']);
    });
    

    Has anybody been able to successfully register a service worker via file:///?

  7. deepak1556 commented on Dec 20, 2015

    @deepak1556
    Member

    @PhilJ 'file' scheme has been registered to enable service worker by default from v0.36.0 . which version of electron are you on ?

    Have verified working with https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker . Could you provide sample app in case it doesn't work for you, thanks!

  8. PhilJ commented on Dec 20, 2015

    @PhilJ

    Thanks for your response and the test case. I'm using Electron 0.36.1, so the file protocol should be supported for service workers.

    I reduced the problem to the simple registration of an service worker and it still fails on my machine, even though it is fully covered by your test case. sw.js exists under this path (when i copy the path from the error to a browser instance the file is loaded). However this is not relevant for the error, it does not change if I remove the worker file.

    // client/index.html
    <script>
    navigator.serviceWorker.register('sw.js', {scope: './'})
    .then(function () {
      console.log('sw registered')
    })
    .catch(function (error) {
      console.log('error when registering sw', error)
    });
    </script>
    
    // client/sw.js
    self.addEventListener('fetch', function (event) {
      console.log(event);
    });
    
    // main.js
    app.on('ready', () => {
        mainWindow = new BrowserWindow({ width: 1200, height: 1000 });
        mainWindow.loadURL(`file://${__dirname}/client/index.html`);
    });
    

    On the console I receive an error, that the registration failed. The network monitor shows a strange error, I guess the browser must have somehow blocked the request.

    screen shot 2015-12-21 at 00 06 09

    Any idea what could be different about my environment, that causes this behaviour?

  9. deepak1556 commented on Dec 22, 2015

    @deepak1556
    Member

    @PhilJ could you try clearing sw storage data using session.clearStorageData and retry.

  10. havenchyk commented on Dec 23, 2015

    @havenchyk

    @deepak1556 I also have this kind of problem with 0.36.0. Also reported issue #3897

  11. havenchyk commented on Dec 23, 2015

    @havenchyk

    @deepak1556 after session.clearStorageDate I have the same message:

    Failed to register a ServiceWorker: No URL is associated with the caller's document

  12. havenchyk commented on Dec 23, 2015

    @havenchyk

    @PhilJ In my case the problem was in the old version of electron-prebuilt

  13. PhilJ commented on Feb 23, 2016

    @PhilJ

    The problem for me was caused by electron-compile, which still suffers by that issue. I'm now using gulp for build an service workers work like a charm now.

  14. anaisbetts commented on Feb 24, 2016

    @anaisbetts
    Contributor

    The problem for me was caused by electron-compile, which still suffers by that issue.

    Can you file a bug for this?

  15. VandeurenGlenn commented on Jul 24, 2016

    @VandeurenGlenn

    Maybe replacing request.url could be a solution (temporary)?

    var request = new Request(urlWithCacheBusting,
                  {credentials: 'same-origin'});
    
    request.url = urlWithCacheBusting.replace(/file:\/\/\/(.*):\//, '');
  16. rahul-dutt-sharma commented on Jan 23, 2018

    @rahul-dutt-sharma

    Hey, I am able to make a Fetch request but unable to save the response in the Caches API's cache I have to create an cached app read from an in memory webui.zip file

    Is this even possible?

    I get the error that file:// request is unsupported by the cache api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions