1

I'm exploring writing a LWC mixin that loads a script as part of its use using loadScript from the Platform Resource Loader.

The script I want to load is the Apollo GraphQL client Javascript code, and to have this be reusable among multiple components. I know LWCs are primitive and clunky and won't be able to take advantage of things like auto-grouping of queries across multiple queries into one callout, but it would still be nice to make GraphQL queries from the client.

The Platform Resource page doesn't mention anything about caching. Are scripts loaded with loadScript re-fetched and re-parsed every time? Or is one script instance reused on subsequent calls? If I have multiple components on my page that all fetch, parse, and execute a script, then it would be wasteful to use loadScript for this purpose.

1 Answer 1

2

Static Resources are loaded only once, and then cached. This means that the same script loaded from multiple places will only produce one network transfer. Further, the script should only be parsed/loaded once per namespace (this ensures that data doesn't leak across namespaces), as far as I can tell.

Also, to correct a statement:

I know LWCs are primitive and clunky and won't be able to take advantage of things like auto-grouping of queries across multiple queries into one callout, but it would still be nice to make GraphQL queries from the client.

LWC is not a primitive runtime, although it is missing a couple of nice-to-haves, some of which I have heard are being considered/planned in a future version, so stay tuned. LWC does indeed group callouts to the server, but only when it would be advantageous to to do so (see this very detailed technical explanation). It also performs only a single request for duplicate requests with the same parameters, assuming @AuraEnabled(cacheable=true). LWC is very performant, except in the cases where developers are unaware of Locker Service problems and how to avoid them.

It's true that there are some annoyances with LWC. I won't try to sugarcoat it. That said, LWC is actually quite capable, and is personally one of my favorite frameworks. Languages like React end up having all kinds of weird "hacks" to make things work correctly, while LWC just tries to "do the right thing", and usually gets it right, in my opinion.

You must log in to answer this question.

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