Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

OmEr1112's avatar

Laravel app downloading all assets again on every route request

I have a strange error in my Laravel(5.6)/app. Whenever there is any route request in the app, all css, js, bootstrap and icons files reload all over again in the browser. I'm debugging this app from last 2 days. And I have no clue what's going on.

I worked on this project a few days ago and it was working fine then I started working on some other project and after a couple of days when I worked again on this project to add some new features, it is behaving like this. Whenever I request any route in the app, all the assets download and load again in the browser. I didn't provide any code sample here because I don't know where the error might be.

If there have been an error in some particular file then that particular route would have behave that way. But all routes reload all the assets all over again. So I just want your opinion is this ever happens to you and how should I tackle it?

If you wanna recreate this project then here is the GitHub link. https://github.com/OmEr1112/Laravel-Shopping-Cart . Recreating this project is very easy. Just create a database, run migrations and then seed the database. For sass and js, run 'npm run dev' if needed.

0 likes
35 replies
RoboRobok's avatar

Isn't it just because you have Disable cache enabled in Dev Tools?

OmEr1112's avatar

'Disable cache' is not enabled in devtools.

RoboRobok's avatar

How do you know that the assets are loaded all over again? If you are sure about it, can you share the request and response headers for your assets?

OmEr1112's avatar

I was also looking into this response header and found out cache_control is being set to 'no-cache, private'. And I'm not doing anything like that in my app.

This is response header

array:5 [ "cache-control" => array:1 [ 0 => "no-cache, private" ] "date" => array:1 [ 0 => "Mon, 21 May 2018 09:24:16 GMT" ] "content-type" => array:1 [ 0 => "text/html; charset=UTF-8" ] "set-cookie" => array:2 [ 0 => "XSRF-TOKEN=eyJpdiI6IlhVaGhxUU02N056TVZzcGdxaGhPdlE9PSIsInZhbHVlIjoiaU1ONmFPV1c4dnBmakY2ZG1FcHVSV044V3grMXkxWGFEMmRjQVwvd3VsUzJ6SlVhb0FLM3h0OWJcL2FxeGhsMmFGaHhSMGR4UWhHeG1NMFwvRzR0aUh3RkE9PSIsIm1hYyI6ImNmMGI2YTY5ZDk3ZWM2MDczN2VmZjFhOTEyODI1NTY3ODc4NGE4ZTQ3ZTAyNTUwYjMzMDBhNGI1ZDc2ODcwOGUifQ%3D%3D; expires=Mon, 21-May-2018 11:24:16 GMT; Max-Age=7200; path=/" 1 => "laravel_shopping_cart_session=eyJpdiI6InhJQmE1Y2dqY1p1Q3l2S0pFcmwyTGc9PSIsInZhbHVlIjoidG54UWZUNmVWSWgya2hZRHBWeStic2h3XC9raFJ6UEN0NlY4VXBSdWZSNDBZbnJ4RG0zVlBFNko2dHE2Ym9lVGdGWVRCcG9vXC9DUUlZTUFXUzhSb09pQT09IiwibWFjIjoiNGQ5M2FhNmEyYjkzOGMyMjU5ZWJiM2RiM2IyNWU2MTRkMTdiZDYwNWU1MDIzOGQyODQzMDQyNmVlM2QzYWRhZCJ9; expires=Mon, 21-May-2018 11:24:16 GMT; Max-Age=7200; path=/; httponly" ] "Set-Cookie" => array:2 [ 0 => "XSRF-TOKEN=eyJpdiI6IlhVaGhxUU02N056TVZzcGdxaGhPdlE9PSIsInZhbHVlIjoiaU1ONmFPV1c4dnBmakY2ZG1FcHVSV044V3grMXkxWGFEMmRjQVwvd3VsUzJ6SlVhb0FLM3h0OWJcL2FxeGhsMmFGaHhSMGR4UWhHeG1NMFwvRzR0aUh3RkE9PSIsIm1hYyI6ImNmMGI2YTY5ZDk3ZWM2MDczN2VmZjFhOTEyODI1NTY3ODc4NGE4ZTQ3ZTAyNTUwYjMzMDBhNGI1ZDc2ODcwOGUifQ%3D%3D; expires=Mon, 21-May-2018 11:24:16 GMT; path=/" 1 => "laravel_shopping_cart_session=eyJpdiI6InhJQmE1Y2dqY1p1Q3l2S0pFcmwyTGc9PSIsInZhbHVlIjoidG54UWZUNmVWSWgya2hZRHBWeStic2h3XC9raFJ6UEN0NlY4VXBSdWZSNDBZbnJ4RG0zVlBFNko2dHE2Ym9lVGdGWVRCcG9vXC9DUUlZTUFXUzhSb09pQT09IiwibWFjIjoiNGQ5M2FhNmEyYjkzOGMyMjU5ZWJiM2RiM2IyNWU2MTRkMTdiZDYwNWU1MDIzOGQyODQzMDQyNmVlM2QzYWRhZCJ9; expires=Mon, 21-May-2018 11:24:16 GMT; path=/; httponly" ] ]

