29. Salesforce Lightning Locker Service | Matt Goldspink


Locker Service

Matt Goldspink is a UI Architect in Vlocity, Dreamforce speaker.

Matt is with strong experience in server-side Java, frontend development in HTML, CSS and JavaScript (Web Components, Angular, React, Sencha ExtJs) and mobile application development on iOS and Android (Native and hybrid).


Main Points

  • Matt’s blog thread on the Salesforce developer site and his locker service experience
  • Why does Salesforce create the locker service and what problem does it tackle?
  • What are the challenges for Salesforce to create the locker service?
  • What is the locker service implementation from a user or developer’s point of view?
  • Explanation of Third-party JavaScript library isolation in the locker service
  • How can we know if a third-party JavaScript library is well supported in the locker service?
  • What are the good resources to learn LWC?

Links


Video Teaser

The YouTube Video URL


Key Takeaway for Locker Service

Many Salesforce developers complain about Locker Service:

  1. It puts a restriction on browser native API calls
  2. It has a negative performance impact
  3. It prevents some popular 3ard party JavaScript libraries from working

They ignore the reason Locker service is introduced and the great benefit it brings to the Lightning platform.

In this episode, Matt gives a holistic view of Locker Service. Hopefully, you can understand better about and really embrace it :).


Related Episodes

Episode 22 – Lightning Web Component


Contact Me

If you enjoy this podcast, please reach me out to let me know your thoughts or simply say Hello. My (Xi’s) contact info:

Take care, until next time :)!

Transcript

AI-generated, unedited.

[00:09] Hey everybody, this is Xi Xiao. This is yet another episode of Salesforce Way podcast. Today I'm so happy to sitting with Matt Goldspink. Hello, Matt. Hey, would you please introduce yourself? Sure. So, my name is Matt. I am from the UK, and I currently work at Velocity as a UI developer. So, I've been with Velocity since twenty fourteen, and that's also the experience that I have on Salesforce is all with Velocity. So yeah, everything I've learnt about Salesforce has been there, and I've kind of picked up on different parts of the Salesforce ecosystem along the way. And right now, I'm kind of working on all the Lightning Web Components that has just been released by the guys, which is really exciting. That's cool. You get the chance to working with the cutting edge stuff. Yeah, yeah, yeah. We're we're all over it. Everything we're doing is now Lightning Web Components. Oh, I so admire you.

[01:11] Yeah, I think along the topic is that the I saw your article in in Salesforce Developer Blog. And it's talking about the Locker service, right? So I think you do have some experience with Locker service. Yeah. So before we started doing Lightning Web Components, we'd done a lot of playing around with Aura and just the Lightning platform in general. And we were on the pilot program when it was launched back in it was either twenty fourteen or twenty fifteen. And Locker was one of these things that was. Almost like a blocker to us to really adopt Aura because at the time it was so new and had a lot of kind of restrictions in it that we couldn't take our existing UI code, which was written in Angular JS at the time, and bring it into Aura. But we kind of kept a track of it and kept revisiting it, and over time it's evolved and improved. And now it's a state where we can do crazy things like import a whole Angular JS app into it, but it's kind of been one of my little passions on the side to understand security technologies, and for me, Locker is one of these things where. From a developer perspective, it can be a bit frustrating and get in the way of what you want to do. From a kind of architectural security perspective, it's it's a piece of ingenuity. It's it's really clever. It's really smart. It solves a lot of security issues that

