Skip to content

[Service Worker] Support redirects to relative URLs in Safari#1978

Merged
adamziel merged 20 commits into
trunkfrom
fix/relative-redirects-in-safari
Nov 14, 2024
Merged

[Service Worker] Support redirects to relative URLs in Safari#1978
adamziel merged 20 commits into
trunkfrom
fix/relative-redirects-in-safari

Conversation

@bgrgicak
Copy link
Copy Markdown
Collaborator

@bgrgicak bgrgicak commented Nov 1, 2024

Before this PR when PHP tried to make a redirect (300 response) to a relative URL, Safari would return a network error and the new page wouldn't load in Playground, we would see a white screen.

This is caused by a Safari bug which prevents Safari from redirecting responses to relative URLs if the response comes from a Service Worker.

Until the bugfix is implemented in Safari, relative URLs can only be correctly redirected using Response.redirect.
This workaround was suggested in the bug report.

Fixes #645

Testing Instructions (or ideally a Blueprint)

Test this in Safari

  • Open the post editor
  • Click on the kebab menu (upper right corner)
  • Select Preferences from the menu
  • In the General tab toggle Custom fields to be active
  • Click on Show & Reload Page
  • Confirm the page reloads

Comment thread packages/playground/wordpress/src/index.ts
@bgrgicak bgrgicak self-assigned this Nov 1, 2024
@bgrgicak bgrgicak requested a review from a team November 1, 2024 09:52
@bgrgicak bgrgicak added [Type] Bug An existing feature does not function as intended [Aspect] Service Worker labels Nov 1, 2024
@bgrgicak bgrgicak changed the title Support redirects to relative URLs in Safari [Service Worker] Support redirects to relative URLs in Safari Nov 1, 2024
Comment thread packages/php-wasm/universal/src/lib/php-request-handler.ts Outdated
@bgrgicak bgrgicak marked this pull request as draft November 5, 2024 13:27
@bgrgicak bgrgicak marked this pull request as ready for review November 5, 2024 16:46
Comment thread packages/php-wasm/universal/src/lib/urls.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/php-request-handler.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/php-request-handler.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/urls.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/urls.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/urls.spec.ts Outdated
Copy link
Copy Markdown
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

Since this is fixing a fairly bothersome problem for Safari users, I'm going to make a couple of the changes I suggested in this review and merge. And we can follow up later if needed.

Comment thread packages/php-wasm/scopes/src/index.ts Outdated
Comment thread packages/php-wasm/scopes/src/index.ts Outdated
Copy link
Copy Markdown
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

Since this is fixing a fairly bothersome problem for Safari users, I'm going to make a couple of the changes I suggested in this review and merge. And we can follow up later if needed.

Scratch that. Let's wait to merge this. As I started implementing the aforementioned suggestions, it seemed more and more like maybe we needed to adjust our approach. So I left a couple of comments and will get @bgrgicak's opinion on those.

Comment thread packages/php-wasm/universal/src/lib/urls.ts Outdated
Comment thread packages/php-wasm/universal/src/lib/php-request-handler.ts Outdated
Comment thread packages/php-wasm/scopes/src/index.ts Outdated
@bgrgicak
Copy link
Copy Markdown
Collaborator Author

bgrgicak commented Nov 8, 2024

There is a reply on the WebKit bug about using Response.redirect to work around the issue.

I was able to make it work by adding a Response.redirect to the PHP-wasm Service Worker.

Redirect in Remote

I took a look at it in this branch, but it didn't work, the 302 response locations are messed up (see screenshot).

This is because it's to late to do the redirect there.

Redirects

  • Location: http://127.0.0.1:5400/ - Location: /scope:123/wp-admin/ was sent by PHP
  • Location: /website-server/ - this is because the previous location can't be correctly resolved in our local environment

Screenshot 2024-11-08 at 12 27 45

@bgrgicak bgrgicak marked this pull request as draft November 8, 2024 11:46
Comment thread packages/php-wasm/universal/src/lib/php-request-handler.ts Outdated
Comment on lines +66 to +89
/**
* Safari has a bug that prevents Service Workers from redirecting relative URLs.
* When attempting to redirect to a relative URL, the network request will return an error.
* See the Webkit bug for details: https://bugs.webkit.org/show_bug.cgi?id=282427.
*
* Because PHP and WordPress can redirect to both relative and absolute URLs
* using the `location` header we need to ensure redirects are processed
* correctly by the Service Worker.
*
* As a workaround for Safari Service Workers, we convert the `location` header
* to an absolute URL for all redirect responses (300-399 status codes).
* This resolves the issue because Safari correctly handles redirects
* to absolute URLs provided by the `location` header.
*/
if (
phpResponse.httpStatusCode >= 300 &&
phpResponse.httpStatusCode <= 399 &&
phpResponse.headers['location']
) {
return Response.redirect(
phpResponse.headers['location'][0],
phpResponse.httpStatusCode
);
}
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@brandonpayton @adamziel This is now the new fix for the Safari issue. I closed all the outdated threads that aren't relevant anymore.

This code is ready for another review.

@bgrgicak bgrgicak marked this pull request as ready for review November 8, 2024 12:00
@bgrgicak bgrgicak requested a review from adamziel November 8, 2024 12:05
@adamziel
Copy link
Copy Markdown
Collaborator

This looks good and I think we can merge. I'm just confused about this part of the PR description:

Because Safari can correctly redirect responses to absolute URLs we are using Response.redirect to redirect PHP responses.
This workaround was suggested in the bug report.

The redirection URL still can be relative even with Response.redirect. Is that fine?

@bgrgicak
Copy link
Copy Markdown
Collaborator Author

Until the bugfix is implemented in Safari, relative URLs can only be correctly redirected using Response.redirect.
This workaround was suggested in the bug report.

@adamziel maybe something like this?

@adamziel adamziel merged commit 5746dfc into trunk Nov 14, 2024
@adamziel adamziel deleted the fix/relative-redirects-in-safari branch November 14, 2024 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Aspect] Service Worker [Type] Bug An existing feature does not function as intended

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

Error enabling "Custom fields" panel in post editor preferences

3 participants