The reason may be at server level, it may be at the code optimization level... The reasons are many. I advise you to start by installing debugbar in your application. It will give you detailed information about everything that the page is loading and you can optimize whatever you want.
laravel website slow loading time
Hello , i have an e-commerce website built with laravel and bootstrap i am suffering from a very very very slow loading time but i can't figure out where is the problem here's the link : http://petit.orcloud.dz/ any help will be very much appreciated
THANK YOU
You have debug bar enabled on production. Be careful with that. But you have 217 queries on the front page alone
My guess is that you are missing a ->with('icon') or >with('promotions')
https://laravel.com/docs/9.x/eloquent-relationships#eager-loading
If you are running a newer version of laravel you can make it throw an error if you forget it on dev: https://laravel.com/docs/9.x/eloquent-relationships#preventing-lazy-loading
@Sinnbeck thank you for your answer i reduced the number of queries on the front page but the loading time didn't really improve
@aureliee123 better but still alot of similar queries. I'm sure you can inprove it even more. 40 queries
Your server response time is terrible the Time to First Byte (TTFB) is around 4 seconds. Your initial payload is 4.9MB How/where is it hosted?
Your images are another bottleneck - around 5 seconds (probably server related)
https://petit.orcloud.dz/storage/comments/sXj8Axg0xbI3M9rH1y5zGhlWqQr5GHs8VFl2GBui.jpg is > 400KB you need to optimise and resize your images.
You also have render-blocking resources. Defer loading your non-essential assets.
Minify your assets (e.g. CSS, JS).
@pom thank you for your answer the ttfb is awful i'm hosting the website in amazon i have another website in the same server https://v1.tabland.shop/ and it's really fast
i minified the js and css files but still same issue
@aureliee123 last I checked you still had alot of queries on the front page. Show your controller for the index method
@aureliee123 I hate to be the bearer of bad news but that site is just as slow. While the time to first interaction is around 2 seconds (acceptable) you have 23 seconds worth of images being downloaded.
https://v1.tabland.shop/storage/produits/gJvGKBqkY0AeEhAxt3m5DvwtrbqMrRhrJ1cJHgTh.jpeg is almost 10MB with a resolution of 3780x5314 😲 total payload is 33.5MB
Are you preloading your images with some sort of JS on the new site?
OMG I just clicked on /produits - 58.8MB I can guarantee you're losing customers.
@pom thank you for your answer
the problem isn't in the second website (https://v1.tabland.shop/) but in https://petit.orcloud.dz/
@aureliee123 the time to first byte is better on https://v1.tabland.shop/ but after that it's really slow. Images takes a loong time to load
@Sinnbeck YEESS i agree
but i can't figure out why the ttfb is better on https://v1.tabland.shop/
i'm showing 6 categories on the front page and 5 products for each category so that's a total of 30 products ( 30 images ) can this possibly be the reason of the slow ttfb ?
public function index() {
$produits = Produit::where('afficher', 1)->with('promotion','categorie')
->orderBy('prio')->get();
$comments = Comment::where('etat',1)->with('client')->get();
$categories = Categorie::where('afficher',1)->with('img')
->get()->each(function ($feed) {
$feed->load('produits.categorie')->take(5);
});
return view('index', compact('produits', 'categories','comments'));
}
@aureliee123 this seems to make a bunch of queries
$categories = Categorie::where('afficher',1)->with('img')
->get()->each(function ($feed) {
$feed->load('produits.categorie')->take(5);
});
Use with directly instead
@Sinnbeck i used this to show only 5 products of each categorie in the index page
@aureliee123 something like this if I understood you correctly
$categories = Categorie::where('afficher',1)->with('img')->with(['produits' => function ($q) {
$q->with('categorie')->limit(5);
})->get();
@Sinnbeck YESS i'll try your code
@sinnbeck $categories = Categorie::where('afficher',1)->with('img')->with(['produits' => function ($q) { $q->with('categorie')->limit(5); }])->get();
this code didn't work it's not returning 5 products for each category
@aureliee123 weird. I could have sworn it would. I am not a computer currently so I am not able to test it out
@Sinnbeck i think the problem lies in the ttfb its about 3s to 5s which is a lot
do you have a route that returns a simple view ( or create one ). One with no database queries. You can call this and measure ttfb. it should be less than 200ms
without any query on the home page the ttfb is 406ms
@aureliee123 that sounds slow. Can you test the exact same on the good site to compare?
@Sinnbeck 200 ms
@aureliee123 something is off. How about on dev for the new site?
Its not a 'server' issue as all the other menu items respond between 40 and 90ms (debugbar timeline)
definitely something you are doing in the home page route
@Snapey something i'm doing in the home page route as in too many queries ? or too much rendering ?
@aureliee123 something you are doing. other pages have more database requests and serve the page quicker
do you have any view composers?
any of the view components have code in them and in particular, loops?
are you using cloud storage for the images?
@Snapey i'm not using any view composers but i do have loops
@foreach ($categories as $key=>$value) <div class="tab-pane fade {{$key==0?"active show":""}} position-relative" id="pills-{{$value->id}}" role="tabpanel" aria-labelledby="pills-monstre-{{$value->id}}">
<div class="swiper-container mySwiper2 h-max">
<div class="swiper-wrapper clr-alternate h-max">
@foreach ($value->produits->take(5) as $item)
<div data-backdrop="false" style="z-index:7000000 !important" class="modal fade" id="exampleModalCenter{{$item->id}}Promo" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog"
do you think the problem is coming from the loops ? i'm using loops on the good website ( https://v1.tabland.shop/ ) too and it's not causing issues
@aureliee123 I visited your site and see you installed debug bar as suggested.
Which is great. But please don't enable it on your production server. Read the README at debugbar repository to learn how to disable it on production.
From the info in the debugbar it doesn't see you are doing nothing fancy. Network tab also seems ok: bundled and minimized JS, not many requests to the server (not a problem as server uses HTTP/2), and just two third party requests from a CDN.
So you might have a very small server, or if you are running database and web server on the same "machine", you could consider giving it more RAM. Ideally database and webserver should run in different machines, but I understand it is not possible for every project.
As debugbar is enabled in production, I assume you are not optimizing your composer file in production, neither caching config or routes. These are small wins which can shave off about 10-15% on server processing time for each request.
Please read very carefully this section on Laravel's official documentation, to learn more about these suggestions:
https://laravel.com/docs/9.x/deployment
Other than those suggestions I don't see how else you could improve it with the information provided.
Good luck
I highly suggest you take a look at this
https://gtmetrix.com/reports/petit.orcloud.dz/nueR7F7D/
It's a metrix of your site, it will give you some tips och how to improve. performance.
@aureliee123 no it should be fine, but you don't explain what you are doing in the home route
@Snapey this is the index page function
public function index() {
$products = Product::where('show', 1)->with('promotion','category')
->orderBy('prio')->get();
$comments = Comment::where('state',1)->with('client')->get();
$categories =
Category::where('show',1)->with('img','products.categorie','products.promotion')
->get();
return view('index', compact('products', 'categories','comments'));
}
and in the view i am showing $products and i'm iterating over the categories and showing 5 products of each category that's about it
Please or to participate in this conversation.