Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SEO Pagination Meta Tags rel="next" / rel="prev" #79

Open
mauricekindermann opened this issue May 21, 2024 · 5 comments
Open

SEO Pagination Meta Tags rel="next" / rel="prev" #79

mauricekindermann opened this issue May 21, 2024 · 5 comments

Comments

@mauricekindermann
Copy link

As far as I can see this package doesn't handle pagination SEO tags? I had to publish my pagination views to make them pretty, so for now I've just added them this way.

But if there is a nicer solution let me know.. Otherwise I can look to do a PR in the future.

@pushOnce('head.end')
    @if ($paginator->hasPages())

        @if (!$paginator->onFirstPage())
            <link rel="prev" href="{{ $paginator->previousPageUrl() }}">
        @endif

        @if ($paginator->hasMorePages())
            <link rel="next" href="{{ $paginator->nextPageUrl() }}">
        @endif

    @endif
@endPushOnce
@ralphjsmit
Copy link
Owner

Hi @mauricekindermann, I actually didn't know these pagination tags existed. I think it would be great to add. If you are willing to provide a PR, you could add a $paginator property to the SEOData object, then create a special PaginatorTagCollection and initialize it from the TagCollection, and then return a collection with LinkTags from the PaginatorTagCollection.

@mauricekindermann
Copy link
Author

Hmm. I tried but failed. I couldn't get it working with the livewire sub compontent I am using. seoData recieves it but then loses it, my best guess is due to the livewire being overwritten by the main page controlling it is my guess.

If you like I can PR my non-working code, but that might be more work than is worth it.

I will try again in a few days.

@mauricekindermann
Copy link
Author

mauricekindermann commented May 22, 2024

Actually here is the code, better than a PR since it doesn't work.

seoData

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
//
        public ?LengthAwarePaginator $paginator = null,

TagCollection

use RalphJSmit\Laravel\SEO\Support\PaginatorTagCollection;
///
            PaginatorTagCollection::initialize($SEOData?->paginator),

PaginatorTagCollection

<?php

namespace RalphJSmit\Laravel\SEO\Support;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;

class PaginatorTagCollection extends Collection implements Renderable
{
    public static function initialize(?LengthAwarePaginator $paginator): static
    {
        $collection = new static();

        if ($paginator && $paginator->hasPages()) {
             // I could not get this to return true.
        }

        return $collection;
    }

    public function render()
    {
        return $this->map(fn (LinkTag $tag) => $tag->render())->implode(PHP_EOL);
    }
}

And in my view I would return

        return view('livewire.x-component', [
            'SEOData' => new SEOData(
                paginator: $this->paginatedMethod(),
            ),
        ]);

I'm still pretty new to laravel so it's a bit of a black box to me to understand how to debug it in a way that will help me get it working.

@ralphjsmit
Copy link
Owner

If you could PR your wip code then I can take a look at that.
For reference, how does your Livewire component look like where you have the pagination?

@ralphjsmit
Copy link
Owner

Hey @mauricekindermann, I think the reason is that you're providing the SEOData parameter to the Livewire Blade component (so the actual Blade of the LW component), whilst the SEOData should be provided to the layout file.

Does the paginator ?page= property change on the same page, or do you redirect every time someone goes to the next page? Because if you change the ?page= property dynamically on the page, without a redirect, then the <head> needs to be updated on the page without redirect, which isn't possible with LW).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants