AdamVaclav's avatar

Laravel 11 Asset bundling production issues

Hey all, I'm having issues with my production(shared hosting) asset bundling with vite.

After uploading files to prod, running npm install and npm run build, this is the URL laravel is trying to load assets from. http://[::1]:5173/resources/css/app.css http://[::1]:5173/resources/js/app.js http://[::1]:5173/@vite/client

In my layout file, I'm importing asset as instructed from documentation:

@vite(['resources/css/app.css', 'resources/js/app.js'])

I guess the first problem is, on production, isn't it supposed to grab files from /public/build/assets/... ? The second problem is the url itself, I have my env set up correctly(I think), I'm not sure what is going on with htttps//[::1}5173/ in the url of the assets?

APP_NAME="xxxxx xxxxxx" APP_ENV=production APP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx APP_DEBUG=false APP_TIMEZONE=UTC APP_URL="https://xxxxxxxxxxxxxx.com"

Any ideas what to do?

0 likes
5 replies
LaryAI's avatar
Level 58

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:

  1. Ensure that you have run npm run build on your production server. This command should generate the production-ready assets in the public/build directory.

  2. Verify that the public/build directory exists and contains the compiled assets.

  3. Check your vite.config.js file to ensure that it is set up correctly for production builds. It should have a build section that specifies the output directory, which should be within your public directory.

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',
    },
});
  1. In your Blade layout file, you should use the @vite directive as you are already doing. This directive should automatically switch to using the production assets when APP_ENV is set to production.
@vite(['resources/css/app.css', 'resources/js/app.js'])
  1. Make sure that your .env file on the production server has the APP_ENV variable set to production. This tells Laravel to use the optimized assets.

  2. 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
  1. 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.

nexxai's avatar

You should be running npm run build prior to deployment which will build static versions of your assets in the /public folder and so those are should be what actually get deployed to your production site.

AdamVaclav's avatar

@nexxai what's the difference between running npm run build in local prior to deployment or running it on the server? Doesn't make sense to me there should be any

gych's avatar
gych
Best Answer
Level 29

Its fine to build your application on production server.

Can you check if your public folder contains the hot file? If so, delete it.

3 likes
damienb's avatar

About vite issue on laravel 11 I solved my problem quickly when I realized that the gitignore was preventing me from pushing the manifest.json file and the build folder located in the public folder. So I uncommented the gitignore and pushed that and then restored the initial gitignore state. It works perfectly. Alès France

Please or to participate in this conversation.