Simple Laravel SEO Tips for Beginners

SEO

The Most Important Factors to Consider with Laravel SEO

The most important factor to consider with your technical SEO is the information you supply to search engines in the head of your pages, this includes the title tag, meta data such as description and OpenGraph data. When using the layouts feature of Blade, you may notice that updating the head tags can quickly become as mess.

Another important factor to consider is the performance of your application, when working with the Laravel Framework one of it's best and also worst features is that we are free to create the design pattern of our application as we see fit. Unoptimised database queries, bloated front-end assets and long-running tasks that should be a queued action are some of the most common reasons for slow Laravel applications. Installing packages like the Laravel Debugbar can assist you in identifying these issues.

The quality of your HTML which wraps your page or posts content is also a very important factor, using semantic tags to clearly identify portions of your content is a must. Make use of HTML5 semantic tags such as section, article, nav, aside, header and footer instead of repeating div over and over again.

Coming From a WordPress Background

If you're coming to Laravel from a WordPress background you have probably not had to really look into the finer detail of the head structure that you require as plugins such as YoastSEO handle all of that for you and the user is simply required to fill in information in the post editor or on the various settings menus.

As Laravel doesn't even have an admin panel by default, it is safe to say there is no YoastSEO style package for Laravel in terms of providing you with inputs on a page somewhere. Instead we must do this ourselves in the code or add our own inputs.

I personally recommend the SEO Tools package by Artesaos which provides use with a config file for the default values for the head data of our application and one liners we can place in the head of our layout files.

SEO Generator Service Class Example

Having a solid design pattern in place for generating each page or posts unique SEO head tags is absolutely crucial to getting the results you are striving for with your SEO strategy. By making use of the SEO Tools package mentioned previously, we can easily create a service class which will handle the SEO generation by using values from the database, let's take a look.

<?php

namespace App\Services;

use App\Models\Post;
use Artesaos\SEOTools\Facades\OpenGraph;
use Artesaos\SEOTools\Facades\SEOTools;

class PostSEOService
{
    public function generate(Post $post): void
    {
        SEOTools::setTitle($post->title);
        SEOTools::setDescription($post->meta_description);
        OpenGraph::addImage($post->twitter_image_url);
    }
}
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Services\PostSEOService;

class PostController extends Controller
{
    public function show(Post $post, PostSEOService $seo)
    {       
        $seo->generate($post);

        return view('posts.show', compact('post'));
    }
}

Validate Your Application

Using tools like Ahrefs to perform a site audit should be part of your overall testing strategy to ensure that your application is optimized for search engines. Site audit services like Ahrefs can raise issues such as meta description length, broken links, orphaned content and much more. If you're a small business or just starting out, the free service offered by Ahrefs is the best I have found personally, you can purchase a monthly subscription, however this will set you back at least $100 a month, if not more, which in my opinion just isn't worth the price tag unless you have a large website.

SEO validation with ahrefs

Using the data you can generate from services such as GTMetrix, Google's Lighthouse technology and SEO auditing sites you can ensure you're application is ready for crawling.

Use a Sitemap

Sitemaps are often overlooked when considering SEO and in truth, lots of small websites don't require one. Sitemaps allow us as developers to make a list of pages and tell search engines that these are the pages we'd like crawled. A properly linked website will allow search engines to find all of the pages and posts our application has so a sitemap wouldn't actually improve our SEO.

However, including a sitemap doesn't negatively impact your SEO so why not? There a plenty of reasons to include the functionality to produce a sitemap early on as once you grow, the need for a sitemap may occur and then you would be scrambling to implement the functionality.

For Laravel, I recommend Spatie's Laravel Sitemap package which will handle the generation of the sitemap itself, then we just need to decide when to generate the sitemap. For smaller website, I recommend you include the sitemap generation function whenever a post or page is updated to ensure our sitemap is always up to date - as there shouldn't be an incredible amount of posts being updated and created at the same time you won't have any issues. For larger scale applications it would be ideal to schedule the sitemap generation command on a daily basis.

Once you have a sitemap generated, it is a good idea to link your sitemap in Google's Search Console.

Use a sitemap with Laravel

Implement Web Analytics

Tracking which portions of your application receive the most traffic will help you understand your audience. Having tracking data to work from, you can identify the portions of your website that you could consider improving.

Are your users landing on a certain page and just leaving? Is there a particular post that has lots of traffic? These are all very important questions that you should know.

Using a web analytics service can guide you towards figuring out the answers to these question but you still be diligent enough to analyse this data further in your own business context.

Track traffic with Google Analytics

Think SEO From The Start

SEO shouldn't be a last thought when developing any web application. Building in SEO features from the very start will eliminate a lot of stress in the future.

From implementing keywords into your hardcoded content to database design, every feature of your application should have it's possible impact on SEO considered.

Copyright © 2024 | bonnick.dev