diff --git a/app/Http/Controllers/ArticleController.php b/app/Http/Controllers/ArticleController.php index e09188c3..629b223e 100644 --- a/app/Http/Controllers/ArticleController.php +++ b/app/Http/Controllers/ArticleController.php @@ -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']; diff --git a/app/Repositories/ArticleRepository.php b/app/Repositories/ArticleRepository.php index 15a51a0a..0818c462 100644 --- a/app/Repositories/ArticleRepository.php +++ b/app/Repositories/ArticleRepository.php @@ -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} */ diff --git a/contracts/Repositories/ArticleRepositoryContract.php b/contracts/Repositories/ArticleRepositoryContract.php index 9f4ee584..ab36b412 100644 --- a/contracts/Repositories/ArticleRepositoryContract.php +++ b/contracts/Repositories/ArticleRepositoryContract.php @@ -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. * diff --git a/package.json b/package.json index 027abe55..cab33471 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "base", "private": true, - "version": "8.10.5", + "version": "8.10.6", "description": "", "scripts": { "dev": "npm run development",