Skip to content
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

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

Closed
adamziel opened this issue Feb 26, 2024 · 5 comments · Fixed by #1072
Closed

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

adamziel opened this issue Feb 26, 2024 · 5 comments · Fixed by #1072
Assignees
Labels
[Aspect] Browser [Type] Bug An existing feature does not function as intended

Comments

@adamziel
Copy link
Collaborator

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

@adamziel adamziel added [Type] Bug An existing feature does not function as intended [Aspect] Browser JS API labels Feb 26, 2024
@adamziel adamziel changed the title Apply URL rewriting rules in the request handler, not in the service worker Move URL rewriting from the service worker to the request handler Feb 26, 2024
@bgrgicak
Copy link
Collaborator

Makes sense to me. We should rewrite all requests the same way htaccess does it.
@adamziel feel free to assign this one to me.

@bgrgicak
Copy link
Collaborator

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

I wouldn't replace PHPRequestHandler. Adding rewrites similarly to what we have today should be enough.

@adamziel
Copy link
Collaborator Author

Adding rewrites similarly to what we have today should be enough.

@bgrgicak I wouldn't add WordPress-specific logic to any @php-wasm package, but perhaps the generic PHPRequestHandler could accept an option like rewrite: (url: string, php: BasePHP) => string and then the WordPress package would just ship a WordPress-specific rewrite handler.

@adamziel adamziel added this to the Zero Crashes milestone Feb 29, 2024
@bgrgicak bgrgicak moved this to In review in Playground Board Mar 1, 2024
@adamziel adamziel reopened this Mar 1, 2024
@github-project-automation github-project-automation bot moved this from In review to Done in Playground Board Mar 1, 2024
@adamziel
Copy link
Collaborator Author

adamziel commented Mar 4, 2024

Surfacing this comment from a related PR:

My follow-up question would be: do we still need this code?

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;
}
}
}

If yes, then #1054 is still relevant and we'll still need to support that logic at the request handler level, otherwise the service worker will rewrite multisite URLs correctly, but other PHP.wasm consumers like WP-NOW and VS Code won't.

If not, then #1054 is indeed solved.

@bgrgicak
Copy link
Collaborator

bgrgicak commented Mar 5, 2024

@adamziel this code isn't needed. I removed it in this PR #1083

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Aspect] Browser [Type] Bug An existing feature does not function as intended
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants