Comments (12)
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.
The solution is to use self.registration.scope
instead of location
. This points to the scope of the service worker.
from wayne.
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.
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.
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.
self.registration.scope
returns url with domain, I don't test HTTP yet.
from wayne.
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.
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.
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.
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.
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.
ok, thx, I will recheck my situation.
from wayne.
Related Issues (20)
- Create PoC for WebRTC chat HOT 3
- Add copyright and version to compiled files
- Add support for glob path
- Create PoC of SSE Proxy HOT 1
- Add middlewere HOT 1
- Waiting for promise inside get route HOT 6
- app.get without res method HOT 28
- Create POC for JSX => HTML
- Add a way to easily call fetch inside route HOT 2
- Update documentation in README
- Add parenetneses and question mark syntax HOT 1
- Error retrieving binary resources HOT 3
- Unicode filenames
- Should Angular work too per React poc? HOT 1
- How to Run Docs demo ? HOT 1
- Add Side Code viewer for the demos
- Add convenient passthru method HOT 1
- Add route capture handler in fetch listener HOT 4
- Body Parser middleware HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wayne.