Open-source document signature platform. Not affiliated with DocuSign.
TypeScript
Read the field revision before the fields, and keep it current after a save on the way out
Claude26e00e0647a0
diff --git a/src/components/envelopes/prepare-live-shell.tsx b/src/components/envelopes/prepare-live-shell.tsxindex 5461448..375d67a 100644--- a/src/components/envelopes/prepare-live-shell.tsx+++ b/src/components/envelopes/prepare-live-shell.tsx@@ -591,18 +591,36 @@ function PrepareLiveShellContent({return;}- // After any save still on its way, so this one lands last.- void saveChainRef.current.then(() =>- fetch(`/api/envelopes/${envelopeId}/fields`, {- body: JSON.stringify({- ...buildFieldRequest(unsaved),- expectedRevision: fieldsRevisionRef.current,- }),- headers: { "content-type": "application/json" },- keepalive: true,- method: "PUT",- }).catch(() => undefined),- );+ const signature = serializeFieldRequest(unsaved);++ // After any save still on its way, so this one lands last, and in the+ // chain itself: a page the browser keeps and later shows again (Back+ // after leaving) picks up the revision this save made before it+ // saves again, instead of having its next save refused.+ saveChainRef.current = saveChainRef.current.then(async () => {+ try {+ const response = await fetch(`/api/envelopes/${envelopeId}/fields`, {+ body: JSON.stringify({+ ...buildFieldRequest(unsaved),+ expectedRevision: fieldsRevisionRef.current,+ }),+ headers: { "content-type": "application/json" },+ keepalive: true,+ method: "PUT",+ });++ if (!response.ok) {+ return;+ }++ const saved = (await response.json()) as ReplaceFieldsResponse;++ fieldsRevisionRef.current = saved.fieldsRevision;+ lastSavedSignatureRef.current = signature;+ } catch {+ // The page has gone, or the save did not arrive: nothing to do.+ }+ });}// A full page exit (closing the tab, reloading, typing an address)diff --git a/src/lib/envelopes.ts b/src/lib/envelopes.tsindex e5d5a45..a384437 100644--- a/src/lib/envelopes.ts+++ b/src/lib/envelopes.ts@@ -2308,11 +2308,15 @@ export async function getPrepareEnvelopeDataForUser(return null;}- const [fieldsByEnvelope, pages, signers, fieldsRevision] = await Promise.all([+ // Read before the fields, so a save landing in between pairs the newer+ // fields with the older revision: the editor's first save is then+ // refused and it asks for a reload, rather than an older list being+ // taken as the latest.+ const fieldsRevision = await readFieldsRevision(envelopeId, database);+ const [fieldsByEnvelope, pages, signers] = await Promise.all([listFieldsForEnvelopeIds([envelopeId], database),listEnvelopePagesForEnvelopeId(envelopeId, database),listSignersForEnvelopeId(envelopeId, database),- readFieldsRevision(envelopeId, database),]);return {