It seems like your Laravel application is still trying to load assets as if it were in a local development environment, which is why it's attempting to access the assets via http://[::1]:5173/. This is the default behavior when using Vite in development mode, as it serves assets over a local server running on port 5173.
In production, however, you want to serve the compiled assets from your public directory. Here's what you can do to resolve the issue:
-
Ensure that you have run
npm run buildon your production server. This command should generate the production-ready assets in thepublic/builddirectory. -
Verify that the
public/builddirectory exists and contains the compiled assets. -
Check your
vite.config.jsfile to ensure that it is set up correctly for production builds. It should have abuildsection that specifies the output directory, which should be within yourpublicdirectory.
Here's an example of what your vite.config.js might look like:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
build: {
// The outDir should be set to your public directory
outDir: 'public/build',
},
});
- In your Blade layout file, you should use the
@vitedirective as you are already doing. This directive should automatically switch to using the production assets whenAPP_ENVis set toproduction.
@vite(['resources/css/app.css', 'resources/js/app.js'])
-
Make sure that your
.envfile on the production server has theAPP_ENVvariable set toproduction. This tells Laravel to use the optimized assets. -
Clear your Laravel caches to ensure that the configuration is reloaded. You can do this by running the following commands:
php artisan config:clear
php artisan cache:clear
- If you're using a CDN or have any caching mechanisms in place, make sure to invalidate the cache so that the new assets are served.
If you've followed these steps and are still seeing issues, double-check your server configuration to ensure that it's not serving assets from a previous build or from a development server. Also, ensure that your .env file is correctly set up and that there are no caching issues at play.
If the problem persists, you may want to check the network requests in your browser's developer tools to see exactly where the requests are being made and what responses are being received. This can help you pinpoint where the configuration might be going wrong.