connorm's avatar

Laravel Vite Issue

My application is using Inertia.js, and I've followed the migration guide from Laravel Mix to Vite. I'm fairly confident I have everything done correctly; here's my terminal output after running npm run dev (note: I'm using Laravel Sail too):

vite v2.9.13 dev server running at:
Local:    http://localhost:5173/
Network:  http://172.19.0.4:5173/
 
ready in 2466ms.

Laravel v9.19.0
APP_URL: http://localhost

...however, when I go to my application in the browser, I see a blank screen, as well as the following console errors:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://0.0.0.0:5173/@vite/client. (Reason: CORS request did not succeed). Status code: (null).`

Module source URI is not allowed in this document: “http://0.0.0.0:5173/@vite/client”. login:19:1

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://0.0.0.0:5173/resources/js/app.js. (Reason: CORS request did not succeed). Status code: (null).

Module source URI is not allowed in this document: “http://0.0.0.0:5173/resources/js/app.js”.

I've compared my own project's config to one of the Laravel Starter Kits that uses Inertia + Vue as well, but I'm still having no luck.

0 likes
71 replies
Sinnbeck's avatar

Please show the vite config. What domain are you serving laravel on? Localhost?

1 like
connorm's avatar
connorm
OP
Best Answer
Level 1

@Sinnbeck Figured it out.. it was my docker-compose.yml file that was missing:

${VITE_PORT:-5173}:${VITE_PORT:-5173}

Thanks for chiming in though :)

2 likes
connorm's avatar

@rsarvarov

# For more information: https://laravel.com/docs/sail
version: '3'
services:
  laravel.test:
    build:
      context: ./vendor/laravel/sail/runtimes/8.0
      dockerfile: Dockerfile
      args:
        WWWGROUP: '${WWWGROUP}'
        NODE_VERSION: '16'
    image: sail-8.0/app
    extra_hosts:
      - 'host.docker.internal:host-gateway'
    ports:
      - '${APP_PORT:-80}:80'
      - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
    environment:
      WWWUSER: '${WWWUSER}'
      LARAVEL_SAIL: 1
      XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
      XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
    volumes:
      - '.:/var/www/html'
    networks:
      - sail
    depends_on:
      - mysql
      - redis
  mysql:
    image: 'mysql/mysql-server:8.0'
    ports:
      - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
      MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ROOT_HOST: "%"
      MYSQL_DATABASE: '${DB_DATABASE}'
      MYSQL_USER: '${DB_USERNAME}'
      MYSQL_PASSWORD: '${DB_PASSWORD}'
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
      - 'sailmysql:/var/lib/mysql'
    networks:
      - sail
    healthcheck:
      test: [ "CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}" ]
      retries: 3
      timeout: 5s
  redis:
    image: 'redis:alpine'
    ports:
      - '${FORWARD_REDIS_PORT:-6379}:6379'
    volumes:
      - 'sailredis:/data'
    networks:
      - sail
    healthcheck:
      test: [ "CMD", "redis-cli", "ping" ]
      retries: 3
      timeout: 5s
networks:
  sail:
    driver: bridge
volumes:
  sailmysql:
    driver: local
  sailredis:
    driver: local
rsarvarov's avatar

