Solution:
Thanks to @parastoo it works now. I had to spin up the dev server like so (2 different terminal tabs):
vite --host=HOST_IP
php artisan serve --host=HOST_IP
then connect with your mobile device (which is connected to your wifi) to:
http://HOST_IP:PORT
HOST_IP can be seen in the terminal when you run vite --host
PORT can be configured by adding --port=8000
to the artisan command.
No additional entry in vite.config.js
was required.
Original Question
I’m using inertia, a monolithic approach to develop apps with a frontend framework like vue
and laravel
as backend. I’m trying to connect a mobile device from my network to my development server, which uses vite
with php server
:
- run vite:
vite
- run php server:
php artisan serve
The site is served from http://localhost:8000
. From How to expose ‘host’ for external device display? #3396 I read, that you can do something like this:
vite --host
which should expose your network:
vite v2.9.13 dev server running at:
> Local: http://localhost:3000/
> Network: http://192.xxxxxxxxx:3000/
ready in 419ms.
but when I try to connect to the network url on my phone, this page can't be found
. I’ve also tried to connect with port 8000
which shows this site can't be reached
.
Any way to make it work?
5
Answers
I would recommend using Chrome and just "Toggle device toolbar" – F12 and Ctr+Shift+M
Or both devices should be on the same network and check if firewall allows connection on 3000 port;
Your php terminal is running on localhost:8000
So you can run on php artisan serve with host.
You should connect to the same network, then check your Local Ip and serve the Laravel project by:
** you should disable the firewall **
updated
Add this config to your
vite.congif.js
fileFor example this is my config file :
Then run this command and add the host that you served with Laravel:
These commands should run together with same host:
Vite uses its own dev server, so you can’t just use php artisan serve. You need to use the vite dev server.
https://github.com/vitejs/vite#how-does-it-work
If you want to use php artisan serve, you can follow the instructions in the link above. You’ll need to use the php server in your browser though, so http://localhost:8000/
*** Clarification
Given all the answers, I couldn’t figure out why hot reload didn’t always work. With this answer, I’d like to clarify some steps.
For a mobile device to see the page, it has to be served on a local network starting with
192.*.*.*
(e.g.192.168.1.130
).In the first terminal
Now you should be able to connect to the server from your mobile with
http://192.168.1.130:5173
. However, the hot reload doesn’t work yet. For that, let’s start the Vite dev server.In the second terminal
This will start the Vite development server on the same network, but it will use a different port (in my case it was
5174
) which is fine because the hot reload works as expected.CAVEAT: if you run commands the other way round,
php artisan serve
either can’t connect to the same port, or if it connects, the hot reload doesn’t work.**
*** Improving Workflow (optional)
I was annoyed every time typing
--host
and--port
commands so I made some changes to the.ENV
andpackage.json
files..ENV
package.json
Now to start the workflow, just open two terminals and type
php artisan serve
andnpm run dev