`title: 离线表单提交与重放:IndexedDB + Background Sync``categories: Web 开发/前端/数据管理``keywords: 离线表单,IndexedDB,Background Sync,队列,重放``description: 将表单请求离线入队到 IndexedDB,网络恢复或触发 Background Sync 后重放,结合幂等与冲突解决策略保障一致性。`入队与幂等键async function enqueueForm(db, payload) { return new Promise((resolve, reject) => { const tx = db.transaction('forms', 'readwrite'); tx.objectStore('forms').put({ id: crypto.randomUUID(), payload, ts: Date.now() }); tx.oncomplete = () => resolve(); tx.onerror = () => reject(tx.error); }); } 重放与冲突解决使用版本或服务端返回冲突信息进行合并或提示用户选择;保证操作幂等。

发表评论 取消回复