WordPress / wordpress-playground

Run WordPress in the browser via WebAssembly PHP
https://w.org/playground/
GNU General Public License v2.0
1.63k stars 251 forks source link

Offline mode doesn't work in Firefox #1645

Open bgrgicak opened 3 months ago

bgrgicak commented 3 months ago

Offline mode works in Chrome and Safari, but throws an error in Firefox.

TypeError: NetworkError when attempting to fetch resource.

Recreate

brandonpayton commented 3 months ago

The failure I'm seeing is for:

https://www.googletagmanager.com/gtag/js?id=G-SOME-IDENTIFIER

Screenshot 2024-07-24 at 6 41 12 PM

brandonpayton commented 3 months ago

The failure I'm seeing is for:

https://www.googletagmanager.com/gtag/js?id=G-SOME-IDENTIFIER

That may be a red herring. After locally removing the index.html dependency on googletagmanager.com and confirming it is no longer used by index.html in the browser, the error still occurs.

brandonpayton commented 3 months ago

@bgrgicak, I wanted to know more about the Offline work and spent more time looking at this, setting fetch breakpoints in the service worker and in the main page dev tools, and comparing request URLs with those satisfied from the cache. AFAICT, there don't appear to be any files not found in the cache.

Those logs are here. There are 28 entries, 14 fetches and 14 corresponding cache hits. And in the dev tools Network tab, there were 14 requests logged. I didn't notice any differences between the 14 file names but did not confirm entry by entry.

I also spent time trying to find the origin of the "NetworkError when attempting to fetch resource" error but haven't had any luck. I was eager to help out and have a fix waiting as you started your day, but it didn't work out. :) I'm curious what you will find here. It will be super interesting to learn the source of this issue.

bgrgicak commented 3 months ago

That may be a red herring. After locally removing the index.html dependency on googletagmanager.com and confirming it is no longer used by index.html in the browser, the error still occurs.

It's ok for GTM to fail. It also fails in Chrome because we don't cache it.

bgrgicak commented 3 months ago

I was eager to help out and have a fix waiting as you started your day, but it didn't work out. :)

Thank you! It felt really good seeing that you helped. 🙂

bgrgicak commented 3 months ago

When I check the network requests while offline and compare it to online, it stops before requesting the SQLite plugin.

This plugin isn't preached, so I assume that there is something different in how FF fetches these files from cache.

I can see the file in cache.

When online the file is fetched from the server and not returned by the service worker. In Chrome it's returned by the worker.

brandonpayton commented 3 months ago

Ah, good call, comparing between online and offline requests.

I poked at this a bit more at the end of the day but unfortunately don't have more to share.

adamziel commented 2 months ago

It seems like all fetch() requests in the web worker fail when offline:

CleanShot 2024-07-29 at 11 55 50@2x CleanShot 2024-07-29 at 11 55 43@2x

I'm able to open both these files in a new tab, but somehow firefox cannot fetch them at the time the service worker requests them.

Two more issues I found in Firefox:

CleanShot 2024-07-29 at 11 56 57@2x

All of these seem like Firefox bugs. Perhaps we can work around them by poking around the worker lifecycle or claim() calls.

Aha, I am actually turning my wifi off. The "Work Offline" mode seems to be different from actually being offline so I am not using it.

Also, it is so weird this works on a localhost but not on playground.wordpress.net 🤔

adamziel commented 2 months ago

I wonder if these fetch() requests even make it to the service worker. It seems like that can only be debugged in Firefox nightly:

https://firefox-source-docs.mozilla.org/devtools-user/application/service_workers/index.html#debugging-service-workers

adamziel commented 2 months ago

One problem seems to be related to the service worker lifecycle. I'm testing on localhost as follows:

  1. Start the Playground server (php -S 127.0.0.1:9996 -t dist/packages/playground/wasm-wordpress-net)
  2. Open it in firefox
  3. Refresh the page
  4. Close the tab
  5. Stop the Playground server
  6. Go to 127.0.0.1:9996

This is what I see on the first page load:

CleanShot 2024-07-29 at 12 22 40@2x

If I navigate to admin and then back to the homepage, the CSS is there. This is what we've seen in Chrome before https://github.com/WordPress/wordpress-playground/pull/1643. It doesn't seem to be kicking in in Firefox.

This may or may not be related to the larger issue we're seeing on playground.wordpress.net.

adamziel commented 2 months ago

Idea for a short debugging feedback loop:

  1. Create a local https:// setup
  2. Serve the build Playground through that https domain
  3. Hope it would break in the same way as playground.wordpress.net does
  4. Edit the built files directly to avoid long waiting for the build

Alternatively, we could start a staging environment and do that there.

brandonpayton commented 2 months ago

I wonder if these fetch() requests even make it to the service worker. It seems like that can only be debugged in Firefox nightly:

https://firefox-source-docs.mozilla.org/devtools-user/application/service_workers/index.html#debugging-service-workers

@adamziel It's possible to debug Service Workers in Firefox today using dedicated dev tools opened via this page: about:debugging#/runtime/this-firefox. But debugging via web page dev tools (as supported by Firefox Nightly) looks like a huge UX improvement.

Screenshot 2024-07-29 at 8 00 03 AM

brandonpayton commented 2 months ago

Taking this one at @bgrgicak's request