Skip to content

Commit

Permalink
fix(News):Canonical URL for individual news articles based on permali…
Browse files Browse the repository at this point in the history
…nk values
  • Loading branch information
nickdenardis committed Aug 6, 2024
1 parent 86e8f76 commit 41b48a7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function show(Request $request): View|Redirector|RedirectResponse

$request->data['base']['page']['title'] = $article['article']['data']['title'];
$request->data['base']['page']['description'] = $article['article']['data']['meta_description'];
$request->data['base']['page']['canonical'] = $request->data['base']['server']['url'] ?? '';
$request->data['base']['page']['canonical'] = $this->article->getCanonicalUrl($article['article']['data'], $request->data['base']);

if (!empty($article['article']['data']['hero_image']['url'])) {
$request->data['base']['hero'][]['relative_url'] = $article['article']['data']['hero_image']['url'];
Expand Down
23 changes: 23 additions & 0 deletions app/Repositories/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,29 @@ public function find($id, $application_ids, $preview = null)
return $article;
}

/**
* {@inheritdoc}
*/
public function getCanonicalUrl(array $article, array $request)
{
// Fallback if there is no route path defined for news
if (empty($article) || empty($request['site']['news']['route_path'])) {
return $request['server']['url'] ?? '';
}

// Use the domain of the current page
$uri = parse_url($request['server']['url']);

// Build up the fully qualified URL for the article
return
trim($uri['scheme'] . '://' . $uri['host'] . '/' . $request['site']['subsite-folder'], '/') .
str_replace(
['{$permalink}', '{$id}'],
[$article['permalink'], $article['id']],
$request['site']['news']['route_path']
);
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions contracts/Repositories/ArticleRepositoryContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public function listing($application_ids, $limit = 5, $page = 1, $topics = []);
*/
public function find($id, $application_ids, $preview);

/**
* Build the fully qualified URI for the article
*
* @param array $article
* @param array $request
* @return array
*/
public function getCanonicalUrl(array $article, array $request);

/**
* Get the image for the meta data.
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "base",
"private": true,
"version": "8.10.5",
"version": "8.10.6",
"description": "",
"scripts": {
"dev": "npm run development",
Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/Repositories/ArticleRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,29 @@ public function article_with_featured_image_should_return_url(): void
$this->assertEquals($article['data']['featured']['url'], $imageUrl['url']);
$this->assertEquals($article['data']['featured']['alt_text'], $imageUrl['alt_text']);
}

#[Test]
public function article_with_news_route_should_return_canonical_url(): void
{
$article = app(Article::class)->create(
1,
true,
[
'permalink' => 'permalink',
'id' => 123,
]
);

$request = [
'server' => ['url' => 'https://example.com/subsite/news/slug-id'],
'site' => [
'subsite-folder' => 'subsite/',
'news' => ['route_path' => '/news/{$permalink}-{$id}'],
],
];

$url = app(ArticleRepository::class)->getCanonicalUrl($article['data'], $request);

$this->assertEquals($url, 'https://example.com/subsite/news/permalink-123');
}
}

0 comments on commit 41b48a7

Please sign in to comment.