This is request header.

array:9 [ "host" => array:1 [ 0 => "127.0.0.1:8000" ] "connection" => array:1 [ 0 => "keep-alive" ] "upgrade-insecure-requests" => array:1 [ 0 => "1" ] "user-agent" => array:1 [ 0 => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36" ] "accept" => array:1 [ 0 => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,/;q=0.8" ] "referer" => array:1 [ 0 => "http://127.0.0.1:8000/shoppingCart" ] "accept-encoding" => array:1 [ 0 => "gzip, deflate, br" ] "accept-language" => array:1 [ 0 => "en-US,en;q=0.9" ] "cookie" => array:1 [ 0 => "XDEBUG_SESSION=VSCODE; XSRF-TOKEN=eyJpdiI6IkJHZWMwSWQxWldCUmFXNW0wc21Zc3c9PSIsInZhbHVlIjoiamVyODZXQVAzSVQwdEdtbXoyUmxRNW5tUCtzZTRaOXdhcGZuT1lMbmRjVjJQM29Rck1oSDVXN0Vhck5QVzNUaTVZTm1mampPY0Z1QWpqZjU4TGtLckE9PSIsIm1hYyI6IjIyMTcwMGEwM2M5NmUxOTg4ZjFjYTI4ZmNlNWZlYTZmOTZkOTEwYjk2ZGQxZDNiMDMzYzc1MmM4MWQ5NzRmNjYifQ%3D%3D; laravel_shopping_cart_session=eyJpdiI6IkpQUUd5Rld2UjhtUFBFc0FudjhteGc9PSIsInZhbHVlIjoicnBOSEJHUXNcL0NXSXBsWUt3ZHV0SEViSjhVa2RNR3EzUmsxNnBhME5hV0E3OEZONnpzXC9jTmhvZndMUHgwYU5la3RidVpPeFA2dDFWS1A1SmtndlQrdz09IiwibWFjIjoiZGM3MjY5M2UwMzBjMzhhZmYwYzFmZDI4Y2NkNDVjNDdlMWNmOGYxYWZkYTQ2ODQ1OTliMTQwZWQxYmI1ZTZmYiJ9" ] ]

OmEr1112's avatar

I am sure the assets are reloading bcoz i confirmed it through network tab and 'php artisan serve' also serving every asset again on every route request.

arukomp's avatar

you're showing the headers for your html page, which shouldn't be cached anyway. What about js/css response headers?

arukomp's avatar

if you can't find them, then how do you know they're not being cached?

In Chrome/Firefox developer tools, Network tab. Reload the page and you'll be able to see all the requests.

By default, html/text responses are not cached, while js/css/images are cached.

OmEr1112's avatar

I didn't see these files' header there. but I can see if they are loading from cache or downloading everytime.

response header for app.css

Connection: close Content-Length: 1505 Content-Type: text/css; charset=UTF-8 Date: Mon, 21 May 2018 16:14:16 +0200 Host: 127.0.0.1:8000

request header for app.css

Accept: text/css,/;q=0.1 Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Connection: keep-alive Cookie: XDEBUG_SESSION=VSCODE; XSRF-TOKEN=eyJpdiI6Im1rd2VtNzM5TTJvTU9kNjJuTUJIbGc9PSIsInZhbHVlIjoiMlRMOWFlajhpZzJldjhJbmFLc0tROUZlaFArZmcxRmJpNlQ4RW9kckoxMlJxVlp6S1FpTGcraFRsVmVkTmdRMklRMFNNU1ZCeEdFWURuK2w4K3lNYkE9PSIsIm1hYyI6ImVmMjk2MTU0MWE1MmI5MTFjMGRlNDE1MDAzYjk2NTdjNjk0NWI2ZGZiNDQzYTc0ZjEyNTRjNTljYmUzYTZiNzAifQ%3D%3D; laravel_shopping_cart_session=eyJpdiI6Imh4cytrQURcL1BuNEpYUjhDV2tTMXJBPT0iLCJ2YWx1ZSI6IjBBSjJNZXY5K1VlakxFRE9TdUJNeVwvZXhrbXZuQUVFd1daT25kamp0TzRRdG5hemJlUHFHNkVYWVhhWFAzd1IzTHNrZ0ttRTZwNit2dituVDI1VHE5UT09IiwibWFjIjoiZGU5OGQ1ODQzOTdkMGUzMDMxMzJjY2Q5YjlhYTdjMWU3Zjg1MWFhODJiOGQyMDhhY2Y3ODA2YjA4ODYwODBjYyJ9 Host: 127.0.0.1:8000 Referer: http://127.0.0.1:8000/ User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36

