Skip to content

Fix: use deterministic IDs for code block copy buttons - #471

Draft
danielledeleo wants to merge 1 commit into
mainfrom
deterministic-code-block-ids
Draft

Fix: use deterministic IDs for code block copy buttons#471
danielledeleo wants to merge 1 commit into
mainfrom
deterministic-code-block-ids

Conversation

@danielledeleo

@danielledeleo danielledeleo commented Apr 1, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Replaces now.UnixNano-based element IDs with a simple ordinal counter
  • Applies to both render-codeblock.html (fenced code blocks) and ghcode.html (shortcode)
  • Verified across two consecutive clean builds of a 620-page site: all 1,570 output files produce identical hashes

Problem

Code block copy buttons were assigned IDs like id-18a245ee1c0763ad using the wall-clock nanosecond timestamp at render time. This made HTML output non-deterministic — 40 out of 1,571 files differed between clean rebuilds of unchanged source, breaking content-addressable caching, diffing, and reproducible deployments.

Solution

// Before
{{ $codeBlockId := printf "id-%x" (now.UnixNano) }}

// After (render-codeblock.html)
{{ $codeBlockId := printf "cb-%d" .Ordinal }}

// After (ghcode.html)
{{ $codeBlockId := printf "gh-%d" .Ordinal }}

.Ordinal is Hugo's zero-based per-page counter. Different prefixes (cb- vs gh-) prevent collisions if both templates are used on the same page. Output IDs are clean and readable: cb-0, cb-1, cb-2, etc.

@danielledeleo
danielledeleo requested a review from a team as a code owner April 1, 2026 16:40
@github-actions

github-actions Bot commented Apr 1, 2026

Copy link
Copy Markdown

❌ One or more of the commit messages in this PR do not match the nginx-hugo-theme git guidelines, please check the CI job for more details on which commits were flagged and why.

Please do not close this PR and open another, instead modify your commit message(s) with git commit --amend and force push those changes to update this PR.

@github-actions

github-actions Bot commented Apr 1, 2026

Copy link
Copy Markdown

Deploy Preview will be available once build job completes!

Name Link
😎 Deploy Preview https://frontdoor-test-docs.nginx.com/previews/nginx-hugo-theme/471/

@danielledeleo
danielledeleo force-pushed the deterministic-code-block-ids branch from 6940e33 to 976785d Compare April 1, 2026 16:54
@danielledeleo
danielledeleo marked this pull request as draft April 1, 2026 18:32
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