[02:41] I know that I've faced in the past in other companies. And I think it's one of these technologies that's just been very misunderstood. And I think a lot of people's frustrations that I've seen within Velocity, for example, has come from those early days where it wasn't really as stable and mature as it is today. And you know, looking at where it is today, you can kind of show people that. It does do everything that it should do, which is you know from a security perspective, which is good. But it also allows you as a developer to do the things that you need to do as well, using whatever frameworks you need to use. Cause you know, for the locker service, to me it's a black box. I I just try to to deploy my code, the front end code to the Lightning component or Lightning Web component, and then I start to see whether it works or not. That's how, like naively, I try to do my stuff. When when I hit the wall, then I go back. But but you do have more experience with the locker. So how can I understand this black box? Should I, as a developer, understand that? And what kind of things, you know, does it tackle? Yeah, so I think the first way to to really understand it is to understand why Salesforce have implemented it. You kind of need to approach it. Differently as a Salesforce developer than a regular web developer. So, if you look at a regular web developer, you kind of own the whole platform. So, if you look at something, say like the Google homepage.

[04:12] The Google developers kind of own that whole page. They own the server. They own whatever front-end frameworks they want to use, any libraries they want to include. All of that is under their control, and it's the same for kind of other websites. You know, if you think of the Facebooks, the Netflix, anything like that, those developers are all internal and they own everything. If you put yourself now into the Salesforce world, you typically have a lot of different companies and partners all writing small pieces of the user interface, and it's all going to live on the same page together. So we might have people like at Velocity writing components. You might write some web components. We may have people from you know the Deloittes and Accentures, or even just people are in house developers at different companies using Salesforce, all writing components that are going to live on the same page. And the challenge with that is that with with regular JavaScript and CSS, it's kind of a bit of a A global nightmare in the sense that I write some CSS and it may style, say, paragraph tags. But if your component uses paragraph tags, my CSS is going to go and affect and change the look and feel of your paragraph tags. And if you go down to the JavaScript level, it's a similar thing. You can kind of run things like query selectors to get a hold of different elements in a page. But if I can suddenly get a hold of your element, so let's say you have a form field in your component.

[05:43] And I can get a hold of it, and I can go and see what data someone's entered. And let's say that that data was meant to be secure between your server and the customer. I'm not meant to go to anything at Velocity. Then we've kind of you know leaked suddenly a lot of data between two different or three or four different parties that shouldn't have access to that. So, for developers, you kind of have to rethink and kind of accept the fact that you are just a participant in a little box on a screen, and you don't own the whole page. And you have to really think about, well, what are my restrictions now because I am in this box. And so, Locker is the kind of technology that I guess reinforces the security and the isolation of your little sandbox for your component. And and the way that they do it is really smart. You know, I can go into. More technical detail, but at a high level, it essentially stops my CSS from touching any of your CSS. So I could write a paragraph tag CSS rule, but it would only touch my component and not yours. And similarly, I can query for some DOM elements using the the query selector API, but it would only give me back the elements that are in my component and not anything in anyone else's component.

[06:58] Can I in in a way can I think of it like the multi talent cloud like as a Salesforce right? Yeah. So we are sitting in maybe the same server, but we are different customers of Salesforce. So we are not allowed to see their data. We're not allowed to see their metadata. Yeah. Somehow there's a way to handle this boundary between these customers. So in this case, it's like different maybe slots that, as a developer, I can go to the front end page, click the editor. And then you know, drag those component into those slot onto the page. Those are kind of boundaries, right? Exactly. Yeah. So that that slot is your boundary for your Aura component or Lightning Web component, and and you can't leak anything out of that. And similarly, no one can kind of leak anything into you to you know take data or mess up how your component looks and feels. Hmm. That's cool, but you know, as the beginning tutorial, I don't really want to go into those details. And you mentioned that it's all the time evolving, right? At the beginning, it wasn't so easy to you know add those like Angular or React kind of libraries on top, but now it's getting much better. Yeah, yeah. So one of the ways that they kind of do this sandboxing is that they insert proxies for some of the the kind of standard. I guess web developer APIs, so things like the document and the window object that are always global in a browser.

