Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions internal/ui/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ if (!window.trustedTypes || !trustedTypes.createPolicy) {
*
* @param {string} url - The URL to send the request to.
* @param {Object} [body] - The body of the request (optional).
* @param {RequestInit} [fetchOptions] - Additional fetch options (optional).
* @returns {Promise<Response>} The response from the fetch request.
*/
function sendPOSTRequest(url, body = null) {
function sendPOSTRequest(url, body = null, fetchOptions = {}) {
const options = {
method: "POST",
headers: {
"X-Csrf-Token": document.body.dataset.csrfToken || ""
}
"X-Csrf-Token": document.body.dataset.csrfToken || "",
...fetchOptions.headers,
},
...fetchOptions,
};

if (body !== null) {
Expand All @@ -36,6 +39,32 @@ function sendPOSTRequest(url, body = null) {
return fetch(url, options);
}

/**
* Navigate after a POST request that may redirect with a flash message.
*
* @param {Response} response - The fetch response.
* @param {string} [redirectURL] - An explicit redirect URL (optional).
*/
function navigateAfterPOSTResponse(response, redirectURL) {
if (redirectURL) {
window.location.href = redirectURL;
return;
}

const locationHeader = response.headers.get("Location");
if (locationHeader) {
Comment on lines +54 to +55

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Handle opaque redirect responses before navigating

This helper assumes it can read Location/redirect info after sendPOSTRequest(..., { redirect: "manual" }), but browser fetch returns an opaqueredirect response for server 302s in manual mode (empty headers and no usable redirect target). As a result, both redirect checks fail and execution falls through to window.location.reload(), so actions like /feeds/refresh do not navigate to the server’s redirect destination (/feeds) and the intended flash-message flow is still broken.

Useful? React with 👍 / 👎.

window.location.href = locationHeader;
return;
}

if (response?.redirected && response.url) {
window.location.href = response.url;
return;
}

window.location.reload();
}

/**
* Open a new tab with the given URL.
*
Expand Down Expand Up @@ -663,12 +692,8 @@ function toggleEntryStatus(element, toasting) {
function handleRefreshAllFeedsAction() {
const refreshAllFeedsUrl = document.body.dataset.refreshAllFeedsUrl;
if (refreshAllFeedsUrl) {
sendPOSTRequest(refreshAllFeedsUrl).then((response) => {
if (response?.redirected && response.url) {
window.location.href = response.url;
} else {
window.location.reload();
}
sendPOSTRequest(refreshAllFeedsUrl, null, { redirect: "manual" }).then((response) => {
navigateAfterPOSTResponse(response);
});
}
}
Expand Down Expand Up @@ -1250,14 +1275,8 @@ function initializeClickHandlers() {
// Generic confirmation handler
onClick(":is(a, button)[data-confirm]", (event) => {
handleConfirmationMessage(event.target, (url, redirectURL) => {
sendPOSTRequest(url).then((response) => {
if (redirectURL) {
window.location.href = redirectURL;
} else if (response?.redirected && response.url) {
window.location.href = response.url;
} else {
window.location.reload();
}
sendPOSTRequest(url, null, { redirect: "manual" }).then((response) => {
navigateAfterPOSTResponse(response, redirectURL);
});
});
});
Expand Down