How can one avoid a footnote appearing on the page before its anchor in the text

Hello @Mark_Hilton , this looks very yummy :yum:

There is a lot of code in your example that does not relate to the problem you are encountering, making it harder for others to try to help you quickly. A MWE would greatly augment the chances of receiving some help.

However, it seems you have correctly identified the culprit (the level 2 heading). If you remove the code, the footnote shows up properly after being declared.

#set page(paper: "a8")

// Culprit
#show heading.where(level: 2): it => {
  let threshold = 6em
  block(breakable: false, height: threshold)
  v(-threshold, weak: true)
  it
}

= L1
== Short Footnote (ok)
#lorem(1)#footnote[#lorem(20)]#lorem(1)

= L1
== Long Footnote (not ok)
#lorem(1)#footnote[#lorem(25)]#lorem(1)

The issue you are experiencing is very similar has similarities to with the one here (note the use of a MWE by OP) How do I avoid splitting footnote on multiple pages? - #2 by PgBiel. However, the fix mentioned by @PgBiel in that case doesn’t seem to work, or at least comes in conflict with your heading level 2 show rule:

// At the TOP of your document:
#show footnote.entry: block.with(breakable: false)

I guess you can’t push on both sides at the same time. Someone else may have more insight on the internals of Typst about that.

One solution, as you can see in my MWE, is to reduce the length of the footnote…I had yours fit on the page at 112 characters. I understand this may not be what you are looking for.

Another solution would be to amend that heading level 2 show rule. Are you willing to have two recipes on the same page? Perhaps a pagebreak() after each level 2 heading would be in order?

Edit: There is an open issue about this: Footnote precedes a reference to it · Issue #5314 · typst/typst · GitHub