-
Notifications
You must be signed in to change notification settings - Fork 439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add request interceptor #177
Conversation
Could this same seam for modifying headers be achieved with a addEventListener("turbo:before-fetch-request", ({ detail: { fetchOptions }) => fetchOptions.headers["..."] = "...")) |
Changing headers with |
@kirillplatonov Nice work on this PR! Great that we won't need to fetch JWT every x seconds with your |
Until it's merged I published my fork to npm:
|
Great stuff @kirillplatonov. Could you add some documentation for this on https://turbo.hotwire.dev/reference/drive? It's in the turbo-site repo. Thanks! |
@@ -0,0 +1,15 @@ | |||
export class RequestInterceptor { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we not set this like we do progressBarDelay? Where it's set on the session, not as a singleton.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here in Turbo it would be completely possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to put it in session initially. But we use interceptor in FetchRequest
class where we don't have an access to the session. Add fetching session from delegates not worked as well since not all delegates have access to the session. That's why I end up using a singleton. Seems like the cleanest solution available.
Added documentation here: hotwired/turbo-site#53 |
Thanks for getting this through @kirillplatonov @dhh - useful for JWT / Shopify apps! |
@kirillplatonov Upon further reflection, I think it might be worth exploring an approach similar to #28 (comment). Which basically let's the turbo:before-fetch-request pause the request. Then we could do it all in a callback instead. |
@dhh having the same pausable API for both |
The problem
The modern server-side embedded Shopify apps are built with JWT session tokens which are obtained on the client-side and then passed to every server request.
Turbo
works pretty well with it since it allows to navigate application without doing page reloading on every click or page submission. The biggest issue is passing JWT token to everyTurbo
request.The solution
This PR introduces request interceptor support. It allows to insert async function between every fetch request. Inside this function you can do some preparation for request and add custom headers.
Example usage