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 b15d396
Show file tree
Hide file tree
Showing 4 changed files with 34 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

0 comments on commit b15d396

Please sign in to comment.