How to register service worker on other domain using sub-domain - javascript

I have domain like https://example.com and hosting service worker js and main js on it. I am trying to register service worker on other website like https://example2.com.Till now what I have tried:I had created sub-domain like https://example2.example.com and put both main js and service worker js in that folder and registering service worker on 'example2.com' using this sub-domain like https://example2.example.com/serviceworker.js and https://example2.example.com/main.js but it is giving me error like:
SecurityError: The operation is insecure.
I have two questions from this conditions:
Is it possible to register service worker hosted on different domain ?
If possible then why is it giving me this error and how to do it ?
I am using only javascript to avoid any dependency.
Thank you in advance!

Their is one tricky way to register service worker cross-domain and almost every website doing so is by this way. You can register it by using pop-up window.Steps:
1.Create a new html page in your sub-domain directory and like https://example2.example.com/index.html2.Add your main.js in this index.html page.3.Now instead of loading main.js in https://example2.com open pop window with link https://example2.example.com/index.html
Once your service worker registered, their will be no domain dependency and you can do whatever you want to with this. In this way you can register service worker without redirecting.

Related

consequences of changing a Service Worker directory?

as you might already know, you should put your service workers inside the base directory so that it has the scope of the whole project, otherwise some features (e.g. navigator.serviceWorker.ready()) won't be available.
My problem is that I've initially put my service worker inside a directory and now I feel kinda trapped.
I use the service worker to register and handle web push notifications and I'm wondering if I would lose all my current subscriptions if I move it to the base directory
N.B. It's mandatory that the service worker registered will remain the same, no new service worker should be registered
If you don't want to move your service worker in base directory. You can use "Service-Worker-Allowed" header in your response. If this header is present then your service worker will be registered even if your sw.js file is in nested directory.
Service-Worker-Allowed header '/'
here the the official documentation regarding this header.
https://w3c.github.io/ServiceWorker/#service-worker-allowed

How Do I Make a Service Worker Compatible with Wildcard Subdomains?

I have a fairly simple question. I have a manifest.json file for my new service worker that lists "start_url" as "https://example.com" and the scope is "/". That works great unless the URL has a subdomain. In that case I get several errors saying that the manifest start url is not valid, it has been ignored, and that the manifest has no matching service worker.
The service worker still works but I would like to eliminate these errors. I use wildcard subdomains for all listings categorized by geographic location (ex: https://city-state.example.com). That lets me feather out the categories on the other side of the domain name (ex: https://city-state.example.com/category/subcategory). Is there a way to use something like https://(*).example.com for the start url or scope to avoid this error?
A service worker is scoped to a single origin and no higher in a file page than the level it is served from.
The rules are to provide security and prevent 3rd party scripts from attaching service workers to invade your site.
You will have to replicate your service worker on each origin. But honestly, unless the application is exactly the same you will want to customize the service worker logic to the specific application.

Send postmessage to ServiceWorker on AMP

I am in a tricky situation. I have a service worker in my page where I can init my cache with a postmessage (so on a new version, without any change on my service worker file, I can add or remove the file from my cache).
But here my problem, I need to do the same thing on amp version on my page. I figured out how to call and register my service worker, but couldn't find a way to make my postmessage call.
Here my question: is there any solution to make a post message call from an AMP page ?
if somebody as the same problematic here how I solve it :
No more postMessage, as it's impossible to do in AMP be i store data in a json file in my server and Service worker ask the file (without any cache) to get my data

Service worker file not found in offline mode

My web app is like this:
I have an Apache server running on port 80, with virtual hosts for my domain and different subdomains, including port 443 for SSL.
I have a nodejs application running on port 5000.
All traffic to my main domain is redirected using proxypass from apache to nodejs.
NodeJs then loads Service Workers.
Problem:
ON first load of application in online mode it works properly.Shows Service worker registered over the complete domain message as expected. Even for some css,js and img it return response fromServiceWorker.
But now when I open same page on offline mode service worker dont play it part to open the web app.
Error:
1. Uncaught (in promise) TypeError: Failed to fetch(…).
An unknown error occurred when fetching the script for service-worker file.
Any help on this regard to implement this properly will be be helpful.
Your browser will attempt to refetch the JavaScript file corresponding to the current service worker for every navigation request. You can read more about that in this Stack Overflow answer.
What you're seeing logged reflects the fact that the JavaScript for your service worker can't be fetch (which makes sense, because you're offline). What happens in that case is that the previous JavaScript that your browser already knows about and has cached will be reused, and your service worker should function as expected. The noise in the DevTools console about the service worker's JavaScript failing to be fetched can be ignored.
If you're seeing any failures related to the actual function of the service worker itself (like, retrieving cached resources are failing), then that would point to an issue with your implementation. But it doesn't sound like that's what's being logged.
Make sure the html files are cached, and that the service worker script is in the top level.

How do I load a Web Worker script over HTTPS?

I am attempting to use a Web Worker to offload some CPU intensive calculations into a separate thread. For a little context, I am taking an audio stream from getUserMedia and saving it into a file to be uploaded to my service after it is complete. I am able to retrieve the stream from the user and play it back via the WebAudio API and through an HTML5 player, but now I need to take the next step of saving it into a file.
The problem:
My main service is running over an HTTPS connection, since it is restricted to signed in users only. I have a worker script that does what I need it to, and I am attempting to load the script in via a relative path into my worker. I am receiving the following error
Mixed Content: The page at 'https://someurl.com:1081/some/path' was loaded over HTTPS,
but requested an insecure Worker script
'http://someurl.com/some/path/lib/assets/javascripts/worker.js'.
This request has been blocked; the content must be served over HTTPS.
I figured it was because I was using a relative path in my code like so:
worker = new Worker('lib/assets/javascripts/worker.js');
I wanted to rule this out so I made the following change:
worker = new Worker('https://someurl.com:1081/some/path/lib/assets/javascripts/worker.js');
This did not solve my error. It appears that the Worker is loading my script via HTTP no matter what url location I attempt to use. I couldn't find any reference on how to use the Web Worker via HTTPS, so I am hoping someone can provide some insight.
Possible Solution
I do want you to know there is a possible solution, but it seems a bit hacky to me. I can load my worker script up as a Blob and pass that directly into the Worker. If this is the only solution, I can make it work. But I was hoping to find a way to make the script load via HTTPS.
Have you tried
//someurl.com:1081/some/path/lib/assets/javascripts/worker.js
instead of
https://someurl.com:1081/some/path/lib/assets/javascripts/worker.js
Just something I found here,
Deezer content is served over HTTP
I solved this. The error itself was misleading and caused me to go down a rabbit hole looking for the solution.
The issue here actually stems from the way I have this service configured. The service that starts the web worker is actually proxied behind another service, and all requests go through the parent service. This works great for most requests, but was causing an error in this case. Instead of forwarding the request on this port to my app, the web worker was attempting to download the worker script from the parent service itself. This means the error stemmed from the fact that the script wasn't found, not that the protocol was incorrect.
To solve this, I had to pass in a localized script location from Rails using its asset pipeline. This allowed the worker to grab the script and actually work.

Categories

Resources