phlex-ruby / phlex

A framework for building object-oriented views in Ruby.
https://beta.phlex.fun
MIT License
1.27k stars 83 forks source link

Disallow rendering the same component more than once #607

Closed joeldrapper closed 3 months ago

joeldrapper commented 10 months ago

Rendering a component more than once can result in unintended consequences. For example, if your component uses DeferredRender to capture slots, the content of those slots will leak between different renders of the same instance.

I think we should update Phlex to disallow this, raising an error if you attempt to render the same instance of a component more than once. The place to do this would be in __final_call__ which is the method that gets renamed to call after the first render for caching.

https://github.com/phlex-ruby/phlex/blob/6a0eb85d2fe6e3959644c45185150ee29856eee0/lib/phlex/sgml.rb#L97-L127

I would approach this by first checking for an instance variable and raising if it's true, and then setting that instance variable to true.

joeldrapper commented 8 months ago

We can add this as a warning in 1.x.

bugdimes commented 3 months ago

I will do this in a few days