medusa-ui/medusa

IdentifierGenerator is not working ideally

Closed this issue · 1 comments

Right now, identifierGenerator works like this:

private IdentifierGenerator() {}
//TODO: Not ideal, IDs should be unique but also reproducable
//Hash is not guaranteed unique, especially based on content (same content = same id)
public static String generateIfID(String value) {
return "if-" + Math.abs(value.hashCode());
}
public static String generateTemplateID(String value) {
return "t-" + Math.abs(value.hashCode());
}

And that's potentially a little dangerous.

Imagine scenario:
You have two iterations on the same content, as such:

[$foreach $items-bought]
    <p>Medusa</p>
[$end]

[$foreach $items-bought]
    <p>Medusa</p>
[$end]

The ID would only hash the content, so it would generate the same for both iterations - causing confusion and invalid HTML (id needs to be unique).

But at the same time I need to make sure that no matter how many refreshes you do, it always generates the same ID (otherwise the ConditionalRegistry or IterationRegistry does not have the right reference -> you get a situation where things work until you refresh).

Not sure what the best solution for this is.

Possible fix in PR #40

Changes:

example page: http://localhost:8080/history