Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] Use 'Scratch Pad' instead of now.UnixNano #156

Merged
merged 1 commit into from
Jun 30, 2024

Conversation

TetraTheta
Copy link
Contributor

Using now.UnixNano will change <a>'s ID every time the site is built.

In most cases, this is fine. But in my case, it didn't work well. When testing my site locally, a few bs/collapse shared the same ID. This made them to be opened or closed at once, together. I don't know why that happened, because that means multiple bs/collapse were generated at the same time, and the chance is highly unlikely.

So I changed the $id part to be like this:

{{- $id := printf "collapse-%s" (delimit (shuffle (seq 1 9)) "") -}}

This also works for creating distinct IDs.

Both now.UnixNano and delimit (shuffle (seq 1 9)) "" have a problem. They will create a different ID every time they are called. If you build a site twice without changing anything, the build results won't be the same because of the different IDs.

After a brief search, I've found that Hugo supports a per-page scratch pad. Since the scratch pad is only shared on a single page, we can safely use incremental numbers for the IDs because no same number will occur on a single page if we increment it properly.

I tested this for my site, and it works as intended. It even worked for nested bs/collapse. The only difference is that I use %03d instead of %d for numbers.

This might increase build time because now Hugo has to assign a map for each page, instead of just getting now.UnixNano.

@razonyang razonyang merged commit fdfc144 into hugomods:main Jun 30, 2024
1 check passed
@razonyang
Copy link
Member

Thanks.

@TetraTheta TetraTheta deleted the use-scratch branch June 30, 2024 11:05
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.

None yet

2 participants