[08:29] And so, one of the challenges they faced is that there's a lot of different methods and properties that exist on these. And so, they've had to find ways to basically lock it down to only let developers have access to safe versions of all of those. And I think that's the thing that's taken them a long time to do because there's always new. Methods and properties added to these in all the different browsers, and so kind of opening them up slowly to make sure that they're secure and also work consistently across browsers has been, I think, one of the big challenges for the guys at Salesforce. But you know the state that it's in right now is they've they've kind of implemented a lot of these APIs so that pretty much every library works. You know things like React, jQuery, D3 will all safely work inside of Locker Service now. Hmm. Maybe just the side question is that it's hard for me to grab this locker service. Is it like a like Apex code implementation, or is it like JavaScript, or what is it? So it it's in JavaScript, and essentially, when you upload your component to Salesforce, and the web page that you load in Salesforce wants to render your component. It downloads the code from Salesforce and then it puts it into Locker Service into a little container, basically. That Locker Service sits around, and so when you have some code in your component that

[09:59] says asks for a different DOM element, what you're really doing is asking Locker Service, "Hey, can you give me the safe version of this method to call?" And Locker Service then takes care of making sure you only get Pieces of the HTML and DOM that are safe for your component, so it just sits in its kind of own little, I guess, sandbox, security sandbox that Locker just manages for you and makes sure that you don't escape out of and no one escapes into. Okay, I got it. So if I check the HTML source, I'm able to see there's like a locker JS file loaded. Yeah, I mean it's you know Salesforce did a nice thing of kind of minifying and and compressing and and rewriting everything, so it's not very easy to to see. It did used to be open source in the Aura project. I have a feeling that it disappeared recently when they kind of moved Aura moved away from Aura to more LWC. So I I don't know where that code lives now. But you used to be able to look at it, but you can. There is something called the Locker API Viewer, which if you go to the Developer Docs for Lightning Web Components, you'll see that there is a tab at the top. And that basically lists all of the standard API functions from things like the window object, the document object, and the element object. And it says which ones are supported in Locker. It gives you kind of

[11:29] a nice green, red, and orange. So you know, green means it's fully supported and compatible. Red means it's not supported at all, and then yellow means it's kind of semi-supported, but there are some kind of quirks that you need to be aware of. So you can you can see what APIs and things are there to be able to sort of check your code against. That's a very useful information. So you mentioned it's Locker API viewer. It's a web page, right? It's a web developer developer side Salesforce. Okay. Yeah. I I will get that link from you later and put in our show notes. Sure. Hi, here's a quick note. Don't forget to check the show notes of this episode, where you can find short video teasers, the guest contact information, the important complementary materials, and my own learning points, among many other useful information. In addition, I have also started a YouTube channel to share important stuff I learned from my guest. If you are serious about your Salesforce developer journey, you should definitely consider subscribing to this channel. And obviously, the name of this YouTube channel is Salesforce Way. Now let's get back to the show, shall we?

[12:54] All right. And actually, I've read your article in the Developer Blog. There was one thing you mentioned. I think it's quite interesting. It's called the third-party JavaScript library isolation. Uh huh. You remember that? Yeah, I do remember that.

[13:12] Don't worry. That was quite a funny thing. So, could you open it up? What that means? Yeah, sure. So. Typically, when I say third party, I mean the JavaScript libraries that are written by someone that aren't myself or the the company that's writing the component. So we normally class things like jQuery, Angular, React, Moment JS, D3. All these open source libraries are typically referred to as third party libraries. And you know, in the spirit of good web development, we don't want to reinvent the wheel. So we want to try and reuse good frameworks and libraries inside of our components. So a good example of this is Moment JS. So Moment JS for web developers is a very popular date manipulation library, and it allows you to do really simple date calculations. So for example. If I was to give it my date of birth, it could tell me how old I am today, or you know how old I will be on a certain date in the future. And it does all sorts of things. You know, it can format it to different time zones, so it gets used very heavily and. One of the challenges that you typically have as a web developer when you're not in the Lightning world is that if you have different components that want to use Moment JS, they may be using two or three different versions of Moment JS, and you kind of have to because of the kind of global nature of a web

[14:42] page in JavaScript, you you kind of have to stick with just one version. And so then you you have to figure out okay well will the version that I use work with the version that this other component needs or am I going to have to switch to their version and I'm going to have to run all my regression tests and you know all these kind of common issues crop up. Now, the the beauty of Locker, and this is where I think one of the things that Locker does really well is that Locker isolates the libraries that you load into your component in the same way it isolates your code from other components. It will isolate the Moment JS library that we might load into our component. So I can load you know version two dot fifteen of Moment JS, but your component might load two dot eleven of Moment JS. And it wouldn't be a problem because of the way Locker's isolated these two components. We can have two versions of moment living and running on the page at the same time, in a way that you just can't do with standard JavaScript. So, in some ways, while Locker's restrictive, it's actually more flexible to be able to do some crazy things like that. Okay, but does it have any side effect? For example, we're loading too many the the you know the instances. Yeah, so so the memories. Exactly. Yeah. So there are some obvious downsides to this. So a common example I always give to people is that you know we say that you can use a library like React in your page to write

[16:12] components, and a lot of people you know like to write React components because it's you know it's a popular, well-known framework. It's well-tested. And it has a lot of functionality that people need, but the problem is, is that if I was to write a React component using React fifteen, and you were to reuse one written with React sixteen, we're now downloading two versions of React onto the same page, and we're suddenly having to download all this extra JavaScript to do something that could. Be done with a shared framework, and I think that's one of the things where you know to to sidetrack onto promoting LWC. That's one of the areas where I think LWC will actually help solve some of these framework battles that we might have in components. Because with LWC, you really don't need React anymore because it's essentially its own. UI framework that's very similar to React, but yeah, to go back to your original point, you're right that having too many versions of the same framework on a page is just going to slow it down and increase the memory. Just a quick question. I just pop up in my head. If we have two components loading the same version. Would lock her smartly know that okay we just need one copy. Ah, it it does if it's from the same static resource. So you know if you're writing a managed package like we do at Velocity, then we will always use the components that the frameworks that are uploaded in a static resource. And in that case, then yes. But you know if.

[17:43] If you know in Velocity we use React fifteen and then you know some other companies use React fifteen as well, and then we had their components. Unfortunately, it would still load the two separate versions because they're from their own packages. Okay, I got it. Yeah. So a lot of time we need to use many of those famous third-party libraries. He already mentioned like Angular, React, and D three, even the Google Map. So, is there a good way for me to decide? Okay, in our project, we are gonna choose this one. So we are we we are safe to use this library. Yeah, so there are a couple of things. So, firstly, one thing that we've started to do a lot now is to kind of really question: Do we need this framework? Is you know the features that it provides you know superseded by browser standards? So, a really good example of this is jQuery. A lot of people. Used to always include jQuery because it abstracted all these differences in browsers and added some extra functionality. Nowadays, you don't need to use jQuery. All of the kind of core functionality is implemented by all of the browsers already, so you're not really benefiting from including something like jQuery. Another example that you kind of mentioned there would be, say, Google Maps, and the guys at Salesforce now have a Maps component that you can use. So, where possible, I would probably say just use that instead

[19:13] of trying to include Google Maps yourself. But there are going to be kind of examples where you do need to include these frameworks. So D three is is a good example of something that a lot of people use to create you know dynamic charts and graphs and data visualizations. And kind of one problem that people always used to hit was, well, is this going to work inside of Locker Service at all? Is it even worth me trying to write a component that uses it and upload it? So one of the other tools that Salesforce have provided now, which is another tab on the Lightning Developer Docs I spoke about before, is called the the Locker Console. And essentially, what that is is a little JavaScript playground where you can copy and paste, say, the contents of D three into a little text area on it, and hit run, and it will tell you if it will throw an error inside of Locker, or if it will kind of work fine. And essentially, what it does is it tries to run the code through Locker on that web page and let you know. So that's a really useful tool. We've used it a few times recently. To kind of evaluate some other libraries that we needed to use to kind of save ourselves the hassle of figuring out, okay, is this going to work or not? This is a very useful information. Give me the link later, then I'll put in. I will. We love all these little tools. Yeah, yeah. But then I think that just the decision in general will just

