hydrationDone Event for Custom Code in Funnels

By Sparkforge Support Team ·Last reviewed May 3, 2026

The hydrationDone custom event fires after a Sparkforge funnel or website preview finishes hydrating. Listen for it to safely run custom DOM-manipulating JavaScript without race conditions.

Sparkforge funnels and websites dispatch a custom hydrationDone event in the preview after hydration completes. Listening for this event lets you safely run custom JavaScript that depends on the rendered DOM.

The problem it solves

Custom code in a code element used to run before the preview finished hydrating. That timing mismatch caused scripts to fail, miss elements, or produce inconsistent results, especially when manipulating the DOM.

How to use it

  1. Add a code element to the funnel or website page in the builder.
  2. Attach an event listener for hydrationDone.
  3. Place your custom logic inside the listener so it runs only after hydration completes.
document.addEventListener("hydrationDone", () => {
  // your custom javascript here
});

Once the preview's DOM is loaded and the event fires, your code runs without race conditions against hydration.

Optimize JavaScript and the hydration event

If the page has Optimize JavaScript enabled, hydration is deferred until user interaction. That means the hydrationDone event will not fire on initial load. If your code must run on page load, disable Optimize JavaScript for that page.

When you need this

  • Reading or modifying rendered DOM nodes.
  • Initializing third-party widgets that need the page fully painted.
  • Reading text or attributes that the builder injects during hydration.

Frequently Asked Questions

What is the hydrationDone event?
It is a custom DOM event that Sparkforge dispatches once the funnel or website preview finishes hydrating, signaling that custom code can safely run.
Why was my custom JavaScript failing before?
Without the event, scripts ran before hydration finished, so they hit a partially rendered DOM and produced inconsistent results.
Does Optimize JavaScript affect this?
Yes. With Optimize JavaScript enabled, hydration is delayed until user interaction, so the event will not fire on initial load. Disable it if you need code to run immediately.
Where do I add the listener?
Inside a code element on the page in the builder. Use document.addEventListener with hydrationDone as the event name.

Was this article helpful?