This behavior seems odd to me...

So, a static resource loaded into a VF page creates a variable 'someVar' that can now be used on the VF page. A lightning component and a lightning web component hosted in the same aura app show someVar to be undefined in their javascript. This I would kind of expect.

Now, I load the static resource in the lightning component as well as on the VF page. someVar from the lightning component is not accessible on the VF page and someVar loaded to the VF page is not accessible to the lightning component, that is fine.

However, now the lightning web component has access to someVar from the lightning component after loading into the lightning component?

It is like it is sharing that static resource with everything else in the aura app? My understanding was that these were all supposed to be self contained and you need to load static resources in each component. Does loading a static resource in a lightning component add it to some lightning context on the page that the LWC can access and use? This kind of sharing of static resources wasn't exactly behavior I expected.

1 Answer 1


Depends on what your static resource is doing:

The reason for VF <=> AC/LWC is Lockerservice - when you declaring variables in a static resource it happens on the global scope (in that case the window object). VF has no Lockerservice and accesses the "real" global scope. AC / LWC gets a secure copy which is not linked to the real one => they have a own copy.

For AC / LWC i recommend to the section about sharing javascript here. Depending on what your static resource is executing it is expected that they share the same data. As long as it is not implemented as Singleton both AC and LWC shares the same values. The only thing which is prevented is that the resource is loaded over and over again when referenced by multiple components but they both accessing the same global scope

  • Ooh, so are you saying they automatically prevent multiple static resources being loaded by multiple components? So it's safe to just throw it in every component? Or I need to prevent that on my end? Jul 1, 2019 at 17:04
  • SFDC is doing it for you (i think they are checking if there is already a script tag with given resource url and just skip it if its the case) - if you are loading the same resource but as different static resource / with different name then of course it would be loaded multiple times - its only url based and not content diffing. So requiring it in every component should not have a negative impact (but also not sure what the benefit would be then ;) )
    – Renji-xD
    Jul 1, 2019 at 17:06
  • Ok cool. Yeah my concern when I noticed this behavior was loading that script multiple times. But we just want to throw it in every component because it is part of our feature tracking, so we need it in every component and don't know what components may be grouped with other components per page. Thanks for the info! Jul 1, 2019 at 17:20
  • ya that is of course a valid use case. This behavior is btw also documented in the docs: developer.salesforce.com/docs/atlas.en-us.lightning.meta/… . For your reference if needed (see one time loading) . I think lwc does not explicitly states it in the doc but in the background its doing the same (and i saw their code which checks for an existing script tag)
    – Renji-xD
    Jul 1, 2019 at 17:22
  • Thanks for the link. I was definitely failing to find useful info on this while trying to Google it. Jul 1, 2019 at 17:48

You must log in to answer this question.

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