HubSpot / calling-extensions-sdk

A JavaScript SDK for integrating calling apps into HubSpot.
https://developers.hubspot.com/docs/api/crm/extensions/calling-sdk
MIT License
46 stars 29 forks source link

Calling SET_WIDGET_URL doesn't work before redirect to other URL #230

Open xvlcekp opened 1 week ago

xvlcekp commented 1 week ago

Checklist

Description I cannot figure how to redirect to a different widget URL. I assume send message of type 'SET_WIDGET_URL' doesn't work. Here is my scenario:

I set an initial URL pointing to a decision server. It retrieves the portal ID, makes the API request to get the customer's url. Before redirect, I set 'SET_WIDGET_URL' with the new URL I do the redirect to the new URL. It is very similar to the issue #18 . The URLs are in format: company1.x.com, company2.x.com, company3.x.com and so on...

Expected behavior When I send a message of type messageType.SET_WIDGET_URL, I expect the iFrameManager stores the new host, so I can do the redirect to this URL, where a calling-extensions-sdk library will be initialized.

To Reproduce I am calling this message, but it has no effect:

      cti.sendMessage({
          type: 'SET_WIDGET_URL',
          iFrameUrl: `https://newInstance.host.com/custom//index.html`
      });

Screenshots/source code

<script>
    const sizeInfo = {
        width: 400,
        height: 650,
    };
    const cti = new window.default({
        debugMode: true,
        eventHandlers: {
            onReady: ({ engagementId, portalId, userId } = {}) => {
                cti.initialized({ // The script was initialized to get portalId
                    engagementId,
                    isLoggedIn: false,
                    sizeInfo,
                });
                if (portalId) {
                    let newUrl = getUrlPlaceholder(); // We fetch URL based on portalId
                    cti.sendMessage({
                        type: 'SET_WIDGET_URL',
                        iFrameUrl: `https://${newUrl}`
                    });
                    window.location.assign(`https://${newUrl}/custom/index.html`); // I assume this script will initialize the library after redirect
                }
            },
        },
    });
</script>

hubspot cti panel

Device information https://www.whatsmybrowser.org/b/UCQLI

Additional context I cleared the private information because of data protection. If you need further information, let me know.

esme commented 2 days ago

Hi @xvlcekp, the iframeManager indeed doesn’t persist the URL across window unload events. Instead of using SET_WIDGET_URL to store the new URL before redirecting, you might consider using local storage or session storage to save the URL. This way, you can retrieve it after the redirect and call SET_WIDGET_URL to initialize the sdk library as needed. Let me know if this answers your question!

Esirei commented 1 day ago

Hi @xvlcekp, the iframeManager indeed doesn’t persist the URL across window unload events. Instead of using SET_WIDGET_URL to store the new URL before redirecting, you might consider using local storage or session storage to save the URL. This way, you can retrieve it after the redirect and call SET_WIDGET_URL to initialize the sdk library as needed. Let me know if this answers your question!

I'm having the same issues reported here. Unfortunately, this solution does not work on the new page as the calling extension has not been initialised.

xvlcekp commented 1 day ago

Hi @esme thanks for your answer. The problem isn't with storing the URL, but once the iframeManager is initialized with one domain, it doesn't initialize on the other again. Setting new destinationHost is required. On the new domain it is not possible to do anyting as @Esirei mentioned, because the communication is dead.

esme commented 17 hours ago

@xvlcekp Thanks for the explanation! I don't believe we're ever setting a new destinationHost on cti.sendMessage({ type: 'SET_WIDGET_URL' }) because this sends the message to our internal SDK support library and we're not handling the message internally. Let me investigate this a little further to figure out we can resolve this