Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow rendering the same component more than once #607

Open
joeldrapper opened this issue Nov 28, 2023 · 1 comment
Open

Disallow rendering the same component more than once #607

joeldrapper opened this issue Nov 28, 2023 · 1 comment
Labels
Milestone

Comments

@joeldrapper
Copy link
Collaborator

joeldrapper commented Nov 28, 2023

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.

phlex/lib/phlex/sgml.rb

Lines 97 to 127 in 6a0eb85

def __final_call__(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: nil, &block)
@_buffer = buffer
@_context = context
@_view_context = view_context
@_parent = parent
block ||= @_content_block
return "" unless render?
around_template do
if block
if is_a?(DeferredRender)
__vanish__(self, &block)
template
else
template do |*args|
if args.length > 0
yield_content_with_args(*args, &block)
else
yield_content(&block)
end
end
end
else
template
end
end
buffer << context.target unless parent
end

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 joeldrapper added this to the 1.10 milestone Dec 6, 2023
@joeldrapper joeldrapper modified the milestones: 1.10, 2.0 Jan 29, 2024
@joeldrapper
Copy link
Collaborator Author

joeldrapper commented Jan 29, 2024

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

@joeldrapper joeldrapper added the good first issue Good for newcomers label Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant