Skip to content

Commit c523d3d

Browse files
committed
bug #1005 [LiveComponent] fix: respect data-turbo="false" when performing redirects (phramz)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] fix: respect `data-turbo="false"` when performing redirects | Q | A | ------------- | --- | Bug fix? | yes | New feature? |no | Tickets | no | License | MIT Any redirect response from the backend will trigger a `Turbo.visit()` regardless if the frontend component lives within a `data-turbo="false"` context. This might lead to several problems when it comes to cross-site redirects since they will be dispatched via javascript `fetch()` where stricter rule apply (e.g. CORS etc). Commits ------- cfb3103 fix: respect `data-turbo="false"` when performing redirects
2 parents a5d2b05 + cfb3103 commit c523d3d

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/LiveComponent/assets/dist/Component/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default class Component {
5555
private doEmit;
5656
updateFromNewElementFromParentRender(toEl: HTMLElement): void;
5757
onChildComponentModelUpdate(modelName: string, value: any, childComponent: Component): void;
58+
private isTurboEnabled;
5859
private tryStartingRequest;
5960
private performRequest;
6061
private processRerender;

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,9 @@ class Component {
18931893
this.set(modelBinding.modelName, value, modelBinding.shouldRender, modelBinding.debounce);
18941894
});
18951895
}
1896+
isTurboEnabled() {
1897+
return typeof Turbo !== 'undefined' && !this.element.closest('[data-turbo="false"]');
1898+
}
18961899
tryStartingRequest() {
18971900
if (!this.backendRequest) {
18981901
this.performRequest();
@@ -1949,7 +1952,7 @@ class Component {
19491952
return;
19501953
}
19511954
if (backendResponse.response.headers.get('Location')) {
1952-
if (typeof Turbo !== 'undefined') {
1955+
if (this.isTurboEnabled()) {
19531956
Turbo.visit(backendResponse.response.headers.get('Location'));
19541957
}
19551958
else {

src/LiveComponent/assets/src/Component/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ export default class Component {
337337
});
338338
}
339339

340+
private isTurboEnabled(): boolean {
341+
return typeof Turbo !== 'undefined' && !this.element.closest('[data-turbo="false"]');
342+
}
343+
340344
private tryStartingRequest(): void {
341345
if (!this.backendRequest) {
342346
this.performRequest()
@@ -430,7 +434,7 @@ export default class Component {
430434

431435
if (backendResponse.response.headers.get('Location')) {
432436
// action returned a redirect
433-
if (typeof Turbo !== 'undefined') {
437+
if (this.isTurboEnabled()) {
434438
Turbo.visit(backendResponse.response.headers.get('Location'));
435439
} else {
436440
window.location.href = backendResponse.response.headers.get('Location') || '';

0 commit comments

Comments
 (0)