File tree 3 files changed +26
-0
lines changed
3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,15 @@ async myMethod () {
35
35
}
36
36
```
37
37
38
+ #### Turbo Streams
39
+
40
+ Request.JS will automatically process Turbo Stream responses. Ensure that your web app sets the ` window.Turbo ` global variable:
41
+
42
+ ``` javascript
43
+ import { Turbo } from " @hotwired/turbo-rails"
44
+ window .Turbo = Turbo
45
+ ```
46
+
38
47
# License
39
48
40
49
Rails Request.JS is released under the [ MIT License] ( LICENSE ) .
Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ export class Request {
13
13
if ( response . unauthenticated && response . authenticationURL ) {
14
14
return Promise . reject ( window . location . href = response . authenticationURL )
15
15
} else {
16
+ if ( response . ok && response . isTurboStream ) { response . renderTurboStream ( ) }
16
17
return response
17
18
}
18
19
}
Original file line number Diff line number Diff line change @@ -47,4 +47,20 @@ export class Response {
47
47
get text ( ) {
48
48
return this . response . text ( )
49
49
}
50
+
51
+ get isTurboStream ( ) {
52
+ return this . contentType . match ( / ^ t e x t \/ v n d \. t u r b o - s t r e a m \. h t m l / )
53
+ }
54
+
55
+ async renderTurboStream ( ) {
56
+ if ( this . isTurboStream ) {
57
+ if ( window . Turbo ) {
58
+ Turbo . renderStreamMessage ( await this . text )
59
+ } else {
60
+ console . warn ( 'You must set `window.Turbo = Turbo` to automatically process Turbo Stream events with request.js' )
61
+ }
62
+ } else {
63
+ return Promise . reject ( new Error ( `Expected a Turbo Stream response but got "${ this . contentType } " instead` ) )
64
+ }
65
+ }
50
66
}
You can’t perform that action at this time.
0 commit comments