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

JS error when removing an element in beforeClientResponse #19954

Open
sissbruecker opened this issue Sep 13, 2024 · 0 comments
Open

JS error when removing an element in beforeClientResponse #19954

sissbruecker opened this issue Sep 13, 2024 · 0 comments

Comments

@sissbruecker
Copy link
Contributor

Description of the bug

The scenario is basically:

  • Some client side code calls a @ClientCallable method
  • The method schedules a beforeClientResponse hook
  • The hook removes the element from the UI

This results in a JS error like:

Uncaught (in promise) TypeError: this.$server is undefined
    anonymous FlowClient.js line 1049 > Function:3
    anonymous FlowClient.js line 1049 > Function:3
    Ut FlowClient.js:1049
...

The issue seems to be that @ClientCallable schedules a JS call to return the return value of the method to the client:

"execute":[[0,null,null,"return (async function() { this.$server['}p']($0, true, $1)}).apply($2)"]],"timings":[257,2],"syncId":4}]

However, since the element has been removed from the DOM, the JS call results in a null reference.

Expected behavior

There should be no error. Flow should either remove the JS call from the UIDL, or have client-side error handling in case an element reference can not be resolved.

Alternatively, it should not be possible to remove elements from the UI in beforeClientResponse.

Minimal reproducible example

public class TestComponent extends Div {
    public TestComponent() {
        var button = new Button("Remove");
        button.addClickListener(e -> getElement().executeJs("this.$server.remove()"));
        add(button);
    }

    @ClientCallable
    public void remove() {
        UI.getCurrent().beforeClientResponse(this,
                context -> getElement().removeFromParent()
        );
    }
}

Versions

  • Vaadin / Flow version: 24.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🔖 Low Priority (P3)
Development

No branches or pull requests

2 participants