[20:43] be based on well, if it solves all I need and it works in Locker, then you know we should probably go ahead and use it if if that's the right thing to do. And I think that. That decision at the end of the day will always come down to the developers in the team that are writing that component, and just kind of awareness to the implications of including, say, your own version of D three or whatever it is inside the component. Yeah, you know, to me, I started to learn the Lightning Web Components, and there are those sample libraries released by Salesforce, and those codes more or less they are really simple, really basic. How to, uh, across communication between different components. How to you know show the different items. But when the project gets complicated, I'm not really sure how to do those stuff. I I don't really find any open source project online that, for example, we can really mimic those functionalities or features in Angular or in React. So I don't know from my point of view how powerful the Lightning Web Component. I just heard this is like the modern standard, right? We have this web component. Yeah. Yeah, so I would say that the best examples to look at for something a little bit more complete are the that kind of bigger applications that Salesforce have built and shipped. So I know that there are some kind of recipe examples which show small examples, sort of the

[22:13] hello world and how to do simple data binding. But they have some good ones. Like there's a I'm trying to remember the name of it, but I think it's it's a kind of an e-bike store where you can drag and drop bikes into a shopping cart, which is. It's a really good example because it shows how to do something complex, like how do I do drag and drop between two different Lightning Web Components, which is. In the past, in Aura, it's actually been really difficult to do. But with Lightning Web Components, you can see it using just the standard browser drag drop APIs to move a piece of data from one component on the screen to another. So I would always start at these kind of bigger examples, and then I think that a lot of the you know bigger sort of architecture questions about how do we structure you know a number of web components to build a larger application. You can actually look at how people do this with React and other frameworks. Like, I mean, Polymer is a good example to compare against because Polymer is another web component implementation, and a lot of the principles about you know separating out components and sending messages between the two. Apply equally across these frameworks. It's just that there might be some API differences, but the the general architecture of messaging and data passing and isolation of components is is really the same. It's all about you know component

[23:43] isolation, writing reusable, small, simple components, and then. Kind of puzzling them together with a larger application component that kind of mediate between them all. So it's really you know there's not something special to say to say LWC and Lightning Web Components in general. It's really just you know I think taking just good architectural decisions and really remembering that you know a component should be isolated. It shouldn't be able to do things outside of itself. And therefore, you need to kind of think about how does that affect the decisions that we're going to make architecturally for all our components. Okay, thanks a lot. I recently started to learn unlocked package, and people always tell me, "Okay, start design your package," and if you come back to Lightning Web, come on, and it's the same thing. It it you always need to take a step back. I think to figure out, okay, what am I going to do here? Because yeah. I can start hacking away at code, and then in you know a couple of hours, I'll be like, "Oh my lord, what have I done?" You know, you have to you have to always take a little step back and think about, okay, how am I going to approach this and and build this up. Thanks, Matt. I think we had a really good conversation. Do you still have something else you want to add? No, no. I I think I am. Just thank you very much for for inviting me on the podcast. It's been great to talk about Locker and LWCs, and

[25:14] yeah, yeah. I hope people find it useful and interesting. Definitely, definitely. Thanks, Matt. It's great talking with you, and you. Thank you very much. Yeah. Bye bye. All right. Bye.

[25:30] If you enjoyed listening to this session and want to have a really easy way to support this podcast. Why don't you give us a quick rating or even a review on whatever channel you are listening to this show? If you are using iTunes, just go to SalesforceWay.com/itunes. It should open up the iTunes page for you. I read pretty much all the reviews you leave over there, and it really helps other people discover this show. So your support is very much appreciated, and I'm super grateful you have spent your invaluable time with me here. And I'd really love to hear how you think about this podcast. At last, I hope you are doing well, taking care, and see you next time.

Leave a Reply