Skip to content

fix(editor): stale lock version errors when saving site scoped sections - #261

Open
erdostom wants to merge 1 commit into
maglevhq:mainfrom
erdostom:bug/lock
Open

erdostom wants to merge 1 commit into
maglevhq:mainfrom
erdostom:bug/lock

Conversation

@erdostom

Copy link
Copy Markdown

Editing a site scoped section (e.g. a navbar with a menu) always ended up showing the "someone else updated the content in the meantime" modal, even with a single editor. The very first save went through (both stores shared the same lock version at that point), and every subsequent save failed forever.

Root cause: a site scoped section is listed in a layout store (its "slot", e.g. the header/main store) while its content lives in the global site scoped store (_site), which is the store the content services actually write to. The editor forms however submitted the lock version of the slot store (LockVersionConcern#source_lock_version, keyed off SectionContent#store_handle). Since site scoped writes only bump the _site store, the two versions diverge after the first save and every following save raises ActiveRecord::StaleObjectError.

Fixes:

  1. Forms now submit the section's own lock version (LockVersionConcern#source_lock_version): the sections content fetcher already stamps each section with the lock version of the store its content comes from — the _site store for site scoped sections. This is a no-op for regular sections (their stamped version is the one of their own store).

  2. The :persisted turbo stream event now dispatches the fresh lock version of the store which was actually written (UpdateSectionService / UpdateSectionBlockService return it), so the auto-saving settings forms keep working on subsequent changes. Previously the (stale) slot store version was dispatched.

  3. The preview now uses the same counter: Maglev::SectionComponent #lock_version reads the lock version stamped on the section attributes instead of the unrelated site/page model lock versions. As a result, the section:checkLockVersion mechanism only refreshes genuinely stale preview sections instead of always refreshing site scoped ones (the two counters could never match).

Tests:

  • request specs: with the slot and _site stores holding different lock versions, the section and block forms of a site scoped section render the _site lock version, updating with it succeeds and dispatches the bumped version, and a stale submission is still rejected with a 409.
  • component spec: SectionComponent#lock_version uses the stamped attribute, including for a site scoped definition.
  • UpdateSectionService specs now assert the returned store (the passed store for regular sections, the site scoped one for site scoped sections).

Editing a site scoped section (e.g. a navbar with a menu) always ended
up showing the "someone else updated the content in the meantime" modal,
even with a single editor. The very first save went through (both stores
shared the same lock version at that point), and every subsequent save
failed forever.

Root cause: a site scoped section is listed in a layout store (its
"slot", e.g. the header/main store) while its content lives in the
global site scoped store (_site), which is the store the content
services actually write to. The editor forms however submitted the lock
version of the *slot* store (LockVersionConcern#source_lock_version,
keyed off SectionContent#store_handle). Since site scoped writes only
bump the _site store, the two versions diverge after the first save and
every following save raises ActiveRecord::StaleObjectError.

Fixes:

1. Forms now submit the section's own lock version
   (LockVersionConcern#source_lock_version): the sections content
   fetcher already stamps each section with the lock version of the
   store its content comes from — the _site store for site scoped
   sections. This is a no-op for regular sections (their stamped
   version is the one of their own store).

2. The :persisted turbo stream event now dispatches the fresh lock
   version of the store which was actually written
   (UpdateSectionService / UpdateSectionBlockService return it), so the
   auto-saving settings forms keep working on subsequent changes.
   Previously the (stale) slot store version was dispatched.

3. The preview now uses the same counter: Maglev::SectionComponent
   #lock_version reads the lock version stamped on the section
   attributes instead of the unrelated site/page *model* lock versions.
   As a result, the section:checkLockVersion mechanism only refreshes
   genuinely stale preview sections instead of always refreshing site
   scoped ones (the two counters could never match).

Tests:
- request specs: with the slot and _site stores holding different lock
  versions, the section and block forms of a site scoped section render
  the _site lock version, updating with it succeeds and dispatches the
  bumped version, and a stale submission is still rejected with a 409.
- component spec: SectionComponent#lock_version uses the stamped
  attribute, including for a site scoped definition.
- UpdateSectionService specs now assert the returned store (the passed
  store for regular sections, the site scoped one for site scoped
  sections).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant