fix: votes, comments and feed refresh all broken due to safeFetch misuse

safeFetch already returns parsed JSON and throws on non-2xx — callers were
treating the result as a Response object and checking .ok / .json(), which
made votes, comment votes, and comment posts silently fail every time.

Also: fetchProposals was missing cache:'no-store' so newly posted proposals
were invisible until the 24hr browser cache expired; getProposals was sending
24hr Cache-Control so Cloudflare CDN served stale data; getShareableImageUrl
had a double slash (/api//petition-svg); wrangler.toml had an invalid [pages]
stanza that caused a warning on every deploy.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
King Omar 2026-06-05 00:35:31 +10:00
parent 4db2a9a6cf
commit 876d0c438c
3 changed files with 8 additions and 35 deletions

View file

@ -750,7 +750,8 @@ function initializeShareButtons() {
'Accept': 'application/json'
},
mode: 'cors',
credentials: 'same-origin'
credentials: 'same-origin',
cache: 'no-store'
});
// Log the response details
@ -1174,7 +1175,7 @@ async function submitVoteToServer(proposalId, voteType, isPetition = false, peti
voteData.petitionDetails = petitionDetails;
}
const response = await safeFetch(`${API_BASE_URL}/votes`, {
return await safeFetch(`${API_BASE_URL}/votes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@ -1183,12 +1184,6 @@ async function submitVoteToServer(proposalId, voteType, isPetition = false, peti
mode: 'cors',
credentials: 'same-origin'
});
if (!response.ok) {
throw new Error('Failed to submit vote');
}
return await response.json();
}
// Replace the old submitVote function
@ -2172,7 +2167,7 @@ async function handleCommentVote(commentId, voteType) {
// Submit vote to server
try {
const response = await safeFetch(`${API_BASE_URL}/comment-votes`, {
await safeFetch(`${API_BASE_URL}/comment-votes`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@ -2183,16 +2178,6 @@ async function handleCommentVote(commentId, voteType) {
voteType: voteType
})
});
if (!response.ok) {
throw new Error('Failed to submit comment vote');
}
const result = await response.json();
// If the server response indicates we should update the UI differently, we could handle that here
// But generally the optimistic UI update above should match the server result
} catch (error) {
console.error('Error voting on comment:', error);
showToastMessage('Hmm, your comment vote didn\'t count. Please try again.', '#ff0099');
@ -2700,13 +2685,12 @@ function updateCommentCount(proposalId) {
if (!commentsToggle) return;
safeFetch(`${API_BASE_URL}/comments?proposalId=${proposalId}`)
.then(response => response.json())
.then(comments => {
const commentCount = comments.length;
const commentCount = Array.isArray(comments) ? comments.length : 0;
const commentText = commentCount === 1 ? 'comment' : 'comments';
commentsToggle.innerHTML = `<span class="comments-toggle-icon"></span> ${commentCount} ${commentText}`;
})
.catch(error => {
.catch(() => {
commentsToggle.innerHTML = `<span class="comments-toggle-icon"></span> 0 comments`;
});
}
@ -2738,12 +2722,6 @@ function submitComment(proposalId, commentText) {
commentText: commentText
})
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to post comment');
}
return response.json();
})
.then(data => {
loadComments(proposalId);
updateCommentCount(proposalId);

View file

@ -1099,7 +1099,7 @@ async function getProposals(request, env) {
total: proposals.results?.length || 0,
hasMore: proposals.results?.length === limit
}
});
}, 200, 0);
} catch (error) {
console.error('Error retrieving proposals:', error);
return createResponse({
@ -2147,7 +2147,7 @@ function escapeHTML(text) {
// Update the getShareableImageUrl function to use the SVG endpoint
function getShareableImageUrl(proposal) {
// Use the dynamic SVG endpoint with the proposal ID
return `https://theradicalparty.com/api//petition-svg?id=${proposal.id}`;
return `https://theradicalparty.com/api/petition-svg?id=${proposal.id}`;
}
// Update the serveCustomizedHtml function to use the SVG URL for meta tags

View file

@ -45,10 +45,5 @@ zone_name = "theradicalparty.com"
pattern = "theradicalparty.com/styles.css"
zone_name = "theradicalparty.com"
# Pages integration
[pages]
build_command = "npm run build"
build_output_dir = "dist"