This is something I am also struggling with. I have a local setup with a couple servers I host projects on. Since vite came into play, I have had to use a local (WSL) instance of laravel to get vite working properly. I would love to know how to do a setup with laravel on a remote sever as I switch between a desktop and laptop regularly.
Vite doesn't work on remote dev servers
We have a development server with lots of developers and lots of sites. We setup each site as a ghost and this makes it great to share with other staff and management and not have to worry about things like firewalls and DNS on lots of different machines.
However the recent switch to Vite seems to break this and I'm looking for advice on how to fix it.
I install a new project with laravel new some-web-site.co.uk --jet --stack=livewire and at the end of installation it automatically runs npm run dev which calls Vite's server. Doing the exact same thing locally with docker works fine, however on our dev server this obviously won't work because localhost makes no sense. It doesn't make a difference when I use --host either but I presume more would need configuring?
Is it possible to setup Vite for remote servers? It seems the documentation concentrates on docker and local development and not development servers.
Update: Thanks a lot 4wk for suggesting an improvement on the Gist of this! Updating here to reflect that change
We ended up achieving this by customising the Vite config. I'm not Vite/JS expert so I welcome feedback, but this solution works for 6+ developers on a remove development server. Additionally we store the config in our .env files and allow Vite to load from there.
https://gist.github.com/legecha/afccb720edc799c04a0d698a4f6f7823
/**
* This vite config allows you to run `npm run dev` in Laravel and access
* resources, including with hmr, in the same manner as if you were
* developing locally.
*
* All config is stored in your standard .env file and multiple users can
* have their own instance running by specifying a unique port.
*
* The certificates are required - use the same ones as used on your
* web server to secure your development domains (for instance letsencrypt).
*
* Add this to your .env file:
*
* VITE_ASSET_HOST="your.development.url.com"
* VITE_ASSET_PORT=5173
* VITE_PRIVKEY_PATH="/path/to/certs/privkey.pem"
* VITE_CERT_PATH="/path/to/certs/cert.pem"
*/
import { defineConfig, loadEnv } from 'vite';
import laravel from 'laravel-vite-plugin';
import fs from 'fs';
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
server: {
host: true,
port: process.env.VITE_ASSET_PORT,
strictPort: true,
hmr: {
host: env.VITE_ASSET_HOST,
port: env.VITE_ASSET_PORT,
},
https: {
key: fs.readFileSync(env.VITE_PRIVKEY_PATH),
cert: fs.readFileSync(env.VITE_CERT_PATH),
},
},
plugins: [
laravel({
input: ['resources/css/base.scss', 'resources/js/app.js'],
refresh: true,
}),
],
}
});
Please or to participate in this conversation.