diff --git a/src/admin/components/elements/Autosave/index.tsx b/src/admin/components/elements/Autosave/index.tsx index 7e8049f118..e51cbb5fd4 100644 --- a/src/admin/components/elements/Autosave/index.tsx +++ b/src/admin/components/elements/Autosave/index.tsx @@ -31,12 +31,18 @@ const Autosave: React.FC = ({ collection, global, id, publishedDocUpdated const [lastSaved, setLastSaved] = useState(); const debouncedFields = useDebounce(fields, interval); const fieldRef = useRef(fields); + const modifiedRef = useRef(modified); // Store fields in ref so the autosave func // can always retrieve the most to date copies // after the timeout has executed fieldRef.current = fields; + // Store modified in ref so the autosave func + // can bail out if modified becomes false while + // timing out during autosave + modifiedRef.current = modified; + const createCollectionDoc = useCallback(async () => { const res = await fetch(`${serverURL}${api}/${collection.slug}?locale=${locale}&fallback-locale=null&depth=0&draft=true`, { method: 'POST', @@ -95,29 +101,31 @@ const Autosave: React.FC = ({ collection, global, id, publishedDocUpdated }; setTimeout(async () => { - const res = await fetch(url, { - method, - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - 'Accept-Language': i18n.language, - }, - body: JSON.stringify(body), - }); + if (modifiedRef.current) { + const res = await fetch(url, { + method, + credentials: 'include', + headers: { + 'Content-Type': 'application/json', + 'Accept-Language': i18n.language, + }, + body: JSON.stringify(body), + }); + + if (res.status === 200) { + setLastSaved(new Date().getTime()); + getVersions(); + } + } setSaving(false); - - if (res.status === 200) { - setLastSaved(new Date().getTime()); - getVersions(); - } }, 1000); } } }; autosave(); - }, [i18n, debouncedFields, modified, serverURL, api, collection, global, id, getVersions, locale]); + }, [i18n, debouncedFields, modified, serverURL, api, collection, global, id, getVersions, locale, modifiedRef]); useEffect(() => { if (versions?.docs?.[0]) {