Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Auto-retry failed uploads in-app on load and reconnect (correct CSRF, all browsers) instead of relying on Background Sync (#313)

Murtaza Nasir679e54b1e9b4
diff --git a/static/js/app.modular.js b/static/js/app.modular.js
index b12f6db4..5601458d 100644
--- a/static/js/app.modular.js
+++ b/static/js/app.modular.js
@@ -3605,6 +3605,13 @@ document.addEventListener('DOMContentLoaded', async () => {
await recordingsComposable.selectRecordingById(deepLinkRecordingId);
}
+ // Auto-retry any uploads that previously failed and were saved to
+ // IndexedDB (#313): once on load, and again whenever the browser
+ // reports it's back online. Works in every browser (no Background
+ // Sync) and resubmits through the normal CSRF-correct upload path.
+ uploadComposable.resurfaceFailedUploads();
+ window.addEventListener('online', () => uploadComposable.resurfaceFailedUploads());
+
// Clean up orphaned incognito data if we're not viewing incognito recording
// This can happen if user navigated away without the cleanup triggering
if (uploadComposable.hasIncognitoRecording() && selectedRecording.value?.id !== 'incognito') {
diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.js
index 55dd4c2d..a2adc6cd 100644
--- a/static/js/modules/composables/upload.js
+++ b/static/js/modules/composables/upload.js
@@ -647,6 +647,13 @@ export function useUpload(state, utils) {
fileItem.recordingId = data.id;
fileItem.progress = 100;
+ // If this was a resurfaced retry (#313), drop its IndexedDB record
+ // now that it's safely on the server so it isn't retried again.
+ if (fileItem._failedUploadId) {
+ try { await FailedUploads.deleteFailedUpload(fileItem._failedUploadId); }
+ catch (e) { console.warn('[Upload] Could not clear retried upload from IndexedDB:', e); }
+ }
+
// The in-progress recording session is now safe to clear (issue
// #287(b)): the audio is on the server. Only do this for queue
// items that came from an in-app recording, to avoid touching the
@@ -719,61 +726,74 @@ export function useUpload(state, utils) {
const friendlyErr = getFriendlyError(error.message, t);
setGlobalError(`${friendlyErr.title}: ${friendlyErr.guidance}`);
- // Defense-in-depth recovery (issue #297, #287):
- // 1. Persist the file to IndexedDB so background sync can retry.
- // 2. If IndexedDB persistence fails (quota exceeded, private mode,
- // missing IndexedDB), fall back to triggering a browser-side
- // download so the user keeps a local copy of the audio. The
- // worst possible outcome is permanent audio loss on upload
- // failure, so we treat both layers as best-effort and only
- // stop trying once one succeeded.
- let persistedToIndexedDB = false;
- try {
- await FailedUploads.storeFailedUpload({
- file: fileItem.file,
- fileName: fileItem.file.name,
- fileSize: fileItem.file.size,
- clientId: fileItem.clientId,
- notes: fileItem.notes,
- tags: fileItem.tags,
- asrOptions: fileItem.asrOptions,
- error: error.message
- });
- persistedToIndexedDB = true;
- } catch (dbError) {
- console.warn('[Upload] IndexedDB persistence failed for failed upload:', dbError);
- }
-
- if (persistedToIndexedDB) {
+ // If this failure is itself a resurfaced retry, just bump the retry
+ // count on the EXISTING IndexedDB record — don't store a duplicate or
+ // re-download. resurfaceFailedUploads() will pick it up again on the
+ // next online/load (until it hits the retry cap).
+ if (fileItem._failedUploadId) {
try {
- if ('serviceWorker' in navigator && 'sync' in ServiceWorkerRegistration.prototype) {
- const registration = await navigator.serviceWorker.ready;
- await registration.sync.register('sync-uploads');
- console.log('[Upload] Registered background sync for failed upload');
- }
- } catch (syncError) {
- console.warn('[Upload] Failed to register background sync:', syncError);
+ await FailedUploads.updateRetryCount(
+ fileItem._failedUploadId, (fileItem._retryCount || 0) + 1, error.message);
+ } catch (e) {
+ console.warn('[Upload] Could not bump retry count:', e);
}
} else {
- // IndexedDB rejected the failed-upload write. Last-resort:
- // hand the audio back to the user as a browser download so it
- // survives a tab close.
- const downloaded = FailedUploads.triggerLocalDownload(
- fileItem.file,
- fileItem.file?.name || `speakr-recording-${Date.now()}.webm`
- );
- if (downloaded) {
+ // Defense-in-depth recovery (issue #297, #287, #313):
+ // 1. Persist the file to IndexedDB so it can be auto-retried
+ // in-app on the next online/page-load (resurfaceFailedUploads).
+ // 2. If IndexedDB persistence fails (quota exceeded, private
+ // mode, missing IndexedDB), fall back to a browser download
+ // so the user keeps a local copy. Permanent audio loss is the
+ // worst outcome, so both layers are best-effort.
+ let persistedToIndexedDB = false;
+ try {
+ await FailedUploads.storeFailedUpload({
+ file: fileItem.file,
+ fileName: fileItem.file.name,
+ fileSize: fileItem.file.size,
+ clientId: fileItem.clientId,
+ notes: fileItem.notes,
+ tags: fileItem.tags,
+ asrOptions: fileItem.asrOptions,
+ error: error.message
+ });
+ persistedToIndexedDB = true;
+ } catch (dbError) {
+ console.warn('[Upload] IndexedDB persistence failed for failed upload:', dbError);
+ }
+
+ if (persistedToIndexedDB) {
+ // Saved for retry. We deliberately do NOT retry right here
+ // (a server-side failure while still "online" would just storm
+ // the endpoint); resurfaceFailedUploads() resubmits it through
+ // the normal CSRF-correct upload path on the next `online`
+ // event or page load — in every browser, no Background Sync.
showToast?.(
- (t && t('toasts.uploadFailedDownloadedLocally'))
- || 'Upload failed. Audio saved to your Downloads folder for retry.',
- 'fa-file-download'
+ (t && t('toasts.uploadFailedWillRetry'))
+ || 'Upload failed — saved and will retry automatically when you reconnect.',
+ 'fa-rotate'
);
} else {
- showToast?.(
- (t && t('toasts.uploadFailedNoRecovery'))
- || 'Upload failed and audio could not be saved locally. Please record again.',
- 'fa-triangle-exclamation'
+ // IndexedDB rejected the failed-upload write. Last-resort:
+ // hand the audio back to the user as a browser download so it
+ // survives a tab close.
+ const downloaded = FailedUploads.triggerLocalDownload(
+ fileItem.file,
+ fileItem.file?.name || `speakr-recording-${Date.now()}.webm`
);
+ if (downloaded) {
+ showToast?.(
+ (t && t('toasts.uploadFailedDownloadedLocally'))
+ || 'Upload failed. Audio saved to your Downloads folder for retry.',
+ 'fa-file-download'
+ );
+ } else {
+ showToast?.(
+ (t && t('toasts.uploadFailedNoRecovery'))
+ || 'Upload failed and audio could not be saved locally. Please record again.',
+ 'fa-triangle-exclamation'
+ );
+ }
}
}
} finally {
@@ -1064,7 +1084,72 @@ export function useUpload(state, utils) {
return IncognitoStorage.hasIncognitoRecording();
};
+ // Cap so a permanently-broken upload doesn't retry forever.
+ const MAX_AUTO_RETRIES = 3;
+ let _isResurfacing = false;
+
+ // Auto-retry path for #313: read the failed uploads persisted in IndexedDB,
+ // rebuild a File for each, and resubmit them through the normal upload queue
+ // (correct CSRF, visible in the Processing Queue, works in every browser —
+ // no Background Sync). Triggered on page load and on the browser `online`
+ // event. Items are removed from IndexedDB on success and retry-counted on
+ // failure (see uploadSingleFile).
+ const resurfaceFailedUploads = async () => {
+ if (_isResurfacing) return;
+ if (typeof navigator !== 'undefined' && navigator.onLine === false) return;
+ _isResurfacing = true;
+ try {
+ const failed = await FailedUploads.getFailedUploads();
+ let queued = 0;
+ for (const rec of failed) {
+ if (!rec || !rec.fileData) continue;
+ if ((rec.retryCount || 0) >= MAX_AUTO_RETRIES) continue;
+
+ // Skip if this upload is already represented in the queue and
+ // actively in flight; otherwise drop a stale failed item so we
+ // don't show duplicates.
+ const existingIdx = uploadQueue.value.findIndex(i => i.clientId === rec.clientId);
+ if (existingIdx !== -1) {
+ const st = uploadQueue.value[existingIdx].status;
+ if (['uploading', 'pending', 'ready', 'queued'].includes(st)) continue;
+ uploadQueue.value.splice(existingIdx, 1);
+ }
+
+ const file = new File([rec.fileData], rec.fileName || `speakr-recording-${Date.now()}.webm`,
+ { type: rec.mimeType || 'audio/webm' });
+ uploadQueue.value.push({
+ file: markRaw ? markRaw(file) : file,
+ notes: rec.notes || '',
+ tags: rec.tags || [],
+ asrOptions: rec.asrOptions || {},
+ status: 'ready', // picked up by startProcessingQueue (skips startUpload's UI-state overwrite)
+ recordingId: null,
+ clientId: rec.clientId,
+ error: null,
+ duration: null,
+ willAutoSummarize: false,
+ preserveOptions: true, // keep the stored tags/ASR, don't clobber with current UI state
+ _failedUploadId: rec.id,
+ _retryCount: rec.retryCount || 0,
+ });
+ queued++;
+ }
+ if (queued > 0) {
+ showToast?.(
+ (t && t('toasts.retryingPendingUploads')) || 'Retrying pending upload(s)…',
+ 'fa-rotate'
+ );
+ startProcessingQueue();
+ }
+ } catch (e) {
+ console.warn('[Upload] resurfaceFailedUploads failed:', e);
+ } finally {
+ _isResurfacing = false;
+ }
+ };
+
return {
+ resurfaceFailedUploads,
handleDragOver,
handleDragLeave,
handleDrop,
diff --git a/static/locales/de.json b/static/locales/de.json
index 33556610..f1496c0a 100644
--- a/static/locales/de.json
+++ b/static/locales/de.json
@@ -1730,6 +1730,8 @@
"processingCompleted": "Verarbeitung abgeschlossen!",
"uploadFailedDownloadedLocally": "Upload fehlgeschlagen. Audio wurde im Downloads-Ordner für einen späteren Versuch gespeichert.",
"uploadFailedNoRecovery": "Upload fehlgeschlagen und Audio konnte nicht lokal gespeichert werden. Bitte erneut aufnehmen.",
+ "retryingPendingUploads": "Ausstehende Uploads werden erneut versucht…",
+ "uploadFailedWillRetry": "Upload fehlgeschlagen – gespeichert und wird bei erneuter Verbindung automatisch wiederholt.",
"shareTargetReceived": "Geteilte Audiodatei empfangen und zur Transkription eingereiht",
"recordingSizeSoftWarning": "Aufnahme ist {{size}}. Erwägen Sie, sie bei Gelegenheit zu stoppen.",
"recordingMaxDurationReached": "Aufnahme hat die Höchstdauer erreicht und wurde automatisch gestoppt.",
diff --git a/static/locales/en.json b/static/locales/en.json
index e604881a..056a9788 100644
--- a/static/locales/en.json
+++ b/static/locales/en.json
@@ -1730,6 +1730,8 @@
"processingCompleted": "Processing completed!",
"uploadFailedDownloadedLocally": "Upload failed. Audio saved to your Downloads folder for later retry.",
"uploadFailedNoRecovery": "Upload failed and audio could not be saved locally. Please record again.",
+ "retryingPendingUploads": "Retrying pending upload(s)…",
+ "uploadFailedWillRetry": "Upload failed — saved and will retry automatically when you reconnect.",
"shareTargetReceived": "Shared audio received and queued for transcription",
"recordingSizeSoftWarning": "Recording is {{size}}. Consider stopping when convenient.",
"recordingMaxDurationReached": "Recording reached the maximum duration and was stopped automatically.",
diff --git a/static/locales/es.json b/static/locales/es.json
index ae88dc99..6f79852c 100644
--- a/static/locales/es.json
+++ b/static/locales/es.json
@@ -1728,6 +1728,8 @@
"toasts": {
"uploadFailedDownloadedLocally": "Subida fallida. El audio se guardó en la carpeta de Descargas para reintentarlo más tarde.",
"uploadFailedNoRecovery": "Subida fallida y el audio no pudo guardarse localmente. Por favor, vuelva a grabar.",
+ "retryingPendingUploads": "Reintentando subidas pendientes…",
+ "uploadFailedWillRetry": "Error en la subida — guardada y se reintentará automáticamente al reconectar.",
"shareTargetReceived": "Audio compartido recibido y puesto en cola para transcripción",
"recordingSizeSoftWarning": "La grabación ocupa {{size}}. Considere detenerla cuando le convenga.",
"recordingMaxDurationReached": "La grabación alcanzó la duración máxima y se detuvo automáticamente.",
diff --git a/static/locales/fr.json b/static/locales/fr.json
index 8d5001eb..f6cd3f7b 100644
--- a/static/locales/fr.json
+++ b/static/locales/fr.json
@@ -1728,6 +1728,8 @@
"toasts": {
"uploadFailedDownloadedLocally": "Échec de l'envoi. L'audio a été enregistré dans votre dossier Téléchargements pour une nouvelle tentative.",
"uploadFailedNoRecovery": "Échec de l'envoi et l'audio n'a pas pu être enregistré localement. Veuillez réenregistrer.",
+ "retryingPendingUploads": "Nouvelle tentative des envois en attente…",
+ "uploadFailedWillRetry": "Échec de l'envoi — enregistré et réessayé automatiquement à la reconnexion.",
"shareTargetReceived": "Audio partagé reçu et mis en file pour transcription",
"recordingSizeSoftWarning": "L'enregistrement fait {{size}}. Pensez à l'arrêter quand vous pourrez.",
"recordingMaxDurationReached": "L'enregistrement a atteint la durée maximale et s'est arrêté automatiquement.",
diff --git a/static/locales/pt-BR.json b/static/locales/pt-BR.json
index 2e4fb64f..5ac3ef38 100644
--- a/static/locales/pt-BR.json
+++ b/static/locales/pt-BR.json
@@ -1728,6 +1728,8 @@
"toasts": {
"uploadFailedDownloadedLocally": "Upload falhou. Áudio salvo na pasta Downloads para tentar novamente mais tarde.",
"uploadFailedNoRecovery": "Upload falhou e o áudio não pôde ser salvo localmente. Por favor, grave novamente.",
+ "retryingPendingUploads": "Tentando reenviar uploads pendentes…",
+ "uploadFailedWillRetry": "Falha no envio — salvo e será reenviado automaticamente quando você reconectar.",
"shareTargetReceived": "Áudio compartilhado recebido e enfileirado para transcrição",
"recordingSizeSoftWarning": "A gravação tem {{size}}. Considere parar quando for conveniente.",
"recordingMaxDurationReached": "A gravação atingiu a duração máxima e foi parada automaticamente.",
diff --git a/static/locales/ru.json b/static/locales/ru.json
index 8dd7c585..d53451e2 100644
--- a/static/locales/ru.json
+++ b/static/locales/ru.json
@@ -1728,6 +1728,8 @@
"toasts": {
"uploadFailedDownloadedLocally": "Загрузка не удалась. Аудио сохранено в папку «Загрузки» для повторной попытки.",
"uploadFailedNoRecovery": "Загрузка не удалась и аудио не удалось сохранить локально. Пожалуйста, запишите снова.",
+ "retryingPendingUploads": "Повторная отправка отложенных загрузок…",
+ "uploadFailedWillRetry": "Ошибка загрузки — сохранено и будет автоматически повторено при восстановлении связи.",
"shareTargetReceived": "Аудио получено через «Поделиться» и поставлено в очередь на транскрипцию",
"recordingSizeSoftWarning": "Размер записи {{size}}. Подумайте об остановке при удобном случае.",
"recordingMaxDurationReached": "Запись достигла максимальной длительности и была остановлена автоматически.",
diff --git a/static/locales/zh.json b/static/locales/zh.json
index 62306b2d..904b7172 100644
--- a/static/locales/zh.json
+++ b/static/locales/zh.json
@@ -1728,6 +1728,8 @@
"toasts": {
"uploadFailedDownloadedLocally": "上传失败。音频已保存到下载文件夹,可稍后重试。",
"uploadFailedNoRecovery": "上传失败,且音频未能本地保存。请重新录制。",
+ "retryingPendingUploads": "正在重试待处理的上传…",
+ "uploadFailedWillRetry": "上传失败——已保存,将在重新联网后自动重试。",
"shareTargetReceived": "已接收共享音频并加入转录队列",
"recordingSizeSoftWarning": "录音大小为 {{size}},可在方便时停止。",
"recordingMaxDurationReached": "录音已达到最长时长,已自动停止。",