Skip to content

Commit d7706e0

Browse files
authored
Adds Turbo Interceptor to add X-Socket-Id header on Turbo requests (#348)
1 parent d41094a commit d7706e0

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/echo.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export default class Echo {
121121
if (typeof jQuery === 'function') {
122122
this.registerjQueryAjaxSetup();
123123
}
124+
125+
if (typeof Turbo === 'object') {
126+
this.registerTurboRequestInterceptor();
127+
}
124128
}
125129

126130
/**
@@ -161,6 +165,15 @@ export default class Echo {
161165
});
162166
}
163167
}
168+
169+
/**
170+
* Register the Turbo Request interceptor to add the X-Socket-ID header.
171+
*/
172+
registerTurboRequestInterceptor(): void {
173+
document.addEventListener('turbo:before-fetch-request', (event: any) => {
174+
event.detail.fetchOptions.headers['X-Socket-Id'] = this.socketId();
175+
});
176+
}
164177
}
165178

166179
/**

typings/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ declare let io: any;
33
declare let Vue: any;
44
declare let axios: any;
55
declare let jQuery: any;
6+
declare let Turbo: any;

0 commit comments

Comments
 (0)