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

Add optional origin to withUrl #38

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/SEOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,13 @@ public function favicon(): static
}

/** Append canonical URL tags to the document head. */
public function withUrl(): static
public function withUrl(?string $origin = null): static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is origin the right term here, or would canonical be more accurate?

Copy link
Contributor Author

@benbjurstrom benbjurstrom Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This RFC says the scheme/host/port combination is an origin. Note that's all we're passing in here. The full canonical url is still derived from the request path.

https://www.rfc-editor.org/rfc/rfc6454#section-5

{
$this->url(request()->url());
if ($origin) {
$this->url(trim($origin, '/') . '/' . trim(request()->path(), '/'));
} else {
$this->url(request()->url());
}

return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Pest/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use ArchTech\SEO\SEOManager;
use Illuminate\Http\Request;
use Mockery\MockInterface;

test('set returns the set value', function () {
expect(seo()->set('foo', 'bar'))->toBe('bar');
Expand Down Expand Up @@ -118,6 +120,16 @@
->toContain('<link rel="canonical" href="http://localhost" />');
});

test('canonical url accepts origin', function () {
$this->get('/testing/5');

seo()->withUrl('https://foo.com');

expect(meta())
->toContain('<meta property="og:url" content="https://foo.com/testing/5" />')
->toContain('<link rel="canonical" href="https://foo.com/testing/5" />');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link MDN docs or something where I can see that both og:url and canonical should be the canonical link?

Copy link
Contributor Author

@benbjurstrom benbjurstrom Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The package is already coupling og:url and canonical together. All my pull request does is give users the option to manually set the origin.

@if(seo('url'))
<meta property="og:url" content="@seo('url')">
<link rel="canonical" href="@seo('url')">
@endif

});

test('canonical url can be changed', function () {
seo()->withUrl();

Expand Down
Loading