showFlag's actions not working after closing modals

Hi everyone,

Our app is currently using showFlag actions for navigation when the user clicks on them. However, once the modal that created the flag is closed, those actions no longer work.

I’ve done some research and found a few posts having the same issue, like this one. The cause seems to be that the flag action still tries to communicate with the modal’s window (or context) after it has been closed, resulting in the postrobot_method / “Target window is closed” error.

Has anyone found a workaround for this? Or is there a recommended approach from Atlassian for this problem?

@NguyenLe the workaround is to call the showFlag in the onClose() event handler, specified when opening the modal or the generic event handler.

This assumes that you are opening the modal from within your app UI, meaning that there is a underlying module that is still active and that acts as the modal parent.

Thank you for the workaround, @remie.

But wouldn’t that just fire a new flag? If it is, I think it will create bad UX

Well, I guess the idea is that you only call showFlag from the event handler, not in the modal. That way, only one flag will be shown.

Thanks for replying again, remie.

Unfortunately, the flag is bound to a specific CRUD action so there’s no hope for it.
Anyways, I hope Atlassian will address this issue as from what I see in Atlassian’s code, the flag shouldn’t required its origin still opens for the actions to keep working (since showFlag is just for passing the flag data to the actual flag provider on the web, IIUC), unless they have a specific reason to do so.

Again, thank you, remie.

Atlassian will not address this issue, simply because it can’t.

The onClick event handler code for the actions is defined within the code that runs in the iframe of the modal. As soon as the modal closes, that iframe is removed from the DOM and the client-side code is therefore no longer available.

This is a deliberate architectural feature of Forge, as the modal content needs to run in an isolated sandboxed iframe. There is nothing for Atlassian to fix.

If you want this to work, you will need to move your showFlag logic to the iframe that is responsible for opening the modal. Please note that you can pass a custom payload to view.close() which is available in onClose() event handler. After the CRUD operation has completed, you can simply pass success/error data and call the showFlag() based on that payload.

This is how we have implemented it and it works perfectly :man_shrugging:t2: