I’m having a problem with getState() method from history in Jira. Even though my link contains required data it doesn’t work from time to time - getState() resolves with and empty string. I tried both calling it directly from AP.history.getState() and AP.require(['history']), (history) => (history.getState()}. Any ideas what might be the problem?
Are calling the AP.history functions in a Admin page, General page, Configure page or User profile page? Only these pages support the AP.history module.
Are you calling AP.history.pushState(…) before calling AP.history.getState(...)?
I’m not sure which type of page is that, I’m entering Epics Map (HeroCoder’s add-in) page, which is under: /projects/IC?selectedItem=com.atlassian.plugins.atlassian-connect-plugin:com.herocoders.plugins.jira.epicsmap__epics-map-page#!selectedQuickFilters=2
As you can see I’m passing the state I want to read, to set some pre-selected options on that page. Unfortunately there is a race condition - sometimes I can read getState() properly, but sometimes it’s empty, even though the state is passed in the url. I’ve added a button, to log the state after the page loads and it works fine - the state is there, even though the initial render didn’t read it.
OK, that’s a project page which is a type of general page. I think I understand your scenario. If I misunderstand, please let me know (I’m making a couple of assumptions):
Some action causes a navigation to a project page with state encoded in the hash fragment of the URL.
The project page waits for a DOMContentLoaded event <- assumption (if not, might be worth trying)
The project page app iframe calls AP.history.getState(callback).
Expected result:
At step 3, the callback is reliably passed the state encoded in the hash fragment of the URL.
Actual result:
At step 3, the callback is intermittently passed an empty or undefined state.
@dmorrow great advice, thanks so much! I had to do a tiny change and it seems to be working fine. Initially I thought that add-in code is triggered only when everything else is ready, but I’ve added some logs and realized that it runs earlier - I moved some initial state variables to functions and triggered them later and that did the trick. Thanks again!
Dugald,
unfortunately moving rendering after DOMContentLoaded didn’t solve the problem. It doesn’t happen on my browser, but others still have the same problem…
I can confirm the problem is there and it looks like a race condition.
In Firefox, on average, every second attempt (page reload or pasting the URL to a new window), fails.
In Chrome, it fails more often - only 20% of attempts succeed. If I open devtools (it slows down the browser I think), then all attempts succeed.
So, it looks like AP.history.getState() does not work consistently.
We have confirmed the problem in another of our apps.
@dmorrow, should we report an ACJIRA bug, or do you want to confirm the problem on your side first?
Thanks for your further comments. I have created [ACJS-1155] - Ecosystem Jira to capture this. You may like to review the description of the issue , comment and vote as you see fit.