News for hackers. Stuff that matters.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Sunday October 16, 2022 @10:47PM (#33229386)
The demo also demonstrates why something like this is insufficient: you can’t rely on the service worker loading. Service workers must be optional. There’s a reason invocations always start by checking if navigator.serviceWorker even exists, and why navigator.serviceWorker.register() returns a Promise, and why the ServiceWorkerRegistration type is comparatively complicated: service workers aren’t permitted in all contexts, and even when they are, they’re installed asynchronously in the background after the page finishes loading.

To show this easily, try this in a Firefox Private Browsing window: service workers are disabled there, so the request goes through to the server, which responds 405 Method Not Allowed.

So in order to make this approach reliable, you’ll need to run that code on your server, not just in the service worker. This is totally viable, but an approach that has seen surprisingly little experimentation. (I myself have a grand scheme that’s actually fairly fleshed out, but I’ve only touched it once in the last year and a half, so don’t hold your breath. My scheme is predicated upon aggressively not requiring client-side scripting, but using it only for completely optional enhancement; and upon running the same source code in a worker, in a service worker, at the edge or on the origin server, with only a teeny bit of JavaScript on the main thread.)

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by thagsimmons on Sunday October 16, 2022 @11:19PM (#33229521)
This is too negative - it's fine to require service workers if it's justified. Service workers work in private browsing mode in Safari, Chrome and Brave, and is only unimplemented in Firefox because it "hasn't been a priority yet":

https://bugzilla.mozilla.org/show_bug.cgi?id=1320796

Firefox will eventually catch up. The possibilities for this approach are actually quite interesting, and go far beyond the much less useful scheme you outlined that relegates it strictly to just being an optimization.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Sunday October 16, 2022 @11:30PM (#33229575)
I used Firefox’s Private Browsing purely as an easy way of demonstrating the problem. A forced reload (e.g. Ctrl+Shift+R) is another way of showing it, and one that’s defined in the spec.

I’ll tweak my statement slightly: for regular site functionality, service workers must be optional. Depending on service workers for anything that does not inherently require them (like background sync or receiving push messages) is bad.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by eyelidlessness on Monday October 17, 2022 @12:05AM (#33229717)
> Depending on service workers for anything that does not inherently require them (like background sync or receiving push messages) is bad.

Or access to CacheStorage, and acting as an offline server when the network is unavailable. Perfectly reasonable to depend on if your use case requires it.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by thagsimmons on Monday October 17, 2022 @12:08AM (#33229734)
Again, this is much too sweeping and categorical. The forced reload problem with service workers is well known and easy to work around. The Firefox private browsing example is neither here nor there - an implementation weakness that has no good rationale and will eventually go away. If your functionality justifies it, you should feel free to use and depend on service workers. If service workers are not present, you should feel free to display an error explaining it to the user. Many things we commonly depend on are optional in corner cases, but that doesn't hold us back.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by skybrian on Monday October 17, 2022 @12:44AM (#33229886)
I think a better way to say it is that, for ordinary, general purpose websites, it's unlikely that such a complicated architecture would be in any way superior to a regular web page implemented using a more ordinary architecture, and there are downsides.

But that doesn't rule it out in some special case. Special cases are special. (I recently wrote a Chrome-only web app because it requires access to the serial port.)

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by thagsimmons on Monday October 17, 2022 @01:02AM (#33229972)
I don't think this is significantly different from what I said ("if your functionality justifies it..."). That is, evaluate service workers in the same way that you would any other complicated additional requirement or feature.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Monday October 17, 2022 @12:44AM (#33229887)
I’m curious how you mean to work around the forced reload problem, because the way I’m looking at it, it’s impossible to work around, by design.

I say: feel free to use service workers, but do not feel free to depend on service workers. They must be optional in general-purpose websites.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by thagsimmons on Monday October 17, 2022 @01:25AM (#33230040)
Very few things are impossible to work around. In this case, you can detect that the service worker is no longer controlling the page, and do something about it: re-register the worker, soft reload the page, etc. I've also seen people do dodgier things like trying to intercept hard reloads, but that's not necessary.

At any rate, this is more of a StackOverflow question than an HN question, and the details of what works will differ from project to project.

https://stackoverflow.com/questions/51597231/register-servic...

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by onion2k on Monday October 17, 2022 @03:11AM (#33230599)
it's fine to require service workers if it's justified

I don't agree with this because you're essentially defending writing websites and apps for specific browsers. "Oh, this doesn't work in your browser because it requires [specific tech|devs to test in more than one browser|whatever]" is a lame excuse in a world where progressive enhancement has been an accepted approach to developing web software for twenty years.

As a tech demo to show something is possible it's fair enough, but in the real world this shouldn't ever happen.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by toqy on Monday October 17, 2022 @09:31AM (#33233195)
In this example where Firefox is the outlier, if you write your web app differently than you otherwise would have then you'd be writing websites and apps for a specific browser...
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by riedel on Monday October 17, 2022 @05:23AM (#33231368)
Cool demo. Like with any PWA, it would be just very gret if all that stuff would be really progressive. Run in the browser for the 90% and render real HTML (that might even work with CSS) for the rest. IMHO frameworks that fallback to accessible HTML for server rendering in a deterministic fashion would be awesome. I personally find the argument that ist works for most not very ethically sustainable...
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by cash22 on Sunday October 16, 2022 @10:52PM (#33229415)
What contexts are they not permitted? I know that not all browsers support them, but are there other limitations?
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Sunday October 16, 2022 @11:22PM (#33229532)
One particularly interesting context here is that force reloading deliberately bypasses service workers. Try pressing Shift+F5 or Ctrl+Shift+R, and all of a sudden the button stops working until you do a regular, non-forced reload.

Beyond that, I don’t know, but I would expect things like some lower-powered and -storaged devices rejecting service workers, and maybe privacy modes too, though in that case they could possibly be allowed with just background stuff neutered.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by jhugo on Sunday October 16, 2022 @11:45PM (#33229634)
Given that the spec requires Service Workers to be optional, future Web specs & implementations may build upon that requirement, so writing your software to assume they always load is both brittle today and subject to unexpected breakage in future.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Monday October 17, 2022 @01:14AM (#33230010)
De jure they’re optional, but if they are implemented everywhere or close enough to everywhere, they will de facto become mandatory, as has happened many times before. This is a large part of the reason why a large part of me wishes browsers wouldn’t provide service workers in Private Browsing/Incognito/whatever modes.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by panzerboiler on Monday October 17, 2022 @02:11AM (#33230254)
I really don't understand your point. Since ServiceWorkers provide an offline experience and can be inspected from the client, they should be better than no ServiceWorker with respect to privacy.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Monday October 17, 2022 @03:09AM (#33230583)
The main purposes of service workers are offline support and persistent background stuff. These are typically irrelevant in Private Browsing windows, which are designed for short-term use, and so service workers are almost always pure waste. (Not always, but almost always, and in line with the intended purpose and semantics of Private Browsing windows, though you can certainly use it in somewhat different ways.)

It’s similar to a large part of the reason why Local Storage and Indexed DB were historically disabled in Private Browsing: why store stuff when it’s only going to be deleted immediately?

Browsers have mostly headed in the direction of removing distinguishing features from their Private Browsing modes, partly to thwart detection of that mode, and partly because many things started to carelessly depend on the gated functionality. Firefox still doesn’t allow writing to Indexed DB (and only made Local Storage work far later than Chromium), and I’ve definitely encountered sites that just assumed it would work, and so fell over in early initialisation.

And so for this specific subthread: it’s ossification at work. If something is supposed to be able to change but never actually changes, you find when you try to change it that you can’t do so any more because people have built too much stuff that assumed it wouldn’t change.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by panzerboiler on Monday October 17, 2022 @04:15AM (#33230948)
I don't agree that private browsing is for short-term use. You can use a temporary storage for how long it is needed (in my case up until the operating system or the browser crashes, aka almost never).
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by chrismorgan on Monday October 17, 2022 @06:20AM (#33231649)
It is designed for that.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by panzerboiler on Monday October 17, 2022 @07:30AM (#33232130)
Where can I find such design specification? All I can find is https://w3ctag.github.io/private-mode/ and https://www.w3.org/2001/tag/doc/private-browsing-modes/ that tell nothing about the short duration of a private session. Some emphasis is put on the necessity that private mode and normal mode should be transparent to the user (i.e. not breaking any normal feature). It should be not possible to tell the two modes apart. If you break a feature like ServiceWorker you are providing a way to detect a private browsing session, and that is a bad thing, worse than anything a SW could provide, in my opinion.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nabakin on Monday October 17, 2022 @12:49AM (#33229915)
I'm more optimistic about using Service Workers in this way, but I agree this was definitely not their intended use case so there will be problems.

There's another flaw I see with using Service Workers in this way. After a period of time where the Service Worker is inactive (a minute or so on Firefox), the browser will shutdown the Service Worker. When it shuts down, all variables in the Service Worker scope are freed and I assume the WebAssembly server instance as well. How would you maintain server state, in this scenario?

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by madsbuch on Monday October 17, 2022 @07:33AM (#33232155)
Commit to a browser persistent storage or skip it off to a server.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nabakin on Monday October 17, 2022 @11:58AM (#33235401)
That's what I'm thinking too. There's no way to keep the state in Rust which means constantly updating a browser persistent storage through JS or keeping state client side in the tab's environment. A pretty janky workaround
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nymanjon on Monday October 24, 2022 @02:18PM (#33320320)
I pretty much build my personal offline-first web apps this way, but with JS.

https://news.ycombinator.com/item?id=33319875

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nabakin on Tuesday October 25, 2022 @11:45PM (#33339581)
Cool! If I'm reading your code correctly, you store service worker state in the cache?
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nymanjon on Wednesday October 26, 2022 @06:55PM (#33350719)
Yeah, I store everything in IndexedDB using a simple Key-Value abstraction library. I have a cache where I store any temporary state between pages.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by koonsolo on Monday October 17, 2022 @03:30AM (#33230708)
Firefox is running behind on a lot of the latest features. And considering their market share, sometimes it's just less effort to ignore its limitations.
Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by buro9 on Monday October 17, 2022 @04:21AM (#33230992)
I RTFA first and couldn't get it working, came here and saw your comment and now know why it doesn't work.

I may be a minority weirdo, but I have set dom.serviceWorkers.enabled to false.

Why? Because legit sites aren't broken by this and I always clear all browsers cache and databases on close (several times per day). IMHO service workers are used mostly for long-lived tracking, to supercede the cookie, and I also dislike background processing for things I just don't care about.

So for me... service workers are disabled. They just don't work. And it turns out the web works just fine.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by yunohn on Monday October 17, 2022 @06:22AM (#33231660)
You read the article titled “… Service Workers…” and tried getting it working on a device, that you explicitly disabled Service Workers because of your strong opinion on them?

I’m… not sure what to say.

Re:Htmx, WebAssembly, Rust, ServiceWorker Proof of Concept by nymanjon on Monday October 24, 2022 @01:45PM (#33319875)
I've created an offline-first web app which is based on service workers. I've created another one that could be pushed to the back end (like on Node.js) that would be just a straight MPA app. I guess both of them could be pushed to the back end if needed. Since I just use them for myself I don't worry about them not working without JS enabled. But I created HTMF, similar to HTMX but made to be a progressive enhancement from the get-go.

https://github.com/jon49/Soccer

https://github.com/jon49/WeightTracker

https://github.com/jon49/MealPlanner

https://github.com/jon49/htmf