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

Serve file as PHP if a static file wasn't found #1203

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 46 additions & 14 deletions packages/php-wasm/universal/src/lib/php-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,53 @@ export class PHPRequestHandler implements RequestHandler {
this.rewriteRules
);
const fsPath = `${this.#DOCROOT}${normalizedRequestedPath}`;
if (seemsLikeAPHPRequestHandlerPath(fsPath)) {
return await this.#dispatchToPHP(request, requestedUrl);
if (this.#isStaticFile(fsPath)) {
return this.#serveStaticFile(fsPath);
} else if (this.#isStaticWpCoreFile(fsPath)) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have WordPress-specific code in PHP-wasm but I'm not sure how to resolve this.

I think that the best solution would be to catch this in service-worker and not even trigger the request handler.

The problem is that service-worker can't check if the file exists, so we would need to have a list of available core files.

// Serve static WordPress core files as 404s to trigger a real fetch() from the server.
return this.#serveStatic404();
}
return this.#serveStaticFile(fsPath);

const phpResponse = await this.#dispatchToPHP(request, requestedUrl);
if (
phpResponse.httpStatusCode === 404 &&
!seemsLikeAPHPRequestHandlerPath(fsPath)
) {
return this.#serveStatic404();
}
return phpResponse;
}

#serveStatic404(): PHPResponse {
return new PHPResponse(
404,
// Let the service worker know that no static file was found
// and that it's okay to issue a real fetch() to the server.
{
'x-file-type': ['static'],
},
new TextEncoder().encode('404 File not found')
);
}

#isStaticFile(fsPath: string): boolean {
return (
!seemsLikeAPHPRequestHandlerPath(fsPath) &&
this.php.fileExists(fsPath)
);
}

#isStaticWpCoreFile(fsPath: string): boolean {
if (fsPath.endsWith('.php')) {
return false;
}
if (fsPath.endsWith('/')) {
return false;
}
return (
fsPath.startsWith(`${this.#DOCROOT}/wp-includes/`) ||
fsPath.startsWith(`${this.#DOCROOT}/wp-admin/`)
);
}

/**
Expand All @@ -142,17 +185,6 @@ export class PHPRequestHandler implements RequestHandler {
* @returns The response.
*/
#serveStaticFile(fsPath: string): PHPResponse {
if (!this.php.fileExists(fsPath)) {
return new PHPResponse(
404,
// Let the service worker know that no static file was found
// and that it's okay to issue a real fetch() to the server.
{
'x-file-type': ['static'],
},
new TextEncoder().encode('404 File not found')
);
}
const arrayBuffer = this.php.readFileAsBuffer(fsPath);
return new PHPResponse(
200,
Expand Down