Barazo default frontend barazo.forum
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(reactions): handle empty response body on unlike and accept stale deletes (#194)

apiFetch now safely handles empty response bodies by reading text first
before parsing JSON, preventing the JSON.parse error on DELETE 204/200
responses. LikeButton's error handler now treats "Not Found" during
unlike as a successful state transition instead of reverting, preventing
count inflation from repeated like/unlike cycles.

authored by

Guido X Jansen and committed by
GitHub
b48a30cf 4cdf15df

+20 -7
+12 -5
src/components/like-button.tsx
··· 105 105 reactionUriRef.current = result.uri 106 106 } 107 107 } catch (err) { 108 - // Revert optimistic update 109 - setLiked(wasLiked) 110 - setCount(previousCount) 111 - reactionUriRef.current = previousUri 112 108 const message = err instanceof Error ? err.message : 'Failed to update reaction' 113 - toast({ title: 'Error', description: message, variant: 'destructive' }) 109 + const isNotFound = message === 'Not Found' || message.includes('not found') 110 + 111 + if (wasLiked && isNotFound) { 112 + // Reaction was already deleted server-side -- accept the unliked state 113 + reactionUriRef.current = null 114 + } else { 115 + // Revert optimistic update 116 + setLiked(wasLiked) 117 + setCount(previousCount) 118 + reactionUriRef.current = previousUri 119 + toast({ title: 'Error', description: message, variant: 'destructive' }) 120 + } 114 121 } finally { 115 122 setPending(false) 116 123 }
+8 -2
src/lib/api/client.ts
··· 139 139 await throwApiError(response) 140 140 } 141 141 142 - if (response.status === 204) { 142 + const contentLength = response.headers.get('content-length') 143 + if (response.status === 204 || contentLength === '0') { 143 144 return undefined as T 144 145 } 145 146 146 - return response.json() as Promise<T> 147 + const text = await response.text() 148 + if (!text) { 149 + return undefined as T 150 + } 151 + 152 + return JSON.parse(text) as T 147 153 } 148 154 149 155 function buildQuery(params: Record<string, string | number | undefined>): string {