Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ariel Mejia
Ariel Mejia

Posted on • Edited on

     

How to optimize your blade views in Laravel

The Context

I want to update my personal site in 2019 & 2020 I was using NuxtJS, this year I got the idea to change it to a Laravel application with blade views, tailwind & just a sprinkle of javascript using alpineJS.

The challenge

I want a beautiful landing page with a great performance, hopefully all lighthouse stats on green

The result

Heremy website as the result of all the tips behind.

The tips

I would going to share how I get all lighthouse on green an a few little more tricks.

  • Open lighthouse on "incognito" mode, this is because chrome extensions could impact in the review.
  • Purge your css using tailwindcss.
  • Many browsers actually support theloading="lazy" attribute so use it on your images that are not loaded on the visual ratio when your site loads.
  • Minify your Javascript in my case I only have one file, but if you have more files laravel mix can attach them and minify them on production.
  • load Javascript only when is required, I only use alpineJS on contact form page so adding js scripts conditionally could help to load faster your site, here an example:
@if(Route::is('contact'))<scriptsrc="public/js/app.js"></script>@endif
Enter fullscreen modeExit fullscreen mode
  • protect your links with target blank usingrel="noopener noreferrer" attribute, it prevents an attack called tabnapping.
  • optimize the images by reducing the size and weight of images, you can use something like:https://tinypng.com
  • take care about accesibility, there are some text color that simply does not contrast enough take that in mind, lighthouse offers some tips on that cases just follow them.
  • use SSL certificate on your site, lighthouse take that in mind and your users a site without SSL cert looks like an unsafe site.
  • use SVGs when you can, they are optimized for the web, or even better when you can use web.p format.
  • Take care about responsiveness of your site, it would not be a stat but definitely more people would visit your site first on mobile than desktop, so take that in mind.
  • Add an alternative text to every image, even if its just an asset:
<imgsrc="me.png"alt="picture of me"/>// on svgs you can use the title tag<svgfill="currentColor"stroke-linecap="round"stroke-linejoin="round"stroke-width="2"class="w-5 h-5"viewBox="0 0 24 24"aria-labelledby="twitter_logo"><titleid="twitter_logo">Twitter logo</title><pathd="M23 3a10.9 10.9 0 01-3.14 1.53 4.48 4.48 0 00-7.86 3v1A10.66 10.66 0 013 4s-4 9 5 13a11.64 11.64 0 01-7 2c9 5 20 0 20-11.5a4.5 4.5 0 00-.08-.83A7.72 7.72 0 0023 3z"></path></svg>
Enter fullscreen modeExit fullscreen mode
  • Optimize your Laravel application when you are going to make a deploy:

Locally:

npm run prod
Enter fullscreen modeExit fullscreen mode

On Server:

clear and cache again the config files, routes & views.

// clear cachephpartisanconfig:clearphpartisanroute:clearphpartisanview:clear// cache filesphpartisanconfig:cachephpartisanroute:cachephpartisanview:cachephpartisanoptimize
Enter fullscreen modeExit fullscreen mode
  • Add cache on your server, if you are going to use NGINX you can set easily some configuration to cache assets like images or pdf files from one request to other by an amount of time that you can customize, here the configuration that I use on my site to compress config & cache assets:
# gzip compression settingsgzip on;gzip_comp_level 5;gzip_min_length 256;gzip_proxied any;gzip_vary on;# browser cache static assets controllocation ~* \.(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {     expires 1d;     access_log off;     add_header Pragma public;     add_header Cache-Control "public, max-age=86400";}# browser caching of pdfslocation ~*  \.(pdf)$ {    expires 365d;}
Enter fullscreen modeExit fullscreen mode

You can handle all files in just onelocation block, but in my case as the PDF file in my site would not change as much, I separate it into anotherlocation block.

Hope this tips would be useful on your Laravel blade public views, as always thanks for reading, happy coding & let me know in the comments if you found it useful.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Fullstack/Mobile developer, Laravel and Flutter enthusiast.
  • Location
    Guatemala
  • Work
    Senior Developer
  • Joined

More fromAriel Mejia

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp