Skip to content

Move URL rewriting from the service worker to the request handler #1054

@adamziel

Description

@adamziel

Requests resolved directly via the JS API (playground.request()) return error 404 if URL rewriting is required to resolve them. For example, playground.request({ url: '/glotpress/projects }); wouldn't get resolved as /index.php/glotpress/projects. This is because Playground simulates the required .htaccess rules in the service worker:

if (workerResponse.status === 404) {
for (const url of rewriteWordPressUrl(unscopedUrl, scope!)) {
rewrittenUrlString = url.toString();
workerResponse = await convertFetchEventToPHPRequest(
await cloneFetchEvent(event, rewrittenUrlString)
);
if (
workerResponse.status !== 404 ||
workerResponse.headers.get('x-file-type') === 'static'
) {
break;
}
}
}

And only fetch() requests are affected. Instead, Playground should apply the following .htaccess rules in the request handler:

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Since PHPRequestHandler is generic and specific to @php-wasm, Playground should provide its own WordPress-specific handler, perhaps via the @wp-playground/wordpress package?

cc @akirk @bgrgicak

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions