Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Last active July 16, 2024 16:17
Show Gist options
  • Save KonnorRogers/d9abe11599e23a45ff5aca83817983fe to your computer and use it in GitHub Desktop.
Save KonnorRogers/d9abe11599e23a45ff5aca83817983fe to your computer and use it in GitHub Desktop.
Catching hydration errors in Lit
class MyElement extends LitElement {
protected update(changedProperties: PropertyValues<this>): void {
try {
super.update(changedProperties)
} catch (e) {
// We should probably check the contents of the message since this could error on a regular update??
// I really dont know. Its hacky, but it works.
const event = new Event("lit-hydration-error", { bubbles: true, composed: true, cancelable: false })
// @ts-expect-error leave me alone TS.
event.error = e
this.dispatchEvent(event)
throw e
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment