ajsheldon93's avatar

Laravel Vite with Laravel Valet on local network

I am having an issue exposing Vite to a local network. I want to get the live reloading from my computer but view it on my phone, both on the same local network.

I am using Valet to host sites and have a directory of projects parked. I have set up Vite with laravel-vite and everything works as expected with hot reloding when i run npm run dev.

I then added the --host to the command so I ran npm run dev --host. This gives a "Network: " url (an IP address with port). When I go to that page, it shows a laravel branded page with the following information:

This is the Vite development server that provides Hot Module Replacement for your Laravel application.

To access your Laravel application, you will need to run a local development server.

Artisan Serve

Laravel's local development server powered by PHP's built-in web server.

Laravel Sail

A light-weight command-line interface for interacting with Laravel's default Docker development environment.

This makes it seem like it can't work with Laravel Valet. I tried switching to Artisan Serve (starting the server, swapping out the APP_URL) and got the same results. (worked without --host but not on the network).

Any idea what I am doing wrong? Any documentation I am missing?

I am just looking to local network development and would like to not have to use something like Ngrok or Expose, not that i could get that to work properly either with Vite.

Thanks!

0 likes
13 replies
thinkverse's avatar

Well, just right off Vite doesn't run your Laravel application like Laravel Valet or artisan serve does. npm run dev spins up a Vite development server, all that does is re-build your assets when you update them, update your Laravel views or any other files and directories you've asked it to watch.

I haven't exposed it locally yet, but based on the fact that artisan serve and Valet runs on one port, and Vite runs on the other. Valet runs on :80 by default, artisan serve runs :8080 and the Vite server runs :5173 by default.

You can see this when you start the server with npm run dev. It tells you in the terminal which port is used.

VITE v3.1.4  ready in 1705 ms

Local:   http://127.0.0.1:5173/
Network: use --host to expose
// Using --host will result in this result below of IP_RANGE
// Network: https://{IP_RANGE}:5173/
// Network: https://{IP_RANGE}:5173/

LARAVEL v9.21.5  plugin v0.5.4

APP_URL: https://laravel.test

And the APP_URL is only forwarded from your .env file, it doesn't do anything in Vite, it's just convenient and quick way to open your Laravel app.

My suggestion would be to expose Vite on one port of your local IP and expose your Laravel app on another port. Like they do on your own machine.

1 like
thinkverse's avatar

Okay, did some research and got it to work using artisan serve and npm run dev. The order off if your start artisan serve or npm run dev first doesn't really matter, but I'd start Vite first to get hot reloading on the first load.

You need to find your local IP address. The IP that's used internally between your devices, on Mac you can find that under System Preferences, Network, and in there you should see your IP address, 192.168.???.???.

That's the IP you will pass in as the host for both artisan serve and npm run dev. In separate terminals run.

php artisan serve --host 192.168.???.???

For the npm command, we first need to add two dashed after the command and then pass in the flag.

npm run dev -- --host 192.168.???.???

Now you should be able to access your site locally from the IP and port that artisan serve gives you, for me, it was :8000.

4 likes
ajsheldon93's avatar

@thinkverse Thanks for the help! I was able to get it working using artisan serve and my local IP. Thanks for explaining the concepts out to better understand too!

This is definitely a good solve for now, but I would love to figure out how to do it with using Laravel Valet. I am constantly working on multiple projects so I "park" a directory for valet and would love to not have to start the Artisan Server every time. I am going to keep looking into it myself, but for now, I at least have a solve for the immediate needs.

Thanks!

thinkverse's avatar

@ajsheldon93 seems the Valet local share feature is broken, I couldn't get it to work either, got the 404 not found response. I did find a discussion on the repository - Unable to run website on local network, which does indicate that the feature is indeed broken, and given it's a year old and contains no input from the Laravel team. It means it'll probably not be fixed anytime soon. 🤷‍♂️

I'd stick with artisan serve and npm run dev for now. 👍

thinkverse's avatar

@ajsheldon93 after doing some further research it seems that support for this feature was never in Laravel Valet in the first place. But it was added to the documentation in Laravel 6 anyway. 🤷‍♂️ I've opened a PR to have the section removed, we'll see what happens. 🙂

ajsheldon93's avatar

@thinkverse I went the opposite direct and pinged them on twitter to see if they can fix it. If Vite is going to be the default going forward, I think this feature would be useful.

We'll see what they want to do.

1 like
Reppair's avatar

@ajsheldon93 have you managed to access your app on the local network using Vite yer? I have the same issues like described here.

hugonorte's avatar

I fixed the problem just updating the vite.config.js file.



export default defineConfig({
    base: '/',
    server: {
        host: '0.0.0.0',
        port: 8000,
        hmr: {
            host: '192.168.X.X', // Change this value for your local network ip address
            port: 8000, // Or your app's standard port
        },
    },
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/scss/app.scss', 'resources/js/app.js'],
            refresh: true,
        }),
    ],
});

Hope this helps you!

Reppair's avatar

This is annoying!

So much cool stuff going on, but we no longer can simply access our dev apps on the local network without messing around with proxies and stuff...

There were years where I would work with a tablet and a phone just to see all the breakpoints at the same time. BrowserSync, anyone? :)

1 like
hirasso's avatar

@reppair I was able to get browserSync to work with vite build --watch! Like so (contrived example):

As you can see, I'm using vite-plugin-browser-sync with the buildWatch option enabled.

This will print the browserSync URLs to the terminal on vite build --watch or vite preview, just as you remember it from the good old webpack days :)

Please or to participate in this conversation.