Proving State Persistence0:45
What trips people up is state is preserved out here between invocations. What's an easy way we can prove this? Out here, we'll say initializedAt equals new Date(), and then inside the handler, we'll say invokedAt equals new Date(). We'll return initializedAt and invokedAt. Let's deploy this. deploy --activate. It's going to be pretty quick. That new version is out there. Let's see what happens.
Caching the Page2:11
time. Looking back at our browser function, what is the most expensive thing? Well, probably launching Chrome and getting a new page, so let's get that out of there. We're going to pull this out, and we're going to put a top-level page, and then we'll have an async function getPage. Let's put all of this in here. We'll make browser a local variable, and then let page... We want to use this scope out here, so we'll set it to await new page, and then in the beginning, we'll have a little short circuit, if page, return page. Looking at this now, when AWS initializes it, this will be null, and we'll have this
beginning, we'll have a little short circuit, if page, return page. Looking at this now, when AWS initializes it, this will be null, and we'll have this function out here. The first time this function is called, it's going to check if the page is not null. If it is, it will return it, otherwise, it's going to launch Chrome and open a new tab. We also need to return the page for the first time, so we know that this is executed on every request. We are going to say let page equal getPage. Let's go ahead and clean this up. We don't need these extra parameters.
Removing Cleanup Logic3:34
Let's go ahead and clean this up. We don't need these extra parameters. The browser is gone. try catch. We're not going to do... Because we don't need to close the browser. We want to leave the browser open between invocations so that we're not constantly relaunching it. We're going to get rid of that, get rid of that. We don't need result.
Optimizing Handler and Deploy3:51
We're going to get rid of that, get rid of that. We don't need result. We're just going to return the page title instead of using the callback format. Because this is an async function, we need to put a await here. Now the handler is much less expensive in terms of operations, because all it's doing is getting the page variable that hopefully already exists after the first request. Then it uses that page and navigates to Laravel.com and then returns the page title. The theory here is expensive stuff goes out here where we can cache it from request to request and then our dynamic handler-specific code goes in here. Let's deploy this again.
