File tree Expand file tree Collapse file tree 3 files changed +26
-0
lines changed
Expand file tree Collapse file tree 3 files changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -35,6 +35,15 @@ async myMethod () {
3535}
3636```
3737
38+ #### Turbo Streams
39+
40+ Request.JS will automatically process Turbo Stream responses. Ensure that your Javascript sets the ` window.Turbo ` global variable:
41+
42+ ``` javascript
43+ import { Turbo } from " @hotwired/turbo-rails"
44+ window .Turbo = Turbo
45+ ```
46+
3847# License
3948
4049Rails 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 {
1313 if ( response . unauthenticated && response . authenticationURL ) {
1414 return Promise . reject ( window . location . href = response . authenticationURL )
1515 } else {
16+ if ( response . ok && response . isTurboStream ) { response . renderTurboStream ( ) }
1617 return response
1718 }
1819 }
Original file line number Diff line number Diff line change @@ -47,4 +47,20 @@ export class Response {
4747 get text ( ) {
4848 return this . response . text ( )
4949 }
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+ }
5066}
You can’t perform that action at this time.
0 commit comments