AlchemyCMS / alchemy_cms

Alchemy is the Open Source Rails CMS framework for the component based web that can be used as classic server side rendered or headless CMS.
https://www.alchemy-cms.com
BSD 3-Clause "New" or "Revised" License
846 stars 314 forks source link

Differing element positions between draft and published page versions #2788

Closed oneiros closed 5 months ago

oneiros commented 8 months ago

Sorry to bother you again with a very specific problem that may or may not be a bug. Also sorry for not using the issue template, but I could not find an easy way to reproduce this behavior on a fresh install.

I am working on an app that uses AlchemyCMS that originally started out with Alchemy 5. I upgraded everything to the latest Alchemy 7.1.6 and everything works mostly fine, except for links to anchors within pages.

I narrowed the problem down to an unfortunate combination of our data, the new way dom ids for elements are being generated and the new page versions feature.

Dom ids for elements are being automatically generated, but due to the new page versions feature the version 5 way of simply using database IDs was not possible anymore. The new way uses an element's position within the page.

This is all good and makes a lot of sense. But for some reason in our database, element positions are sometimes not continuous. So you might find positions like 1, 2, 6, 9. This still works for ordering the elements correctly, but the trouble is when publishing a page, the published versions get continuous positions. So the positions differ between draft and published versions breaking all anchors after the first gap in positions.

On a fresh AlchemyCMS install, all positions seem to always get updated when an element is removed, but I suspect this was not the case in some earlier version we started out with or used along the way.

As such the problem is hard to reproduce and I recognize this may not be considered a bug per se. On the other hand others who come from older versions of Alchemy might face the same problem, so I thought I should report it.

I have to admit I am currenlty at a loss as how to proceed. In the long term I think we should migrate to setting anchors manually, but of course this will be tedious at first. Can you think of an easy way to fix this in an automated fashion in the interim?

The only thing I found is an existing task here: https://github.com/AlchemyCMS/alchemy_cms/blob/50fbd39ef26a46a175408747bafe73f67d43ed9d/lib/alchemy/tasks/tidy.rb#L10-L27

But this has not been adapted to the new page versions feature and will not actually repair the draft versions.

tvdeyen commented 8 months ago

Maybe it helps to add your own element dom id class. https://github.com/AlchemyCMS/alchemy_cms/blob/main/app/models/alchemy/element/dom_id.rb

The rake task should be updated. Thanks for pointing that out.

oneiros commented 5 months ago

FWIW, I think we fixed it with setting anchors manually. I could not come up with any way to generate stable dom ids automatically.

I think there is nothing to be done here. Thanks for fixing the rake task and reading through my too long report :grimacing:

tvdeyen commented 5 months ago

Thank you for the heads up.