Tuesday, September 25, 2018

Loading Visualforce in Iframe, performance issue?


We usually get a requirement that Visualforce page needs to be loaded either from lightning or from Javascript or from any other Visualforce page in an iframe.

We had the same requirement too! Loading Visualforce page from lightning, but major road blocker was performance, Visualforce page was taking almost 3 seconds to load in an iframe.

When we use Visualforce page, we use relative URL like /apex/PageName, when we call Visualforce page from another Visualforce page, we don’t face performance issue, because it will be in same origin and server i.e. visualforce.com.

But when we call the same form lightning, both the server and origin are different, further if you call page in an iframe, it is even slower.

To avoid loading and performance issue, use absolute URL i.e. https://domain+Namespace.visualforce.com/apex/PageName


You can create absolute URL from below code:-

vfUrl = 'https://'+URL.getSalesforceBaseURL().getHost()+ ‘/apex/PageName’


Or
If your org has domain name and namespace [Suppose my org namespace is Test]:-

string nspace = 'Test__';

nspace = nspace.replace('__','').toLowerCase();

string vfUrl ='https://'+URL.getSalesforceBaseURL().getHost().split('\\.')[0];

vfUrl = vfUrl + (vfUrl.containsIgnoreCase('--'+nspace) ? '' : '--'+nspace) +'.'

                             + '.visualforce.com'+ ‘/apex/PageName’





1 comment:

  1. Hi I need a help. I have a similar requirement. From lightning i'm trying to load visualforce page in iframe. once the action is done how to close the iframe and back to the same parent page ?

    ReplyDelete