Tuesday, July 26, 2016

GetClientContext Is Not Defined When Navigating Back To WebResource Html File

Ran into an interesting issue with chrome caching the ClientGlobalContext.js.aspx, and not providing an Xrm context to my javascript, when using the browser to navigate back to my webresource.  So just to clarify:
  1. Hit custom html WebResource via browser.
  2. WebResource loads ClientGlobalContext.js.aspx via script tag:
    <script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
  3. WebResource Displays link to CRM Entity.  Navigate to CRM Entity via link.
  4. Click “back” button in browser to navigate back to the custom WebResource.
  5. Receive JS error:
    GetClientContext is not defined
So, what’s going on?  Opening up the Developer Tools / Network tab on the initial load shows how it’s supposed to be working:


The first line is the load from the custom WebResource.  The second line, is then the ClientGlobalContext.js.aspx, loads itself, but as an xhr, rather than a script.  This loads the Xrm Context.
But when I look at the Network Tab when when I get the error: this is what I see:





The ClientGlobalContext.js.aspx is being loaded from cache.  That’s a good thing normally, but it never actually executed the xhr request.  Why Not?  When I opened the cached document, it was actually the xhr that was cached:


So how do I prevent the xhr from getting cached over the top of the script version?  Just append a query string to the end of the aspx page:
<script src="../../ClientGlobalContext.js.aspx?type=script" type="text/javascript"></script>
That way, the files can get cached separately…

No comments:

Post a Comment