Send postmessage to ServiceWorker on AMP - javascript

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

Related

Does a service worker cache work site-wide, even without loading an HTML with sw.js?

Does a service worker cache work site-wide, regardless whether the user is actually on an HTML page that has sw.js loaded?
For example:
Assuming...
A registered service worker on the users’ browser
With a local cache controlled by the sw
The local cache has a few files cached including html, js, css and some jpg’s
If the user were to directly visit any of those JPG or CSS files in their browser, does the browser run the request through the service worker and/or on it’s own recognize those local files, and thus render the locally cached file?
One a service worker is activated, its fetch handler will be triggered whenever there is:
A navigation to a URL under the service worker's scope.
A request for a URL made by a client page that's under the service worker's scope. (Such as a request for an image, or stylesheet, etc.)
If you were to navigate directly to a URL that exists under the service worker's scope, even if that URL didn't correspond to an HTML document, the service worker's fetch handler would have a chance to respond.
If your service worker has a fetch handler that always responded with a cached version of a given URL, then yes, that response would be used to fulfill the navigation, even if the asset was an image or stylesheet or something else.
You can test this out for yourself:
Visit a site like https://images.guide/ that installs a service worker, caches a number of resources, and uses a fetch handler that responds to requests for precached resources via the cache.
Go offline.
Navigate to a URL that's precached, like https://images.guide/images/hamburger.svg
You'll see that the precached resource is used to respond to the navigation request.

Does it make sense saving a service worker in cache?

From my service worker, I am saving some assets in the browser cache, as well as the service worker script itself, and it works fine so that I can see the service worker url together with all other assets in my devtools cache tab.
Now, when I go offline, my service worker listens to the fetch event and gets all assets from cache.
However, there seems to be no fetch event when the page tries to register the worker itself, therefore I'm getting the following errors in the console:
console error
console erorr
Am I missing something? After all, does it make sense to cache the service-worker script itself?
According to the specification of the update algorithm (which is run for registering also). At the point 7.2:
Set request’s skip service worker flag and request’s redirect mode to "error".
That's mean your service worker request will never pass through the service worker. Instead, it is cached in its own cache according to its own rules. What you see as errors, are the failing attempts of the browser to get a fresh version of the service worker.
As Jeff Posnick says in one of his replies, you can safely ignore these errors.

Need to call data from insecure URL for GCM push notification for chrome

I need to call an insecure (http://) URL to fetch data form service worker file for GCM push notification for chrome. Is this possible anyway?
I did a long search and as #Jeff Posnick written in my another question's(how to register a service worker from different sub domain) answer. It seems for security reason it's not allowed by GCM team to call a insecure service in service worker or while working on GCM push notification for chrome.
Not to reboot this thread, but I stumbled upon this as well. Two solutions are creating a github repository (where you have a standard ssl-certificate) or getting a free certificate from startssl. For the last one, you might need permissions that are not standard in your hosting package.

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.

Can I do a 'PUT' ajax call to a local file similarly to how 'GET' works?

If I load an HTML from file:// and in the HTML there's an AJAX 'GET' request to a relative URL, it means the URL is pointing to a file and the file is loaded.
Can I have a similar behavior for 'PUT'? Meaning overwrite the file's content with the AJAX data?
I need this so I can easily debug scripts without the need to setup an HTTP server to host them and reply to the requested URLs.
Thank you
As far as I know, the HTML/script model in current browsers does not allow changing local resources (sort of a sandbox - think of the mess that would occur if an HTML page could write to a local file). The file:// is just a way to tell the browser a resource it's looking for is local to the machine it's running on, so it won't need to utilize the network. So you can read local resources from script - not change them.
Why not just run a local web server? You'll need it anyway eventually.
I don't think so. Where would the data post to and what would process it?
ExtJs has full REST on Ajax calls. Using the "core" release is free.
Check out their online documentation : http://extjs.com/products/extcore/manual/

Categories

Resources