2

I have an HTML5 app setup; when the browser is closed, the session ends. When I re-open the app, the app detects the session is invalid, and prompts the user to login again. This is all fine.

However, when the user is redirected back to the page, their old session information is still cached:

            sforce.connection.sessionId = '{!$Api.Session_ID}';

Thus, the application still sees the old session and thinks the app is offline, even though the user has a new session and is online.

Is there a way to reset the sessionId dynamically on the cached page?

1 Answer 1

1

You could have some Javascript run when the page loads, that rerenders the bit of script containing {!$Api.Session_ID}, like this

<apex:page>
  <script type="text/javascript">
  // Need to call actionFunction indirectly, since it isn't defined 
  // early enough to add it to window below
  function doRerenderPanel() {
    console.log('Rerendering panel');
    rerenderPanel();
  }

  // Or $(document).ready() if you're using jQuery
  if (window.addEventListener) {
    window.addEventListener('load', doRerenderPanel, false);
  } else if(window.attachEvent) {  
    window.attachEvent('onload', doRerenderPanel);
  }
  </script>

  <apex:form>
    <apex:actionFunction name="rerenderPanel" rerender="scriptPanel" />
    <apex:outputPanel id="scriptPanel">
      <script>
        // sforce.connection.sessionId = '{!$Api.Session_ID}';
        alert('Your session id: {!$Api.Session_ID}');
      </script>
    </apex:outputPanel>
  </apex:form>
</apex:page>

In the example, the alert fires twice - once when the page is first rendered, and again when the outputPanel is rerendered. You will need some logic to ignore the initial value of {!$Api.Session_ID} and only use the rerendered version.

2
  • Thanks very much; To follow up, if this were loaded via offline caching, I obviously can't reset the API.Sessoin_ID. Is there a clean way to detect that the session is no longer valid? Apr 2, 2014 at 21:30
  • You could ignore API.Session_ID completely and just look at the sid cookie. The only way to detect if a session ID is valid is to try using it in an API call.
    – metadaddy
    Apr 2, 2014 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .