Git Product home page Git Product logo

Comments (12)

jcubic avatar jcubic commented on May 18, 2024

Do you have an example website that show the problem? I forget that you can read directories without slash at the end (I think that I have something like this on my other website).

from wayne.

jcubic avatar jcubic commented on May 18, 2024

The solution is to use self.registration.scope instead of location. This points to the scope of the service worker.

from wayne.

jonsam-ng avatar jonsam-ng commented on May 18, 2024

That maybe works, but self.registration.scope is a absolute path, but normalize_url may receive a relative path as pathname, Is that ok?

let path_name = normalize_url(decodeURIComponent(url.pathname));

from wayne.

jcubic avatar jcubic commented on May 18, 2024

normalize_url path should get path to the route that should start with slash. This function is to remove the path to service worker from the URL request.

If you have path /s as route, normalize_url should return /s from full URL request.

I don't think it make sense to use relative path in route.

I need to check to be sure that the trailing slash is there. The version was not yet released to NPM.

from wayne.

jcubic avatar jcubic commented on May 18, 2024

I think I did a mistake, I tested with wrong demo. self.registration returns full URL not that pathname. And also it only works over HTTPS.

from wayne.

jonsam-ng avatar jonsam-ng commented on May 18, 2024

self.registration.scope returns url with domain, I don't test HTTP yet.

from wayne.

jcubic avatar jcubic commented on May 18, 2024

I fixed the code, now it should work the same as before. self.registration.scope is used first and if it fails to use fallback of location.pathname.

Again, can you show me an example where the library gives problems?

from wayne.

jonsam-ng avatar jonsam-ng commented on May 18, 2024

Sorry for late reply. I can describe the scenario I encountered in detail. My website carries the prefix /s/sw.js when request serviceWorker, and the scope is /. When I register a route similar to /m/getData in my application, I find that the processing logic of the route does not take effect. After debugging, I found that the root_url value is' /m ', which is not what I expected '/', and then in 'normalize_url', the prefix '/m' of the route I registered was removed, that causes the route did not match.
I wonder if root_url should allow customization to ensure that 'normalize_url' results as expected.

from wayne.

jcubic avatar jcubic commented on May 18, 2024

If you have service worker /s/sw.js here, the scope of service worker can only be in /s/ so you can only request the URL /s/m/getData. But if you only call it via AJAX you can route this as well, but you have to use full URL with domain name (origin)

I can check what I can do to allow routing /m/getData but the original idea was to only route what's in the current directory. So if you have your app in /foo/bar/ and use get('/baz' you get /foo/bar/baz.

To change this behavior (I don't want to have a breaking change) I would need to introduce @origin that point to the same origin the website is served.

So if you use get('@origin/bar' it will request /bar not /foo/bar/baz. But note that if the worker is not in root directory, you will not be able to open the page in new tab. The Service Worker will only control the request if it's originated from a controlled page.

from wayne.

jonsam-ng avatar jonsam-ng commented on May 18, 2024

Sorry, I don't understand clearly. If I register a service worker like this:

navigator.serviceWorker.register("/s/sw.js", { scope: '/' })

Should I only request from /s/m/getData, instead of /m/getData?
I found from MDN as By default, the scope value for a service worker registration is set to the directory where the service worker script is located in ServiceWorkerContainer: register(), if I provided scope, my root url should be '/' or '/s'?

from wayne.

jcubic avatar jcubic commented on May 18, 2024

Yes exactly if you have /s/sw.js file the get('/m/getData') will point to /s/m/getData

You can't provide { scope: '/'} if your service worker is in /s/ directory, this is literally written in the quote you added the scope needs to be {scope: '/s/'} or any nested directory.

from wayne.

jonsam-ng avatar jonsam-ng commented on May 18, 2024

ok, thx, I will recheck my situation.

from wayne.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.