From db7269ff28068d0c0e90e3cd6765a091794bb639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 26 May 2021 13:18:13 +0200 Subject: [PATCH] Canonical URLs, fix #1 --- README.md | 35 +++++++++++++++++++------- assets/views/components/meta.blade.php | 4 +++ src/SEOManager.php | 14 ++++++++++- tests/Pest/ManagerTest.php | 24 ++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 99ea62f..e1ff3f9 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ Use the `seo()` helper to retrieve the SeoManager instance, on which you can cal Available methods: ```js site(string $site) +url(string $url) title(string $title) description(string $description) image(string $url) @@ -108,6 +109,31 @@ seo() ->description(default: 'We are a web development agency that ...'); ``` +### Extra tags + +To add more tags to the head, you can use the `tag()` and `rawTag()` methods: + +```php +seo()->tag('fb:image', asset('foo')); +seo()->rawTag(''); +``` + +### Canonical URL + +To enable the `og:url` and canonical URL `link` tags, call: + +```php +seo()->withUrl(); +``` + +This will make the package read from `request()->url()` (= the current URL *without* the query string). + +If you wish to change the URL, call `seo()->url()`: + +```php +seo()->url(route('products.show', $this->product)); +``` + ### Modifiers You may want to modify certain values before they get inserted into the template. For example, you may want to suffix the meta `` with `| ArchTech` when it has a non-default value. @@ -226,15 +252,6 @@ This package is completely flexible, and can be customized either by having its You can publish the Blade views by running `php artisan vendor:publish --tag=seo-views`. -### Extra tags - -To add more tags to the head, you can use the `tag()` and `rawTag()` methods: - -```php -seo()->tag('fb:image', asset('foo')); -seo()->rawTag('<meta property="fb:url" content="bar" />'); -``` - ### Extensions To use a custom extension, create a Blade *component* with the desired meta tags. The component should read data using `{{ seo()->get('foo') }}` or `@seo('foo')`. diff --git a/assets/views/components/meta.blade.php b/assets/views/components/meta.blade.php index ef747b4..7372978 100644 --- a/assets/views/components/meta.blade.php +++ b/assets/views/components/meta.blade.php @@ -4,6 +4,10 @@ @if(seo('title')) <meta property="og:title" content="@seo('title')" /> @endif @if(seo('description')) <meta property="og:description" content="@seo('description')" /> @endif @if(seo('image')) <meta property="og:image" content="@seo('image')" /> @endif +@if(seo('url')) + <meta property="og:url" content="@seo('url')" /> + <link rel="canonical" href="@seo('url')" /> +@endif @foreach(seo()->tags() as $tag) {!! $tag !!} diff --git a/src/SEOManager.php b/src/SEOManager.php index bb900ea..edecd7f 100644 --- a/src/SEOManager.php +++ b/src/SEOManager.php @@ -10,6 +10,7 @@ use Illuminate\Support\Str; /** * @method $this title(string $title) Set the title. * @method $this description(string $description) Set the description. + * @method $this url(string $url) Set the canonical URL. * @method $this site(string $site) Set the site name. * @method $this image(string $url) Set the cover image. * @method $this twitter(enabled $bool = true) Enable the Twitter extension. @@ -51,7 +52,10 @@ class SEOManager /** Get a list of used keys. */ protected function getKeys(): array { - return collect(['site', 'title', 'image', 'description', 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description']) + return collect([ + 'site', 'title', 'image', 'description', 'url', + 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description' + ]) ->merge(array_keys($this->defaults)) ->merge(array_keys($this->values)) ->unique() @@ -172,6 +176,14 @@ class SEOManager return $this->set('image', "https://s.useflipp.com/{$template}.png?s={$signature}&v={$query}"); } + /** Append canonical URL tags to the document head. */ + public function withUrl(): static + { + $this->url(request()->url()); + + return $this; + } + /** Get all extra head tags. */ public function tags(): array { diff --git a/tests/Pest/ManagerTest.php b/tests/Pest/ManagerTest.php index 27b5257..c12a040 100644 --- a/tests/Pest/ManagerTest.php +++ b/tests/Pest/ManagerTest.php @@ -94,3 +94,27 @@ test('raw tags can be added to the template', function () { expect(meta())->toContain('<meta foo bar>'); }); + +test('canonical url is not included by default', function () { + expect(meta()) + ->not()->toContain('og:url') + ->not()->toContain('canonical'); +}); + +test('canonical url can be read from request', function () { + seo()->withUrl(); + + expect(meta()) + ->toContain('<meta property="og:url" content="http://localhost" />') + ->toContain('<link rel="canonical" href="http://localhost" />'); +}); + +test('canonical url can be changed', function () { + seo()->withUrl(); + + seo()->url('http://foo.com/bar'); + + expect(meta()) + ->toContain('<meta property="og:url" content="http://foo.com/bar" />') + ->toContain('<link rel="canonical" href="http://foo.com/bar" />'); +});