YouTubevideo· Nov 30, 2023· 5:43· 2,800 views on YouTube
React at a moment you like. The best moments light up here.
[Music] so JavaScript developers are always kind of bragging about loading Spinners I mean you know loading Spinners are cool loading Spinners aren't cool sometimes but then also hey we can load in certain elements and wait for the rest to be pulled from the server or whever we're fetching that data and then we can just kind of load it all in as it comes to us well Live Wire can already do that within larabel but Assan just dropped a
cool little tip right here um where we can make it look like uh Spa for every single request that we hit to the server so I had to play around with this and I want to show you uh just a little bit about what I found so let's dive in within vs code I kind of added a new ad list item and we'll kind of look into what this looks like within the the
actual uh larl application but I scaffolded out a breeze application and then I added some different elements to it for every single item that we add we're basically just going to add a sleep to say hey this is an expensive database query we're adding a new item to the list your database is probably going to respond within a within a second if not less but um here's what
this kind of looks like so um this is without that in progress application this is just out of the box larable Live Wire now we could obviously add a new um loading spinner to this button to make this a little bit more um applicable that hey something is happening behind the scenes because this is a wire click we're not actually um making uh a a request well we're making Ajax request
but we're not actually reloading the page we're not sending a post request it's just kind of asking laravel to to hit this add new item function you can see here that um we're wire click add new item and then we're just uh looping through all the links so you can see here that this add new item it it it doesn't doesn't do anything I mean we could add a loading spinner to it but it
just is the button itself it just adds a new one you can see it takes 3 seconds to add a new one now live wire out of the box now with Live Wire V3 has wire um wire navigate so I was to look at our uh nav bar right here you can see here that we for every dashboard laser route in non- laser route we have wire
navigate appended now what this means is um anytime we make a request that takes longer than it's expected wire navigate basically just adds that cool little loading spinner up to the top and that does a couple other cool things basically like trying to pull the information on the click vers versus the upclick of the mouse and things like that but this is a lazy route where
we're actually loading the information until the um until the until the actual full page loads so let's go take a look at that lazy route real quick um so this is the uh lazy load option we have a placeholder of loading and then we're just waiting and and so what happens here is on the lazy view is we're just lazy loading this Live Wire component now I'm showing you all this to kind of
show what this end progress thing that Hassan was talking about actually does how how it makes your life easier as a larl developer so we have the lazy route where um every time we load this page it it loads everything this is that uh the optimization that JavaScript developers love talking about where we can load everything and wait for a specific expensive function to pull the data that we need so that way we're not waiting
for the whole page to load the other option just with wire navigate gives us is kind of giving us this cool little loading spinner up here on the top and once everything loaded since this is not lazy loaded we're just going to return the page after it's been all done same thing with this we don't have any way of indicating hey this is going on behind the scenes of course we could add that loading spinner to this button but let's add the end progress let's this is what
you all came here for so if I go to appjs we're going to uncomment this and let's go back now here's the cool part any single time we make any requests to Live Wire this add new item button the non- lazy route and the lazy route still works everything that happens is going to show that in progress Navigator it's like giving you Spa functionality without changing anything in your code
other than this you know eight lines of code it's insane watch add a new item look at we have this loading spinner at the top of course we can still have a loading spinner on the button but now we just get this functionality for free it makes it feel like a monitor our application and we still get that lazy loading functionality but but we get more indication behind the scenes that hey something is going on non- La route still does exactly the same because
we're using wire navigate so I just wanted to co show a quick um just a quick little uh you know test quick little demo of what Hassan's kind of showing what what he uh brought to light if you want to say it that way but I think it's it's one those things that JavaScript developers love talking about this and larel Livewire has that functionality while still giving you all the tools that laravel
gives