-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scoped WordPress instances to support multiple browser tabs #31
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…he `scope` feature of service workers. Scoping service workers only leads to registering dozens of workers and a hard to untangle mess.
adamziel
changed the title
Scoped WordPress instances
Scoped WordPress instances to support multiple browser tabs
Oct 13, 2022
This was referenced Oct 13, 2022
adamziel
added a commit
that referenced
this pull request
Oct 14, 2022
Related to #42 This commit implements the URL rewrite with: * A liveServer middleware for local dev * A .htaccess file for apache production setups Documenting this behavior in a visible place will be critical for mass adoption of this project. Nested iframes with src="about:srcdoc" are not controlled by the parent's frame ServiceWorker. This is a bug in Chrome and potentially other Chromium-based browser. This is problematic because WordPress site editor is rendered in one such iframe and it loads a bunch of stylesheets using a link tag like: <link rel="stylesheet" href="/scope:<scope>/<URL>" /> The /scope:<scope>/ part of the URL does not actually exist on the server – it's a WordPress instance scope introduced to make WASM WordPress usable in multiple browser tabs. Learn more at #31 The point is – these stylesheet requests bypass the ServiceWorker and are actually sent to the server where they get a 404 response. This effectively breaks the site editor. We cannot re-route these requests in a ServiceWorker, but we can do it on the server. That's what this commit is. Unfortunately this commit won't solve the problem in setups based on nginx and other server software – let's follow it up with a documentation update.
adamziel
added a commit
that referenced
this pull request
Oct 14, 2022
Related to #42 This commit implements the URL rewrite with: * A liveServer middleware for local dev * A .htaccess file for apache production setups Documenting this behavior in a visible place will be critical for mass adoption of this project. Nested iframes with src="about:srcdoc" are not controlled by the parent's frame ServiceWorker. This is a bug in Chrome and potentially other Chromium-based browser. This is problematic because WordPress site editor is rendered in one such iframe and it loads a bunch of stylesheets using a link tag like: <link rel="stylesheet" href="/scope:<scope>/<URL>" /> The /scope:<scope>/ part of the URL does not actually exist on the server – it's a WordPress instance scope introduced to make WASM WordPress usable in multiple browser tabs. Learn more at #31 The point is – these stylesheet requests bypass the ServiceWorker and are actually sent to the server where they get a 404 response. This effectively breaks the site editor. We cannot re-route these requests in a ServiceWorker, but we can do it on the server. That's what this commit is. Unfortunately this commit won't solve the problem in setups based on nginx and other server software – let's follow it up with a documentation update.
Pookie717
added a commit
to Pookie717/wordpress-playground
that referenced
this pull request
Oct 1, 2023
Related to WordPress/wordpress-playground#42 This commit implements the URL rewrite with: * A liveServer middleware for local dev * A .htaccess file for apache production setups Documenting this behavior in a visible place will be critical for mass adoption of this project. Nested iframes with src="about:srcdoc" are not controlled by the parent's frame ServiceWorker. This is a bug in Chrome and potentially other Chromium-based browser. This is problematic because WordPress site editor is rendered in one such iframe and it loads a bunch of stylesheets using a link tag like: <link rel="stylesheet" href="/scope:<scope>/<URL>" /> The /scope:<scope>/ part of the URL does not actually exist on the server – it's a WordPress instance scope introduced to make WASM WordPress usable in multiple browser tabs. Learn more at WordPress/wordpress-playground#31 The point is – these stylesheet requests bypass the ServiceWorker and are actually sent to the server where they get a 404 response. This effectively breaks the site editor. We cannot re-route these requests in a ServiceWorker, but we can do it on the server. That's what this commit is. Unfortunately this commit won't solve the problem in setups based on nginx and other server software – let's follow it up with a documentation update.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What problem does this PR solve?
Adds support for running WASM WordPress in multiple browser tabs and solves #9.
All WordPress requests are routed through a single service worker shared between all browser tabs. The request lifecycle looks as follows:
It's a back-and-forth conversation between a specific browser tab and the service worker.
Unfortunately, Service workers communicate with tabs using a
BroadcastChannel
– it's a messaging strategy that routes every message to every listener. As a result, each WordPress request was rendered in every tab, often causing unexpected behaviors.How does this PR propose to solve it?
This PR introduces a concept of WordPress
scope
and enables the service worker to post BroadcastChannel messages scoped to specific listeners.Scoping a WordPress instance means installing it at a unique pathname starting with
/scope:<unique number>
. For example:/wp-login.php
would be available athttp://localhost:8778/wp-login.php
/wp-login.php
would be available athttp://localhost:8778/scope:96253/wp-login.php
The scope number is a random and unique number generated by a specific browser tab. The service worker is aware of this concept and will use any
/scope:
found in the request URL to tag all the relatedBroadcastChannel
communication. The WASM workers running in specific browser tabs will then ignore all theBroadcastChannel
communication with an unfamiliarscope
attached.Alternatives considered
scope
feature of ServiceWorker – it led to multiple worker registrations and was hard to reason about.sw.js?tab_id=432
orsw-1.js
– it led to the same problems as relying on thescope
feature.w87953.localhost
– it would require setting up a catch-all DNS domain to even use this project. That's a steep barrier of entry.How to test?
Run
npm run dev
and open the WASM WordPress page in a few different browser tabs. Log in, create pages, play with it, and confirm that each tab behaves as expected. Specifically:Follow-up work
sessionStorage
to preserve the scope across page reloads.