How to clear cache in Laravel?
When you modify a compiled asset like app.css in Laravel, the framework does not immediately reflect those changes on your production site. This behavior is intentional and stems from how the Vite build tool and Laravel's caching mechanism interact. If you are seeing an old hash value appended to your URLâsuch as css/app.css?id=43965480df345823e8e0âit means the browser has cached a previous version of that file, or Laravel is serving a stale copy from its public directory.
The core issue lies in the query parameter appended to the asset path. In your <link> tag, you are referencing {{ url(mix('css/app.css')) }}. The mix() helper function checks the .build-manifest.json file to retrieve the hashed filename (e.g., app.a1b2c3d4.css) and appends it as a query string. This hash ensures browser-level caching works correctly; if the file content changes, the build tool generates a new hash, effectively invalidating the old resource in the browser's eyes.
However, simply changing the source code in resources/css/app.css is insufficient because Laravel has likely already compiled that file into your public/build directory. The web server serves the compiled file from there, not directly from the source folder. To force the changes to appear, you must invalidate the cache at two distinct levels: the framework's internal cache and the browser's local cache.
Clearing the Framework Cache
The first step is ensuring Laravel forgets its previous build state. You can do this by running the php artisan command suite. Specifically, you need to clear the view cache and the optimization cache. While cache:clear handles the application cache (like config or route caching), it does not automatically delete the contents of your public/build directory unless you have explicitly configured Vite to do so via environment variables.
Run the following commands from your project root:
php artisan view:clear
php artisan optimize-clear
If you are using Laravel 9 or higher, you might also want to ensure that the public directory is being served correctly by checking your .env file. Ensure that APP_ENV is set to production if you are relying on Vite's production optimization features. If you have previously run npm run build, this command generates a .build-manifest.json file which maps source files to their hashed output names. Clearing the cache ensures Laravel regenerates this manifest based on your new CSS content.
Handling Browser Caching
Even after clearing the server-side cache, the browser will continue to serve the old app.css?id=43965480df345823e8e0 because of its own HTTP caching policies. The query string acts as a unique identifier for the resource; as long as the ID remains different from what you expect, the browser assumes it is a new file and might skip re-validation depending on how your Content-Type headers are set or if the hash logic hasn't updated correctly in the manifest.
To force the browser to fetch the latest version, you can manually clear the cache for that specific URL. Open your developer tools (usually F12), navigate to the Network tab, right-click the CSS request, and select "Clear browser cache and hard reload." Alternatively, append a temporary query parameter directly in your address bar, such as ?nocache=1, to bypass the stored response immediately.
Best Practices for Development vs. Production
It is important to distinguish between how you handle assets in development versus production. In a local development environment (APP_ENV=local), Vite typically runs in watch mode. When you save changes to app.css, the browser should automatically reload and fetch the new hash without needing to manually clear caches, provided your build process hasn't been interrupted.
In production, however, the workflow changes. You must commit the generated .build-manifest.json and the contents of public/build to version control or ensure they are rebuilt during your deployment pipeline. If you deploy code but forget to run the production build command (npm run build) or clear the cache, you will encounter exactly the issue described in your scenario.
For a smoother workflow, consider configuring Vite to not generate hashes in development if strict live-reloading is required, though this sacrifices the primary benefit of long-term browser caching. The standard approach remains: modify source, rebuild assets, clear framework cache, and ensure the browser accepts the new hash. If you are managing multiple environments, always verify that your deployment script includes steps to regenerate assets before restarting the web server.