Skip to content

Commit 6141e5e

Browse files
authored
feat: better support 3rd party extensions setting canonical urls (#4244)
1 parent 7680099 commit 6141e5e

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

framework/core/src/Forum/Content/Discussion.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ public function __invoke(Document $document, Request $request)
100100
$document->content = $this->view->make('flarum.forum::frontend.content.discussion', compact('apiDocument', 'page', 'hasPrevPage', 'hasNextPage', 'getResource', 'posts', 'url'));
101101
$document->payload['apiDocument'] = $apiDocument;
102102

103-
$document->canonicalUrl = $url([]);
103+
// Only apply the canonical URL if it's empty. It could have been set by another extension already,
104+
// if so, we'll leave it alone.
105+
if (empty($document->canonicalUrl)) {
106+
$document->canonicalUrl = $url([]);
107+
}
108+
104109
$document->page = $page;
105110
$document->hasNextPage = $hasNextPage;
106111

framework/core/src/Forum/Content/Index.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ public function __invoke(Document $document, Request $request)
8989
$document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page'));
9090
$document->payload['apiDocument'] = $apiDocument;
9191

92-
$document->canonicalUrl = $this->url->to('forum')->base().($defaultRoute === '/all' ? '' : $request->getUri()->getPath());
92+
// Only apply the canonical URL if it's empty. It could have been set by another extension already,
93+
// if so, we'll leave it alone.
94+
if (empty($document->canonicalUrl)) {
95+
$document->canonicalUrl = $this->url->to('forum')->base().($defaultRoute === '/all' ? '' : $request->getUri()->getPath());
96+
}
97+
9398
$document->page = $page;
9499
$document->hasNextPage = isset($apiDocument->links->next);
95100

framework/core/src/Forum/Content/User.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public function __invoke(Document $document, Request $request)
4747
$user = $apiDocument->data->attributes;
4848

4949
$document->title = $user->displayName;
50-
$document->canonicalUrl = $this->url->to('forum')->route('user', ['username' => $user->slug]);
50+
51+
// Only apply the canonical URL if it's empty. It could have been set by another extension already,
52+
// if so, we'll leave it alone.
53+
if (empty($document->canonicalUrl)) {
54+
$document->canonicalUrl = $this->url->to('forum')->route('user', ['username' => $user->slug]);
55+
}
56+
5157
$document->payload['apiDocument'] = $apiDocument;
5258

5359
return $document;

0 commit comments

Comments
 (0)