for js file reponse header

Connection: close Content-Length: 97180 Content-Type: application/javascript Date: Mon, 21 May 2018 16:14:16 +0200 Host: 127.0.0.1:8000

request header

Accept: / Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Connection: keep-alive Cookie: XDEBUG_SESSION=VSCODE; XSRF-TOKEN=eyJpdiI6Im1rd2VtNzM5TTJvTU9kNjJuTUJIbGc9PSIsInZhbHVlIjoiMlRMOWFlajhpZzJldjhJbmFLc0tROUZlaFArZmcxRmJpNlQ4RW9kckoxMlJxVlp6S1FpTGcraFRsVmVkTmdRMklRMFNNU1ZCeEdFWURuK2w4K3lNYkE9PSIsIm1hYyI6ImVmMjk2MTU0MWE1MmI5MTFjMGRlNDE1MDAzYjk2NTdjNjk0NWI2ZGZiNDQzYTc0ZjEyNTRjNTljYmUzYTZiNzAifQ%3D%3D; laravel_shopping_cart_session=eyJpdiI6Imh4cytrQURcL1BuNEpYUjhDV2tTMXJBPT0iLCJ2YWx1ZSI6IjBBSjJNZXY5K1VlakxFRE9TdUJNeVwvZXhrbXZuQUVFd1daT25kamp0TzRRdG5hemJlUHFHNkVYWVhhWFAzd1IzTHNrZ0ttRTZwNit2dituVDI1VHE5UT09IiwibWFjIjoiZGU5OGQ1ODQzOTdkMGUzMDMxMzJjY2Q5YjlhYTdjMWU3Zjg1MWFhODJiOGQyMDhhY2Y3ODA2YjA4ODYwODBjYyJ9 Host: 127.0.0.1:8000 Referer: http://127.0.0.1:8000/ User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36

arukomp's avatar

see, there's no cache headers. it's being cached. You can also see from the Network tab whether it's been loaded from the Cache.

I'm kind of confused at the moment. What's the issue here anyway?

OmEr1112's avatar

Whenever there is a new route request. Every asset redownload and doesnt load from cache. In network tab i can see that , size of the assets and then below it the same number. and it's not written it is loading from cache. it feels like app is loading as the first time on every route request. This is the problem

OmEr1112's avatar

someone plzz look into this problem..

Snapey's avatar

Its an issue in the client not Laravel, unless Laravel is explicitly suggesting not to cache

OmEr1112's avatar

yes, you are right. it is not laravel issue but it's happening in every browser and every project. plzz help me figure out where i messed up. i dont know what to do

click's avatar

You should set caching headers in your server config for static files like css, js, jpg, gif, etc.

Or see this pastebin (not mine) for an example nginx config. https://pastebin.com/ePjG0xq8

On the top you see a block with configs per file type and the full image directory. 7d means 7 days. You could also set this to `max'.

map $sent_http_content_type $expires {
 
    default                    off;
    text/html                  epoch;
    text/css                   7d;
    application/javascript     7d;
    application/font-woff      7d;
    application/font-ttf       7d;
    application/font-otf       7d;
    ~image/                    7d;
 
}

And within the server block you use this map:

server {
    // .... other config
 
   expires $expires;
}
arukomp's avatar

It would be easiest if you could just open up the Network tab on Google Chrome, load a page, and give us the screenshot of what you see.

OmEr1112's avatar

I first loaded the site, then navigated to another page, and then went back to home page. Then took this screechot of network tab..

Cronix's avatar

Doesn't appear that the image is publicly accessible.

Snapey's avatar

Something else is wrong looking at your timeline... 8.5 seconds to load the page?

OmEr1112's avatar

I'm not optimizing it any way. i was in initial satges and then this problem happened and i stopped working on it. That's why loading time is high. bcoz it is not the priority right now

Snapey's avatar

I never optimise anything and usually have sub-second page load times (especially if working locally)

OmEr1112's avatar

well there may be some inefficient code there and also you can see that Im transfferring 465kb data before the page loads. which can be reduced. But Im not worried about this right now. and the bigger load times are also bcoz of the problem which I stated at the start of this thread.

Someone plz tell me when navigating through a site which is being developed , assets like bootstrap file, or jquery and other assets like that, download every time while navigating?

click's avatar

@OmEr1112 read my answer at the top of this page again. You simply need to set your caching headers on your local server.

OmEr1112's avatar

@m-rk Im using laravel built-in local server which is apache and using mariadb through xampp. I tried to set cache headers through public/.htaccess file. But it doesn't work . May be you could tell me what should i try there ? exact code.

jlrdw's avatar

Im using laravel built-in server which is apache

Huh?

OmEr1112's avatar

it isn't? im new to back end .so im learning all this

Next

Please or to participate in this conversation.