Skip to content

Commit 3ada007

Browse files
authored
Merge pull request #11688 from nanaya/turbo-redirect-pt2
Fix turbo redirect
2 parents 6d0d60f + b9bc763 commit 3ada007

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

app/helpers.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,17 @@ function page_title()
942942
function ujs_redirect($url, $status = 200)
943943
{
944944
$request = Request::instance();
945-
if ($request->headers->get('x-turbo-request-id') !== null || ($request->ajax() && !$request->isMethod('get'))) {
945+
// This is done mainly to work around fetch ignoring/removing anchor from page redirect.
946+
// Reference: https://github.com/hotwired/turbo/issues/211
947+
if ($request->headers->get('x-turbo-request-id') !== null) {
948+
if ($status === 200 && $request->getMethod() !== 'GET') {
949+
// Turbo doesn't like 200 response on non-GET requests.
950+
// Reference: https://github.com/hotwired/turbo/issues/22
951+
$status = 201;
952+
}
953+
954+
return response($url, $status, ['content-type' => 'text/osu-turbo-redirect']);
955+
} elseif ($request->ajax() && $request->getMethod() !== 'GET') {
946956
return ext_view('layout.ujs-redirect', compact('url'), 'js', $status);
947957
} else {
948958
// because non-3xx redirects make no sense.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/bootstrap": "^3.3.0",
1919
"@types/cloudflare-turnstile": "^0.1.5",
2020
"@types/d3": "^7.1.0",
21-
"@types/hotwired__turbo": "^8.0.1",
21+
"@types/hotwired__turbo": "^8.0.2",
2222
"@types/is-hotkey": "^0.1.1",
2323
"@types/jasmine": "^3.3.13",
2424
"@types/jquery": "^3.3.0",

resources/js/setup-turbo.ts

+18
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ document.addEventListener('turbo:submit-end', (e) => {
2424
}
2525
});
2626

27+
document.addEventListener('turbo:before-fetch-response', (e) => {
28+
if (!e.detail.fetchResponse.contentType?.match(/^text\/osu-turbo-redirect[ ;]*/)) {
29+
return;
30+
}
31+
32+
e.preventDefault();
33+
e.detail.fetchResponse.responseText.then((url) => {
34+
const [currentUrlBase, urlBase] = [document.location.href, url].map((u) => u.replace(/#.*/, ''));
35+
36+
if (currentUrlBase === urlBase) {
37+
// a normal/advance visit to same url won't reload the page
38+
Turbo.visit(url, { action: 'replace' });
39+
} else {
40+
Turbo.visit(url);
41+
}
42+
});
43+
});
44+
2745
// disable turbo navigation for old webs
2846
document.addEventListener('turbo:click', (event) => {
2947
const url = new URL(event.detail.url);

yarn.lock

+22-8
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,10 @@
658658
dependencies:
659659
"@types/unist" "*"
660660

661-
"@types/hotwired__turbo@^8.0.1":
662-
version "8.0.1"
663-
resolved "https://registry.yarnpkg.com/@types/hotwired__turbo/-/hotwired__turbo-8.0.1.tgz#ac0605f3fd4e725f71101be02d2e9d5314afa669"
664-
integrity sha512-EloDlDDxlicl22+gFc77f7j16b8nCUvy6iyCtpICTlofAJfMCfnXNC8nYXCnVApes3BtWklAc2f3RKQgpaaFRQ==
661+
"@types/hotwired__turbo@^8.0.2":
662+
version "8.0.2"
663+
resolved "https://registry.yarnpkg.com/@types/hotwired__turbo/-/hotwired__turbo-8.0.2.tgz#db683460bb32a21715a5cf1f07a7de81d1c3124a"
664+
integrity sha512-fFWI/JNSTVKTPliSOV4fdeC3Kt3FUTbRYkvtF7QPCkqR51+AJWIiX0T5sJXwUnjL9j43tzfBXPZ2jEsBqw8/Bg==
665665

666666
"@types/is-hotkey@^0.1.1":
667667
version "0.1.1"
@@ -7122,8 +7122,7 @@ string_decoder@^1.1.1, string_decoder@^1.3.0:
71227122
dependencies:
71237123
safe-buffer "~5.2.0"
71247124

7125-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
7126-
name strip-ansi-cjs
7125+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
71277126
version "6.0.1"
71287127
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
71297128
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -7144,6 +7143,13 @@ strip-ansi@^4.0.0:
71447143
dependencies:
71457144
ansi-regex "^3.0.0"
71467145

7146+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
7147+
version "6.0.1"
7148+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
7149+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
7150+
dependencies:
7151+
ansi-regex "^5.0.1"
7152+
71477153
strip-ansi@^7.0.1:
71487154
version "7.1.0"
71497155
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-7.1.0.tgz#d5b6568ca689d8561370b0707685d22434faff45"
@@ -7813,8 +7819,7 @@ word-wrap@^1.2.3:
78137819
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.4.tgz#cb4b50ec9aca570abd1f52f33cd45b6c61739a9f"
78147820
integrity sha512-2V81OA4ugVo5pRo46hAoD2ivUJx8jXmWXfUkY4KFNw0hEptvN0QfH3K4nHiwzGeKl5rFKedV48QVoqYavy4YpA==
78157821

7816-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
7817-
name wrap-ansi-cjs
7822+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
78187823
version "7.0.0"
78197824
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
78207825
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
@@ -7831,6 +7836,15 @@ wrap-ansi@^2.0.0:
78317836
string-width "^1.0.1"
78327837
strip-ansi "^3.0.1"
78337838

7839+
wrap-ansi@^7.0.0:
7840+
version "7.0.0"
7841+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
7842+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
7843+
dependencies:
7844+
ansi-styles "^4.0.0"
7845+
string-width "^4.1.0"
7846+
strip-ansi "^6.0.0"
7847+
78347848
wrap-ansi@^8.1.0:
78357849
version "8.1.0"
78367850
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"

0 commit comments

Comments
 (0)