diff --git a/quartz/components/registry.ts b/quartz/components/registry.ts index 2b55b9f..444cd49 100644 --- a/quartz/components/registry.ts +++ b/quartz/components/registry.ts @@ -90,7 +90,12 @@ class ComponentRegistry { try { let instance: QuartzComponent if (typeof r.component === "function") { - instance = this.instantiate(r.component as QuartzComponentConstructor, undefined) + // Check if this constructor was already instantiated (with any options). + // Re-instantiating with `undefined` when options were provided would create + // a duplicate instance with separate afterDOMLoaded scripts. + const existing = this.findCachedInstance(r.component as QuartzComponentConstructor) + instance = + existing ?? this.instantiate(r.component as QuartzComponentConstructor, undefined) } else { instance = r.component as QuartzComponent } @@ -103,6 +108,17 @@ class ComponentRegistry { } return results } + + private findCachedInstance( + constructor: QuartzComponentConstructor, + ): QuartzComponent | undefined { + const ctorId = (constructor as unknown as { __cacheId?: string }).__cacheId + if (!ctorId) return undefined + for (const [key, instance] of this.instanceCache) { + if (key.startsWith(`${ctorId}:`)) return instance + } + return undefined + } } export const componentRegistry = new ComponentRegistry()