Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I custom root_url to normalize_url normally? #33

Open
jonsam-ng opened this issue Apr 1, 2024 · 12 comments
Open

How can I custom root_url to normalize_url normally? #33

jonsam-ng opened this issue Apr 1, 2024 · 12 comments

Comments

@jonsam-ng
Copy link

With the following line:

const root_url = location.pathname.replace(/\/[^\/]+$/, '');

my root_url could be '/s', which lead to a failed route match for my request starts with '/s', but I know my root url should be '/', how can I fix this problem?

@jcubic
Copy link
Owner

jcubic commented Apr 1, 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).

jcubic added a commit that referenced this issue Apr 1, 2024
@jcubic
Copy link
Owner

jcubic commented Apr 1, 2024

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

@jonsam-ng
Copy link
Author

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));

@jcubic
Copy link
Owner

jcubic commented Apr 2, 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.

@jcubic
Copy link
Owner

jcubic commented Apr 2, 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.

@jonsam-ng
Copy link
Author

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

@jcubic
Copy link
Owner

jcubic commented Apr 2, 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?

@jonsam-ng
Copy link
Author

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.

@jcubic
Copy link
Owner

jcubic commented Apr 3, 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.

@jonsam-ng
Copy link
Author

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'?

@jcubic
Copy link
Owner

jcubic commented Apr 3, 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.

@jonsam-ng
Copy link
Author

ok, thx, I will recheck my situation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants