Skip to content

Commit

Permalink
Adds Turbo Interceptor to add X-Socket-Id header on Turbo requests (#348
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tonysm authored Jul 25, 2022
1 parent d41094a commit d7706e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/echo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export default class Echo {
if (typeof jQuery === 'function') {
this.registerjQueryAjaxSetup();
}

if (typeof Turbo === 'object') {
this.registerTurboRequestInterceptor();
}
}

/**
Expand Down Expand Up @@ -161,6 +165,15 @@ export default class Echo {
});
}
}

/**
* Register the Turbo Request interceptor to add the X-Socket-ID header.
*/
registerTurboRequestInterceptor(): void {
document.addEventListener('turbo:before-fetch-request', (event: any) => {
event.detail.fetchOptions.headers['X-Socket-Id'] = this.socketId();
});
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ declare let io: any;
declare let Vue: any;
declare let axios: any;
declare let jQuery: any;
declare let Turbo: any;

0 comments on commit d7706e0

Please sign in to comment.