HTMLRewriter amend HTML Head

Is there a recommend pattern for updating the HTML head tag after processing the body. I’m considering cases such as adding link preload tags to the head after determining that specific image resources referenced in the body should be prioritized. I’m sure there are other similar cases (CSS optimization perhaps).

HTMLRewriter is stream-based, so “rewinding” back to the <head> isn’t really a thing. Maybe run the response through HTMLRewriter twice? First time, decide what you’re going to do, second time do it? Not something I have tried but it sounds feasible.

That’s my impression as well. Just checking if I had missed something clever.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.