To answer that we need to see your repo as described in Requesting Help.
The answer does not (at least as far as I can tell) lie in the code you posted in code blocks in your question, but elsewhere in your repo (likely in combination with the Stacks
theme). Therefore, we need to see the repo, or an example repo that consistently reproduces the error, to help.