Struggling with Laravel + Vite
Laravel Herd has a nice `share` feature built in, but it doesn't handle Vite very well. Watch me fix that issue!
Everything I said
I've been working on the learning platform for highperformancesqlite.com. It's a Laravel app with a Vue front end and Inertia in the middle. Now, there's a bug. I don't know what it is, but there is a bug with HLS video on iOS mobile Safari, where sometimes the video starts, and then it stops. So I need a way to debug that on my phone without pushing to production, so I can make a bunch of changes here and see if I can get it to work.
So I need to expose my local development server to the internet, so I can hit it from my phone, and Laravel Herd has a way to do this built in, but it, it, it's a little bit finicky, and it doesn't quite work. It doesn't play nicely with Vite. So the way that we're about to do it is foolproof, if a little bit more code, but hey, at least it works. Let me show you how.
I've got a standard Laravel application here, nothing special. If we do herd open, you'll see it is just the Laravel start screen. This is a Vue plus Inertia app on the front end. Now, what we are going to do is we're gonna use ngrok to proxy to the site and ngrok to proxy our Vite server. So let's go ahead and touch ngrok.yaml.
We're gonna put everything in a configuration file, so we don't have to continually remember what the command is. So if we touch ngrok.yaml and hop over here, we can open ngrok.yaml, and I'm just gonna paste in my auth token. I have already rolled this, so good luck, have fun, and we're gonna start building out some tunnels.
We're gonna build out a site tunnel, and eventually, we're gonna build out a Vite tunnel too, but let's just start with the site. Now, what we need to do here is we need to tell ngrok, "Hey, whenever you hear a request, you need to proxy that to port 80 on my local machine." Um, you also need to tell ngrok what the host name is.
And so we are just gonna pick, uh, we'll just pick demovideo-aaronfrancis.ngrok.io. You can read their, uh ... You can read their docs for how you can, uh, claim these names. This is just one I'm just made up on the spot. Um, our protocol is, it's just HTTP, and our schemes, we're just gonna route HTTPS.
So if we do that and we come back over here, we can run ngrok, ngrok start. I think it's dash dash all config equals ngrok.yaml. So if we run that, you'll see, here we go. Uh, demovideo-aaronfrancis.ngrok.io is routed to local host port 80. So if I click on this, do you think it's gonna work?
I don't think it's gonna work. But if we click on it, you'll see, hey. Oh, that actually, uh, that actually did kind of work. So what we're seeing here is that, um, the request went out to ngrok. They proxied it back to us. We did land on port 80, but we're getting this Laravel Herd site not found issue, and that is because port 80, port 80 doesn't know which of the many sites it should route to.
And so Herd says, "Ah, I don't know what site this is. I'm serving the request. I don't know what site it is because I can't match up this domain to any of the sites that are parked by Laravel Herd." Now, remember, if we check, uh, demo.test, Herd says, "Yes, I'm routing that. Also, demo.test, I have a link to that directory, and so I know where to go get that application." We can give Herd a little bit of help here.
If we come in here, what we're gonna do is we're just gonna modify the request on the way in. So request header, what we're gonna do is we're going to, uh, we're gonna put a semicolon there, request header. We're going to add a header that says, uh, the host is actually demo.test. So instead of using, instead of using this as your host, oh, why don't we just pretend it's demo.test?
So if we then stop ngrok and start it again and then hit it again in the browser, you'll see that something changed. We still don't see, we still don't see demo.site, but we do see that something changed, which, you know, new errors are better errors, I guess. Um, the problem is, if we check the console here, the page at URL was loaded over HTTPS but requested an insecure style sheet.
So we have a mixed content warning. Um, we're gonna solve this temporarily by coming into the app service provider, and temporarily we're just gonna say, um, let's do URL, URL force scheme HTTPS. So we're gonna make everything be HTTPS, and now, great.
We're hitting demovideo.ngrok.io, and we have our full site loading with no mixed content warnings. So we can navigate. We get all of the assets. We're in a pretty good spot. This is a good start. Not gonna lie, good start, not good enough. But I, I need the Vite server running, and I need to proxy requests to that Vite server as well, so that I can get hot reloading.
I don't have to build every time. I just get the experience that I'm looking for. If we go back to the terminal here, we can run NPM run Vite. That's wrong. We can run NPM run dev. That's right. And it starts the Vite server at 5173. If we look at our shared URL, so the ngrok URL, it still works, which doesn't make a lot of sense until you realize that all of the Vite stuff is loading over local host, and I am on local host.
So it's going to work on this computer, but it's not gonna work, it's not gonna work on my phone. So you see, all these requests are being made to 127.0.0.1:5173, and that's just not gonna be available on other people's computers, obviously. So we need to do a t- a few things. First is we need to change that to be something else, and then we need to expose that port through ngrok.
So let's start by exposing it through ngrok. What we're gonna do is set up a new tunnel. So you can run multiple tunnels, which is quite nice. That's another reason we do it in this config style, so that we can have all of these tunnels together. And on the free plan, you can only run one CLI agent at a time, and so we use this config to get around that.
Um, so we're gonna just call this one Vite, and it doesn't really matter. We can call this Demo Video Vite, and we're going to route that to port five one seven three. We don't need the request header. So we'll pop back over here, and we'll stop this guy and start it again. And now you'll see we have two open, uh, two open proxies, and if we click on Demo Video Vite, it does look like it works.
Good. I was, I was hoping that that was gonna work. So now we have publicly exposed our Vite build dev server to the internet at demovideovite-aaronfrancis.ngrok.io, and this is the start screen right here. We've got Vite running, and it's being proxied by ngrok, but we're not referencing it yet. So if we check back in here, and we look at that, you'll see we're still referencing the one twenty-seven.
Even though we have proxied this outside address to it, Laravel doesn't know to use the new address. So where's this one twenty-seven five one seven three coming from? And that is coming from, if we open this up, and we check in public hot. That is where the hot reload URL is coming from.
So if we were to... What did we name our ngrok one? We named it demovideo vite aaronfrancis. So we'll come over here, and we will replace the URL with our ngrok URL. And now, if we hit it, we should see that it still works. And we'll open up the network tools and check in here, and we'll see, where are these coming from?
There you go. So now, um, now the site is requesting the assets through ngrok back to our local machine, and then it serves it all the way back around. So we have-- we've got it working now, but it's pretty ugly, and we don't do ugly here, so let's make it pretty. We're gonna do php artisan make command, and the command we're gonna make is just share.
So we're gonna make that command. We're gonna stop Vite for now. We're gonna stop ngrok for now, and let's go work on this share command. So opening up share and getting rid of all of this good stuff. We're gonna change it to just share. We can get rid of that. I'm not gonna write a description because we know exactly what we're doing here. So what do we wanna do? Um, I think we wanna kind of like handle all of the grossness inside of this command.
And so we want to start ngrok. We want to force probably, force URL, um, scheme, whatever it was, and we need to update the hot file. So let's just get started. Um, the first thing we're gonna do is say, uh, that the path equals...
No, let's say NGROK_CONFIG equals base path ngrok dot YAML. Is that what we named it? We did name it ngrok dot YAML. So if not file exists NGROK_CONFIG, throw... We're gonna throw a new exception that says, uh, config, config not found, looked for, and we'll just say NGROK_CONFIG.
So that should tell us that we're okay. So if we just say this line. Okay. We're gonna use this to test. Um, AR share. Okay, good. So yeah, not a bad start. Let's use Laravel's process facade and say process start, which I believe puts it in the background.
I hate it when PHP Storm renders that. So if we say start, uh, invoked process, returns invoked process, I believe this puts it in the background. And so what we're gonna do is we're going to run... What is the command? I think it's ngrok dash dash... Not ngork. Ngrok start dash dash all dash dash config equals, and then we'll replace that and put NGROK_CONFIG.
And so we'll call this the ngrok process, and then we'll just dump ngrok is running. Is it just running? Running. So if we do that, and then we just sleep for... We'll just sleep for two seconds so we can see. So if we do that, it does say that it's running, and then it dies, which is good.
That's a good start. So the ngrok process gets started, and now we need to, uh, we need to watch for the hot file and put our URL in there instead of having to manually do that. Now, here's the tricky thing about the hot file. It could be there and then be gone and then be there and then be gone, and so we have to make sure that we're constantly putting the correct URL in there.
'Cause every time you start the Vite dev server, it creates that hot file. So we've already started ngrok. We'll deal with force URL. So this is the one that we want to do next. And we'll say, if file exists, um, let's go ahead and just pull this out here. We'll say that the hot path equals... Where would that be? I think that's public path hot.
Is that correct? So if we go to public, it should be... Yeah, it's gonna be in that directory. You see it's not there because I stopped the dev server. So public path hot. If file exists hot path, um, then we need to, we need to make sure that we put our URL in there. But I kinda also, I kinda also wanna be respectful and save any value that was there so that we can put it back how it was.
Uh, we'll do, let's say hot contents equals nothing. So no hot contents. In fact, that's silly. Let's do equals null. If file exists, then hot contents equals... Let's do question question equal, and we'll do file get contents hot path. So this means right here that if hot contents is null, then we're going to stash away the current contents of the hot file so that we can put it back later.
And once it is written, this dash dash equals means it won't be overwritten. So we're just gonna write it once. And then what we actually wanna do is we wanna do file put contents, hot path, and then something. What are the contents going to be? We need that URL. Uh, let's make sure we got these order right, string, file name, and data. So we got that order right.
So we need the Vite URL right there, and where can we get that? We can get that from the ngrok config. So we're gonna pull in Symfony's YAML parse file, and I think we can just do ngrok config, and we'll do config equals DD config. So I just actually don't wanna start ngrok for this test.
And so now if we run AR share again, boom. There we go. So I'm looking for, uh, tunnels/vite/hostname. That is what I'm going to put in the hot file. So I will do, um... What do you wanna do? Let's do, uh, let's do the array getter. So we'll do array get from config.
We want tunnels.hot... Nope, vite.hostname. And let's just pull this out to make sure that we did that right. We'll do that and wrap it up, DD, and come back here. And hey, there we go. That's exactly what we're looking for. It does, however, need to be prefixed with HTTPS 'cause that's the only scheme that we're serving over, so we're safe to just hard code that.
So we can come back over here and say that we want HTTPS and then that guy. All right. Let us slow down and walk through this again. So the first thing we're gonna do is we're gonna start the ngrok server, uh, to make sure that it's prox- proxying requests, and then we're going to while true.
So in a loop, we're gonna see if the hot file exists. If it does exist, we're gonna stash the contents one time only, and then we're going to overwrite the contents with our value. So we've done this now. We can now sleep for... Let's sleep for three seconds so that we're not just, like, burning up CPU for no reason.
Um, we also need to clean up. We gotta, we gotta undo all of this. We don't want this to, we don't want this to stick around. Um, okay. What do you say? I say we give it a shot. This seems good to me. So clear this out, make sure that nothing else is running. In fact, let's just close those. Uh, actually, I need one to start the Vite server. So if I do artisan share, we don't see anything coming up because we're running in a while true loop.
So we'll add some logging here in a second. So I think first let's just see. Okay, so ngrok is running 'cause I'm able to hit the URL. And then if we were to run Vite... No, why do I keep doing that? NPM run dev. Now the Vite server is running, but let us check the hot file.
Hey, look at that. That is working quite nicely. So if we hit this over here, that still works, and we check the network for where all of these assets are coming from, and that's the wrong one. And that still works. Demo, video, Vite, Aaron Francis, ngrok. That looks pretty good, honestly. I'm, I'm, I'm, I'm pretty happy about that. I've wrapped it up all nicely.
There are a few things we still need to do. There are a few things we still need to set, and we gotta clean up after ourselves. This is, uh, this is very invasive, and then it doesn't, it doesn't undo itself. It doesn't fix everything. So let's fix everything. I am going to start, in fact, um, I'm going to start by... We need to undo this.
And the way that I'm gonna do this is, let's say public function, watch hot file. And what I'm gonna do here is if we were to pull... Yeah, yeah, yeah. Let's pull this out and drop it in here. And what do we need? We need hot path and config. Um, hot path we can bring down. We don't actually need that up there at all.
Hot path, hot contents we can bring down. Those can go there. We just need config, and in fact, I think I'm gonna use config in a few other places. So let's come up here and say that protected... I'll do a type. I'll do a type just for you guys. Protected array config, and this config. So now we're good here.
And what I'm gonna do is this, uh, what did we call it? Watch hot file. What I'm gonna do is clean or rather restore hot file equals that. This is an infinite loop, and so then I'm going to say restore hot file. Interesting. Interesting, interesting, interesting.
So what I need to return from here is I need to return, um... This is an infinite loop, so it's not gonna work. But theoretically, what I need to do is return a function that, uh, puts the hot contents back. So we'll do this again. We'll say, if the hot path exists, I don't wanna stash it, and I don't wanna put that in there.
So if we quit sharing while the dev server, while the Vite server is still running, we need to put the real URL back in, and we can do that because we stashed the hot contents. So hot contents, we'll need to pull both of those into scope. If we do hot path and hot contents, we should have a cleanup function.
It's complaining probably because this is unreachable. Yeah. It's unreachable 'cause it is an infinite loop, which we're gonna fix in a second. Just give me a break. All right, so we restore the hot file. So it returns its own undo function, which, come on. Hey, that's pretty awesome. We need to break this. So if we say, if this, uh, should exit, then break.
And that should make PhpStorm happy. PhpStorm's never happy, um, but it's pretty happy here. We need should exit. So we do protected bool shouldExit. Should exit equals false. So now we have that. And how are we going to signal that we should exit?
I gave it away. We're gonna use signals. We're gonna use signals. Um, we're going to- we're gonna say that this is a signalable command. Implements signalable, goodness, signalable command interface. Woof. That's a mouthful. All right, we have to implement two methods. And so what we're gonna say down here is we're gonna say that this listens to SIGINT and SIGTERM, listens to both of those things, and then what it actually does is
it just sets... When it hears the signal, it just sets this shouldExit equal to true, and it returns false. So it doesn't actually exit right away. So we can say inside of here... Let's do this. Let's do this line, um, watching the hot file.
Um, and then we'll give a- give ourselves a little instructions as a future kindness. Press Control + C to stop. Okay. So now this should run, and we'll stop that. Still infinite looping. Press Control + C to stop. We need to see if our cleanup function actually worked, I guess. Uh, so let's check in here.
Um, so we'll check the hot file is set to demo Vite. Let's close it. Control + C to stop, and then please. Hot file is still set to demo Vite. So what did we do wrong? Um, that was unlucky. Uh, I don't know that we did anything wrong. So why didn't it work is the question.
DD restore hot file. Oh, I think I know why. Um, 'cause I think we didn't restore it in the first place. So let's do this. Let's stop this. Stop that. Everything stopped. Let's start the Vite server. So we're gonna run dev. So hot should say yep, and now if we start our share, we're gonna start the share, and hot should be updated.
And now if we stop the share, hot should go back to what it was. All right, so we've successfully implemented cleaning that up. Let's go ahead and just... Let's go ahead and clean up Ngrok too. Um, I do love this cleanup style. Public function startNgrok, and we will bring this down here, and we'll see if- if we need anything there.
Ngrok config. We do need Ngrok config. Do I wanna set it? Yeah. Yeah, yeah, yeah, yeah. That's fine. We'll say this Ngrok config. Um, so let's do this. Why don't we do it this way? Uh, yeah, let's do this. We'll set Ngrok config. Um, we'll call this a string.
I'm gonna rename this, but we'll call this a string. So we're gonna call this, uh, we'll call... Nope, nope, nope, nope. Let's go ahead and update this. This Ngrok config, and this Ngrok config, and this Ngrok config. Now, we're gonna change this to be a little bit clearer, Ngrok config path, and we're gonna change this to be Ngrok config as the actual array.
Come on, PhpStorm. There we go. All right. Um, oh, shoot, I misspelled it. Ngrok config. Now, we set the path as a class level variable. Uh, string, yep, and then we say, "Hey, couldn't find it. That's too bad." And then we set, um... Yep, so we're good there. So now we need... We can just call this Ngrok config path, 'cause that's the actual string, and we'll say that that is Ngrok.
Now, we need to return a cleanup function. What's a cleanup function gonna look like here? We'll return a function that says, um, if Ngrok, we gotta use it. Oh, PHP. Uh, if Ngrok running, um, yep, Ngrok signal.
Uh, what do we wanna tell it? SIGINT. S- stop running. That seems good enough to me. So coming back up here, we can now say, um, stopNgrok equals this startNgrok. Kinda cool. So we will stopNgrok, and then... Love that.
So now we're cleaning up quite nicely. Remember, this is an infinite loop, so that is going to run forever. So if we... Are we? We're, uh, so we'll share here, and then we'll come over here, and we'll do PS OX grep Ngrok. We are running Ngrok start all. And now if we interrupt this, it should have stopped.
Yes, so we have successfully killed the process. Did the process kill itself, or did we kill it? Doesn't matter. It's dead. That's all that matters. Now, there's still a few things I wanna do here, but hey, are we... We're having fun, aren't we? We're having fun. We're- we're- we've figured out signalable commands, which are pretty cool. We- we figured out this return and undo function, which is pretty cool. We're starting Ngrok.
We're proxying to Vite. If this isn't fun, I, uh, honestly, I d- I don't know what is. Um, but there's more to do. It wouldn't be a TryHard Studios Aaron Francis video unless we did way too much and went way too far. So hey, hey, let's keep going. All right. What I'm gonna do here, because I don't like this, right?
I don't like the URL for scheme. This is a detail that is only applicable to sharing in our case. So I don't like that. I don't like that one bit. So what we're gonna do is AR make provider share service provider. We're going to make our own provider that we can monkey around with. So- Share service provider.
And if I recall, which sometimes I don't, but if I recall correctly, we don't have to do anything as Laravel should have wired this up for us. So we can say, "Thanks, Laravel." It wired it up for us. So we don't have to do anything. The first thing I do wanna do is say app is local. If we're not local, get out of here.
Why, uh, why would I be sharing a production site that already has a live URL? Because I'm a silly boy. I would never do that, so let's get out of here. The second question is, are we currently sharing? Um, don't know. If we are... Well, if we are not, bail. If we are, we gotta do at least that.
We at least have to force HTTPS because we set up ngrok to only serve HTTPS, and so we have to force that. Um, we'll have to figure out how to do this. There's no good way that I can think of. So where there is no way, we shall find a way. Uh, let's say, um, start the share again. So it's watching the hot file.
It's serving ngrok. And if we hit demo.test, we're good. In the ngrok tunnel, we are good. And if we click, we click there, we're good. Um, okay. So we are still able to navigate. We're still loading assets from our proxy, so we're in a good spot. The question is, are we currently sharing?
Who knows? Who can say for sure? I can say for sure. So watch this. There's no good way to do it, and we shall make a way. This touch share file, eh, good enough. Remove share file. This is cool. I do like this. I must admit, I do like this. Remove share file. So we could do public function touch share file.
And what we're gonna do here is, um, let's say file put contents, and we're just gonna have storage path share file. Just made it up. What are we gonna put in there? Something interesting, I think. Let's, uh, let's put this in here. Let's put the host name, but not the host name of Vite.
Let's put the host name of the actual site that we're working with. And so if we come over here and change Vite to site, and then what's our return function gonna be? We shall return and, uh, unlink. But it might not be there, so we're gonna use warp suppress. So we will suppress it if it is not there. Um, that's some old school PHP right there.
So we shall suppress and unlink. So this will create a share file, and then it will, uh, remove that share file as a part of the cleanup function. I don't wanna add return types. Remove share file, remove share file. Cool. Let us run. So we're gonna run share and then hop over here and pop over to storage share file.
Hey, look at that. Cool deal, man. Cool deal, man. So if we come back to our share service provider, are we currently sharing? So if it's not local or, um, the file doesn't exist, and we called that storage path share file. Um, if the file doesn't exist, get out.
So that's bail. Then we need, uh... What do we need? Let's go ahead and get the URL out of it, and we're gonna prepend HTTPS. We're gonna say file get contents for storage path share file, and that should give us our URL. And then we're gonna do a few things. We can do URL.
Uh, where are you? Facades URL force root URL. We're gonna force it to be that, and then we're gonna do, um, let's do config just to set it everywhere. We're gonna do config set, and we're gonna set, um, is it app.url? I think it's app.url. Maybe it's-- I think it's just URL after it gets in.
So if we set URL to URL, and we're gonna set the asset path while we're sharing to just the, uh, root domain. And this can cover a few issues when you're doing Vite build, and it just gets a little bit wonky. There's wonkiness everywhere. Um, so I think this should be right. I do wanna check, though, just to make sure.
So let's go to vendor and Laravel, and then I'm gonna search for app URL to see where that ends up. Um, config app PHP. Yeah, so I think we need to set, um, I think we need to set app.url. Um, no, maybe not.
Let's just check. So if we come back here, and we do config, uh, is config URL, is that a thing, or is it app.url? That would be what we're trying to figure out here. So if we refresh that, null. And if we set app.url. Okay, so it is app.url.
So can we set it nested like this? So if we do dump that, and then we set this, this is like live debugging. Fubar, and we dump it again down below. Did we affect change? We did. So I think that is right. Let's just check asset path as well.
I think asset path is gonna be null. And then asset path should be set. Cool. So we verified that we are setting everything correctly. We need to change this back to URL, and we can close this out. Now, what we've done is we've read the URL out of the share file. So on our share command, we put in the ngrok URL.
And then over here, every time a request comes in, we're reading that out, and we're saying, "Hey- Surprise, uh, the URL is actually different than what you expect because we are proxying. So let's set, set it to URL. Let's cover a few, uh, weird errors by setting the asset, asset path to the base URL. Then we're gonna force the URL, and we're gonna force the scheme only if we are sharing.
So all of that being said, we can stop this. Vite is still running AR share, and we should have everything we've ever wanted. So, oh, nope, that didn't work. It's already bound to another tunnel session, so okay, let's try this again. There we go.
And if we do log in, we see log in. We do register, we see register, and we check the share file should be set correctly. If we open the share file, share file is set correctly. I think we've solved it. Um, is there anything else we need to do over in here? Let's make this just a little bit nicer.
Um, so we can say this, uh... Let's just put out a little bit of information. Ngrok has been started. The PID is Ngrok, uh, ID. So yep, PID is that. And, um, we can say this line, um, touching, touching the share file.
In fact, let's, um, let's add a little bit of logging everywhere so we can make sure. So removing, removing the share file. Um, we can come down here in this cleanup and say, "Stopping Ngrok." I don't know that this is necessary because this is a child process of this process, and that's why I wrapped it in the, in, uh, the running because it might have actually...
I don't know. It might stop itself. So removing share file. And then what else have we done? Watch hot file. Um, okay. Yes, yes, yes. And we can say restoring hot file. And that will be a little bit nicer. It's over the top, but that's how we do it.
So if we check AR share. Touching the share file. Ngrok has been started. PID watching the hot file. Press Control + C to stop. Cool thing is I can stop, uh, Vite over here and start it again, and it'll pick the hot file back up because it is running in a loop. And so just confirm, hey, everything works. That is cool.
Control + C to stop. Removing the share file. Stopping Ngrok. Restoring the hot file. Let's just add, um, right down here should exit is true. This line nothing. Just to make it, goodness gracious, just to make it a little bit... That's the wrong one. Just to make it a little bit better. AR share.
Hey, cool. Great. Stop. New line. Removing, stopping, and restoring. I think that's pretty good. I think that's pretty good. We just need some periods. Removing, uh, touching the share file. Removing the share file. Why am I like this? We may never know. Restoring hot file. There we go. I think we can call it a day here.
Yes, yes, yes, and we're good. PID needs a period, but after that, I'm gonna let you go because this is getting to be a little bit much, isn't it? So there we go. We did it. It's a lot, right? I told you it was a little bit finicky, but we've, we've solved for all use cases, I think. You can start and stop the Vite dev server. You can run Vite build. You can start and stop Ngrok.
You clean up after yourself. Everything should work. HTTPS should be fine everywhere. You're not gonna have any mixed content warnings. I think this is awesome. I don't know if there's a better way. There might be a better way. Laravel might come out with some other different system. If you think that there's a better way, please let me know. If you do think there's a better way, make sure that it covers the Vite dev server because that was the tricky part for me.
That's why we have to watch that hot file. But I'm always open to suggestions. This is the way that I did it, and I'm happy that it works, but there might be a better way. If you find that better way, let me know. Until that time, see ya.