Skip to content

Commit

Permalink
Rewrite /scope:.+/<URL> to just <URL>
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Pookie717 committed Oct 14, 2022
1 parent 7ab27be commit ff15c19
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions dist-web/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
AddType application/wasm .wasm

RewriteEngine on
RewriteRule ^scope:.*?/(.*)$ $1
22 changes: 22 additions & 0 deletions liveServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const liveServer = require('live-server');

liveServer.start({
port: 8777,
root: __dirname + '/dist-web',
open: '/wordpress-browser.html',
file: 'wordpress-browser.html',
middleware: [
(req, res, next) => {
if (req.url.startsWith('/scope:')) {
req.url = '/' + req.url.split('/').slice(2).join('/');
}
next();
},
],
});

liveServer.start({
port: 8778,
root: __dirname + '/dist-web',
open: false
});
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"dev:web": "npm-run-all --parallel dev:web:*",
"dev:web:app": "npm run build:web:app -- --watch",
"dev:web:html": "chokidar --initial --silent \"./src/web/*.html\" -c \"cp src/web/*.html dist-web/\"",
"dev:web:serve": "npx live-server ./dist-web --port=8777 --open=wordpress.html",
"dev:web:serve-iframe-worker": "npx live-server ./dist-web --port=8778 --no-browser",
"dev:web:serve": "node liveServer.js",
"dev:node": "node ./src/node/command.mjs",
"build": "npm run build:web",
"build:web": "npm-run-all --parallel build:web:*",
Expand Down

0 comments on commit ff15c19

Please sign in to comment.