@connorm Cannot make it work :( Can you help?

I have that docker-compose:

version: '3'

networks:
  laravel:
    driver: bridge

services:
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    restart: unless-stopped
    tty: true
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel
      
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
      - ./conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - pgsql
      - redis
    networks:
      - laravel

  pgsql:
    image: postgres:14
    container_name: postgres
    restart: unless-stopped
    tty: true
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: farmweb
      POSTGRES_USER: farmweb
      POSTGRES_PASSWORD: secret
      PGPASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: postgresql
    networks:
      - laravel
      
  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    networks:
      - laravel

  npm:
    image: node:16
    container_name: npm
    volumes:
      - ./src:/var/www/html
    ports:
      - "3000:3000"
    working_dir: /var/www/html
    entrypoint: ['npm']

  artisan:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html
    depends_on:
      - php
      - pgsql
    working_dir: /var/www/html
    entrypoint: ['php', '/var/www/html/artisan']
    networks:
      - laravel

  redis:
    image: redis:7-alpine
    container_name: redis
    restart: unless-stopped
    tty: true
    ports:
    - "6379:6379"
    networks:
      - laravel

And I have default vite port in Laravel - 3000.

Where I need to put it?

iraklisg's avatar

@rsarvarov I have a similar local docker environment (seperate containers for php-fpm and nginx) and I too strungle to make vite.js dev server work :(

Sinnbeck's avatar

@rsarvarov if you are on windows, check out the links I posted below. It's a known issue and they are working on a fix

1 like
rsarvarov's avatar

@Sinnbeck yup I'm on Windows (WSL2). I read #28 issue, but not sure that it's my case.

I downloaded "use-hmr-host-when-set" branch files, then compile it by running run npm run build and then replaced node_modules\laravel-vite-plugin\dist with compiled files.

So then I run npm run dev inside my docker container and trying to fetch http://localhost:3000/@vite/client from my browser I have connection refused.

My vite config:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    server: {
        hmr: {
            host: 'localhost',
        },
    },
    plugins: [
        laravel({
            input: 'resources/js/app.js',
            ssr: 'resources/js/ssr.js',
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

https://user-images.githubusercontent.com/27378369/176599295-9da72b2d-7774-4239-a8f1-351c1316a605.png

2 likes
Sinnbeck's avatar

@rsarvarov What url is it showing in the npm run dev window? What urls are shown in the source of the webpage itself (the script tags)?

1 like
rsarvarov's avatar

@Sinnbeck

 vite v2.9.13 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 7227ms.

In page source:

 http://localhost:3000/@vite/client
http://localhost:3000/resources/js/app.js

Both urls net::ERR_CONNECTION_REFUSED

Sinnbeck's avatar

@rsarvarov I just noticed that you have port 9000 open but not port 3000 ?

ports:
      - "9000:9000"
      - "3000:3000" //add me and rebuild
2 likes
rsarvarov's avatar

@Sinnbeck I run all npm commands inside NPM container =(

npm:
    image: node:16
    container_name: npm
    volumes:
      - ./src:/var/www/html
    ports:
      - "3000:3000"
    working_dir: /var/www/html
    entrypoint: ['npm']

I opened port for that container.

rsarvarov's avatar

@Sinnbeck I opened these ports yesterday, it didn't give any results. I'll try to ask help at vite plugin github page.

rsarvarov's avatar

@Sinnbeck if I run:

docker-compose -f docker-compose.dev.yml run --publish 3000:3000 npm run dev

It now works! But idk why without "publush" docker don't open the port, even I specified it in docker-compose.yml

rsarvarov's avatar

@Sinnbeck Yup, I do docker-compose -f docker-compose.dev.yml up -d --build

Thank you for help!

Sinnbeck's avatar

@rsarvarov Yeah I believe they have that built in to their vite integration IF you use sail

Sinnbeck's avatar

@rsarvarov Anyways. I am going to stop replying on this thread. Each time we reply @connorm with get an emai, and I dont want to spam them. The thread is also marked as solved. Feel free to create a fresh thread that you own

1 like
connorm's avatar

@rsarvarov @iraklisg I'm not sure if this will work, but try this for your nginx container. The key for me was not having that Vite port open. That's what the local dev server appears to be looking for.

  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "80:80"
	  - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
    volumes:
      - ./src:/var/www/html
      - ./conf/nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - pgsql
      - redis
    networks:
      - laravel
renatoxm's avatar

Hey @connorm, can you post your vite.config.js ? I have the same issue, and my docker-compose.yml is exactly equal to yours.

mine is:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    plugins: [
        laravel({
            input: 'resources/js/app.js',
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});
renatoxm's avatar

@connorm

Failed to load resource: net::ERR_ADDRESS_INVALID http : //0.0.0.0:5173/@vite/client

Failed to load resource: net::ERR_ADDRESS_INVALID http : //0.0.0.0:5173/resources/js/app.js

PS: i just spaced url above 'http : //' so I could post the error.

my Laravel Sail running like this:

 vite v2.9.13 dev server running at:
  > Local:    http : //localhost:5173/
  > Network:  http : //172.27.0.6:5173/
  ready in 299ms.
  Laravel v9.19.0
  > APP_URL: http : //localhost

testing this in chrome browser in

http : //localhost/
connorm's avatar

@renatoxm I know you said it's the same, but post your docker-compose.yml file.

renatoxm's avatar
 vite v2.9.13 dev server running at:
  > Local:    http : //localhost:5173/
  > Network:  http : //172.27.0.6:5173/
  ready in 299ms.
  Laravel v9.19.0
  > APP_URL: http : //localhost

running dev server with

./vendor/bin/sail npm run dev

production runs ok with

./vendor/bin/sail npm run build
1 like
renatoxm's avatar

docker-compose.yml

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.1/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
            - redis
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ROOT_HOST: "%"
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 1
        volumes:
            - 'sail-mysql:/var/lib/mysql'
            - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sail-redis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local
connorm's avatar

@renatoxm Everything looks good to me.. you have @vite('resources/js/app.js') inside of you in app.blade.php?

renatoxm's avatar

@connorm yes... :-(

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title inertia>{{ config('app.name', 'Laravel') }}</title>

        <!-- Fonts -->
        <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap">

        <!-- Scripts -->
        @routes
        @vite('resources/js/app.js')
        @inertiaHead
    </head>
    <body class="font-sans antialiased">
        @inertia
    </body>
</html>
Sinnbeck's avatar

@renatoxm any chance you are using brave? It blocks vite for some reason and you need to allow it

2 likes
baumgars's avatar

@renatoxm i am not running docker but running it directly on amazon ec2.

" npm run build" for production was the solution here for a rather mystical problem which i explain now.

I was running a project with "sudo php artisan serve --host 0.0.0.0 --port 80" on a remote amazon server and wanted to access it via my local Browser on Windows. I ran "npm run dev" with and without "-- --host". Without host i had a "connection refused" on 127.0.0.1. No idea where 127.0.0.1 came from and how to ajust it and to what. With "-- --host" i got a "address invalid" with a cryptical URL of "http://[..]:5173" and thats it.

I wasnt aware i need to build the assets with "npm run build" and dont need a running "npm run dev". Access it either via "php artisan serve" or apache or whichever server serves pages.

renatoxm's avatar

Not that i know of. Im running ubuntu under wsl2 (windows)

Sinnbeck's avatar

@renatoxm brave is a browser :) just helped someone else today that had the exact same issue and it was due to the browser. Changing browser fixed it

connorm's avatar

@renatoxm At this point, I would start looking at the official Vite docs for potential WSL2 related hiccups/steps.

1 like
Sinnbeck's avatar

@renatoxm do you have npm installed in Linux? If so, could you try starting the dev server there as a test?

1 like
renatoxm's avatar

@Sinnbeck yes... tried with no luck.

Also tried 'usePolling: true,':

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
    server: {
        watch: {
            usePolling: true,
        }
    },
    plugins: [
        laravel({
            input: 'resources/js/app.js',
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});

and 'vite --host' at package.json

    "scripts": {
        "dev": "vite --host",
        "build": "vite build"
    },

always get the same error...

no luck until now :-(

serge-benard's avatar

@renatoxm Hello! I have the same issue, with the same setup: A new Sail install running on Docker spun up from WSL2

No solutions yet. Generating the scripts with sail npm run build works fine, so I know Vite works; it's just the server.

I spun down my project build and did a ping 0.0.0.0 and it returned the following:

PING 0.0.0.0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.033 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.031 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.037 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=64 time=0.033 ms
^C
--- 0.0.0.0 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4177ms
rtt min/avg/max/mdev = 0.016/0.030/0.037/0.007 ms

So I know my machine is running a server at 0.0.0.0... I don't know what, though!

What a weird bug. I am frustrated!

4 likes
Sinnbeck's avatar

@serge-benard 0.0.0.0 is "all traffic". But windows might send it to a different place (eg your router or something)

renatoxm's avatar

@Sinnbeck no luck... just updated laravel-vite-plugin and tryed to add hmr.host, to defineConfig but no luck.

That's really frustrating

export default defineConfig({
    server: {
        hmr: {
            host: 'localhost',
        },
    },
    plugins: [
        laravel({
            input: 'resources/js/app.js',
        }),
        vue({
            template: {
                transformAssetUrls: {
                    base: null,
                    includeAbsolute: false,
                },
            },
        }),
    ],
});
2 likes
Francismori7's avatar

@renatoxm it's not tagged yet. What I've done in the meantime is downloaded the source of the vite-plugin, ran npm install in it and then ran npm run dev. Copied the contents of /dist to your /node_modules/laravel-vite-plugin/dist in your project.

Also, it seems like you need to add a new npm package on your project: npm install vite-plugin-full-reload --save-dev

After all that, npm run dev in Sail will properly generate the right URL (the one you set in server.hmr.host) and put it inside /public/hot so everything works when you go to http://localhost.

Fffeeeeeeeewwwwww

2 likes
renatoxm's avatar

@Francismori7 PERFECT! Thank you.

Only one thing to change is npm run build in the second step. So recapping the current fix until it is tagged:

1 Clone the main branch at https://github.com/laravel/vite-plugin

2 run npm run build in it (not in your project, on a separate folder)

3 Copy the contents of /dist to your /node_modules/laravel-vite-plugin/dist in your project.

4 Run (in your project): npm install vite-plugin-full-reload --save-dev

5 Run npm run dev in Sail

6 Be happy!

Thanks again bro!

2 likes
jeffgrassine's avatar

I have the same problem. I created a new application and when compiling it doesn't load the front-end, it just shows a blank screen.

2 likes
RossAlexander's avatar

I had been dealing with this issue as well and, unfortunately, the suggested fix in this thread did not work for me. I finally found a quick and easy fix (at least for me on WSL2):

In the file hot located in the public folder, replace http://0.0.0.0:5173 with http://localhost:5173 and you should be good to go.

No more blank white screen and CORS errors!

9 likes
jdbacero's avatar

@RossAlexander This worked for me although I don't really have any shred of understanding why this works.

Many thanks for this 🥳

1 like
ckalita's avatar

if @rossalexander 's solution is working for you, consider adding this to the root of your vite.config.js:

server: {
        hmr: {
            host: 'localhost'
        }
    }

should do the magic trick and you don't have to update hot all the time

10 likes
z3nsh3ll's avatar

@RossAlexander Thanks for saving me. Only 3 hours lost, but it could have been a lot more!

I had the following. Default jetstream install with inertia. Php artisan serve --host running on a second machine on the local network. Vite server running on same second machine. Accessing site through browser on another machine on same local network. CORs errors no matter how much I changed the vite config although I didn't try that hmr object.

Couldn't work out what the [::] address was referring to in the browser requests and I see it's there in the public/hot file and figure straight away that it's the issue. I changed the [::] value to my second machine's internal IP on the local network (not localhost or 127.0.0.1 since I used npx vite --host to broadcast vite server over the network)

Anyway, thanks again

1 like
Dev_Marcos's avatar

@ckalita Man, thanks so so much!! I have been working hours and hours trying to fix this f***** error. FINALLY!!

BlueJay's avatar

@ckalita Thanks also, I did not have a hot file as my setup is similar to @z3nsh3ll - no Docker. I had added the server: { host:true } but my css was not being passed through tailiwind before being served. updating to the below with your solution worked;

server: {
        host: true,
        hmr: {
            host: '192.168.0.154'
        }
    }
1 like
jny986@gmail.com's avatar

I found the issue with myself was that you have to be editing the files in the docker container and running npm run dev on the same connection

I simply connected to the docker container with VS Code (plus docker ext), ran the vite within the VS Code terminal and then when I edit and save a file it automatically updated

enderandpeter@yahoo.com's avatar

So far, I"m having a hard time understanding why Laravel went to Vite. It's more complicated and bulky than necessary. The only advantage is that it uses native JS modules. Is that really worth all this? And I don't understand the "No bundling required" statement when you clearly see it bundling assets and files.

The CORS error is misleading. The browser is having trouble connecting to the Vite server. I am also using docker-compose. Yes, exposing that port is essential.

I have only gotten this to work by running the vite command from the host machine and not the container. Then the basic setup works but it can't make a Websocket connection. I'm not too clear on why that is needed. If you are going to use Websockets, use a proper Websocket server. Luckily the build command works, but this does not feel like a step up from Mix.

2 likes
lsblsb's avatar

I am having the same problem - but only related to the files, that should be injected via the @vite directive in my blade template. they cannot be loaded, instead i am getting an EMPTY RESPONSE ERROR. I tried several solutions, nothing worked yet. I described everything in this ticket:

https://github.com/laravel/vite-plugin/issues/127

I appreciate any help!

ruslanguild's avatar

I was experiencing all the issues described above and only this little piece of code resolved my issues:

server: {
        host: '0.0.0.0',
        hmr: {
            host: 'localhost'
        }
    }
warmwhisky's avatar

I am getting this on both Linux and WSL2. Frustratingly nothing solves it.

I am not really understanding why Laravel changed to Vite. It seems fragile. Too many solutions to one issue and none work for some and some work for others, but nothing solid.

I had this working all weekend on my home Windows 10 with wsl2. Come to work and on Ubuntu I get this issue. My colleagues Windows 10 wsl2 also has the same issue.

What is really getting me down more and more lately is i spend more time setting up the environment than actually producing applications. The countless issues I get with Docker drives me crazy.

Why don't I just go back to simple Xampp and in-lining CSS files to the document head?? I mean really its 2022, things are becoming more tricky to manage.

3 likes
braed's avatar

@warmwhisky you're not the only one who feels this way - all this new technology and i'm honestly failing to see the benefits. Things were working just fine 5 years ago and now I spend hours/days getting projects to just work...

3 likes
louk116's avatar

This is the working config for me in docker windows

import { defineConfig } from 'vite'; import laravel, { refreshPaths } from 'laravel-vite-plugin';

export default defineConfig({ server: { host: '0.0.0.0', hmr: { clientPort: 3000, host: 'localhost', },

},
plugins: [
    laravel({
        input: [
            'resources/css/app.css',
            'resources/js/app.js',
        ],
        refresh: true,
    }),
    {
        name: 'blade',
        handleHotUpdate({ file, server }) {
            if (file.endsWith('.blade.php')) {
                server.ws.send({
                    type: 'full-reload',
                    path: '*',
                });
            }
        },
    }
],

});

enderandpeter@yahoo.com's avatar

I am back a year later.

I am better understanding how the environment in which you visit the site in a browser needs to be able to connect to the environment that is running yarn dev on port 5173, or whatever VITE_PORT might be set to. Generally, I find that for local development with docker, it's best to work from a bind-mounted container so you can run yarn dev locally. When the project is deployed on a real server, you will include the step of running yarn build instead so that it's working with the static files.

lewis4u's avatar

[SOLVED] Finally found the solution for docker or sail + vite but I needed to have ssl... https and this link helped me: https://github.com/vitejs/vite/issues/9311

tldr;

up to vite v2 if no certificate was configured, a self-signed certificate was automatically created and cached.

after vite v3+ they recommend manually creating your certificates. If you still want to use the automatic generation from v2, this feature can be enabled back by adding @vitejs/plugin-basic-ssl to the project plugins.

this is my vite config which works with vite v5 + https

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
import fs from 'fs';

// Paths to SSL/TLS certificate and private key files
const sslKeyPath = '.path/to/your.key';
const sslCertPath = '.path/to/your.crt';

export default defineConfig({
    server: {
        host: '0.0.0.0',
        hmr: {
            host: 'your.local.domain', // from your ssl certificate and needs to be mapped in your /etc/hosts file
        },
        https: {
            key: fs.readFileSync(sslKeyPath, 'UTF-8'),
            cert: fs.readFileSync(sslCertPath, 'UTF-8'),
        },
    },
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js'
            ],
            refresh: true,
        }),
    ],
});
Speed Wasswa's avatar

Well, this comes in a bit late, but i hope it helps a soul: am also using inertia with vue in laravel. So i had to make sure that i only reference the js file in the app.blade.php, as is advised. and the css can be called inside the js file.

Then, i compiled the assets with npm run dev using vite, to see if my changes were visible,

npm run dev

once they were visible, i didnt close the npm run dev yet, i executed the npm run build to compile and package the assets inside public folder.

npm run build

didnt terminate any terminal interface, refreshed my browser to see if the changes took place, and they were there as expected. then i closed and terminated the npm run dev, this lelft the build active. and when i refreshed again, everything remained consistent. That is how it worked for me.

sanicode's avatar

I solve this problem with remove public/hot directory, because this path generate automatically when you npm run dev, vite will serve hot reload on that connection

kutikular's avatar

Guys i had the same problems. And i show you how to fix this: My system: Ubuntu 22.04 Infrastructure: Docker

docker-compose.yml:

services:

#Nginx Service
webserver:
    image: nginx:alpine
    restart: unless-stopped
    tty: true
    ports:
        - "80:80"
        - "5173:5173"
    volumes:
        - ./:/var/www/risks
        - .docker/nginx/nginx.conf:/etc/nginx/nginx.conf
        - .docker/nginx/sites/:/etc/nginx/conf.d/
    networks:
        - main

#PHP Service
risks:
    build:
        context: .
        dockerfile: Dockerfile
        args:
          UID: 1000
          GID: 1000
    restart: unless-stopped
    environment:
        SERVICE_NAME: risks
        SERVICE_TAGS: dev
    working_dir: /var/www/risks
    volumes:
        - ./:/var/www/risks
        - .docker/php/local.ini:/usr/local/etc/php/conf.d/local.ini
    networks:
        - main

networks: main: driver: bridge

docker ps:

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d70f775a64f9 nginx:alpine "/docker-entrypoint.…" 8 minutes ago Up 8 minutes 0.0.0.0:80->80/tcp, [::]:80->80/tcp, 0.0.0.0:5173->5173/tcp, [::]:5173->5173/tcp risks-webserver-1 a2277cc03e71 risks-risks "docker-php-entrypoi…" 42 minutes ago Up 42 minutes 9000/tcp risks-risks-1

take your ip address from your container (in my case it was risks_main), use docker network ls docker network inspect risks_main

check IPV4, and set to vite.config.js (it was 172.19.0.4)

vite.config.js:

import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import tailwindcss from '@tailwindcss/vite';

export default defineConfig({ server: { hmr: { host: '172.19.0.4', }, }, plugins: [ laravel({ input: ['resources/css/app.css', 'resources/js/app.js'], refresh: true, }), tailwindcss(), ], });

docker compose up -d --build docker compose exec container_name bash npm run dev

then it works, i'm sure.

Please or to participate in this conversation.