Skip to content
Advertisement

How can I get around caching policy when using Laravel to generate CSS and Javascript?

I am using Laravel to generate files for what would otherwise be inline JS and CSS.

The web.php configuration is:

Route::get('/js/words.js', function() {
    $words = Word::get();

    return response()->view('words', compact('words'))->header('Content-Type', 'text/javascript');
});

When I deploy this to the server initially everything is fine. But then, when I add a simple CSS/JS caching policy to the Nginx configuration, the generated CSS/JS breaks.

The Nginx configuration is:

location ~* .(css|js)$ {
    expires 30d;
}

Does anybody else do this to avoid inline CSS/JS, and if so, how do you enable caching without breaking the generated files?

Advertisement

Answer

I’ve used it like this

location ~* .(?:css|js)$ {
   expires 365d;
   access_log off;
   add_header Cache-Control "public";
}

and make sure to restart the nginx server

sudo systemctl restart nginx

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement