-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
useSWRSubscription - reconnect after close #2540
Comments
I feel like you could also add auth as const [auth, setAuth] = useState(getHeader());
const { data } = useSWRSubscription(
auth ? [path, auth] : null,
([path, auth], { next }: SWRSubscriptionOptions<SSEResponse, Event>) = {
const eventSource = new EventSourcePolyfill(path, auth);
eventSource.addEventListener('error',()=>{
setAuth(updateAuth());
}
return () => {
eventSource.close()
}
} If the auth info changed here, a new subscription would be created and the old one would be closed |
@promer94 Although I cannot find the document that explains it, I remember that the code you provided using an array is not compatible with SWR 2.0 or later versions. |
This is a bug that has been fixed in #2550 but not released yet 😂. Sorry about that. We will release a new version soon
It is compatible. You could find more info here As a workaround, you could use const [auth, setAuth] = useState(getHeader());
const { data } = useSWRSubscription(
auth ? [path, auth] : null,
(_, { next }: SWRSubscriptionOptions<SSEResponse, Event>) = {
const eventSource = new EventSourcePolyfill(path, auth);
eventSource.addEventListener('error',()=>{
setAuth(updateAuth());
}
return () => {
eventSource.close()
}
} |
@promer94 |
Bug report
How can I reconnect
useSWRSubscription
?Description / Observed Behavior
I used
useSWRSubscription
in SSE that requires authorization. After a 401 error, the header was changed and a new connection was needed after close, but I couldn't find a way to reconnect after the error.I tried setting auth as state so that the key value would change when auth changed, but it doesn't seem to detect the change.
I also tried using
mutate()
just in case, but it still doesn't reconnect.In
useSWRSubscription
, if an event is interrupted under certain conditions, is there a way to reconnect like useSWR-mutate?Expected Behavior
re-open
useSWRSubscription
connection after some events.Repro Steps / Code Example
Additional Context
SWR version. : 2.1.2
The text was updated successfully, but these errors were encountered: