diff --git a/js/src/common/Application.js b/js/src/common/Application.js index 008b967fd9..72a88cdf72 100644 --- a/js/src/common/Application.js +++ b/js/src/common/Application.js @@ -237,7 +237,10 @@ export default class Application { } updateTitle() { - document.title = (this.titleCount ? `(${this.titleCount}) ` : '') + (this.title ? this.title + ' - ' : '') + this.forum.attribute('title'); + const count = this.titleCount ? `(${this.titleCount}) ` : ''; + const pageTitleWithSeparator = this.title && m.route() !== '/' ? this.title + ' - ' : ''; + const title = this.forum.attribute('title'); + document.title = count + pageTitleWithSeparator + title; } /** diff --git a/js/src/forum/components/IndexPage.js b/js/src/forum/components/IndexPage.js index dc6794b4ef..72f8747f1a 100644 --- a/js/src/forum/components/IndexPage.js +++ b/js/src/forum/components/IndexPage.js @@ -79,7 +79,7 @@ export default class IndexPage extends Page { extend(context, 'onunload', () => $('#app').css('min-height', '')); - app.setTitle(''); + app.setTitle(app.translator.trans('core.forum.index.meta_title_text')); app.setTitleCount(0); // Work out the difference between the height of this hero and that of the diff --git a/src/Forum/Content/Index.php b/src/Forum/Content/Index.php index 843c25a277..1593ac147b 100644 --- a/src/Forum/Content/Index.php +++ b/src/Forum/Content/Index.php @@ -15,6 +15,7 @@ use Flarum\Http\UrlGenerator; use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\User; +use Illuminate\Contracts\Translation\Translator; use Illuminate\Contracts\View\Factory; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface as Request; @@ -46,13 +47,15 @@ class Index * @param Factory $view * @param SettingsRepositoryInterface $settings * @param UrlGenerator $url + * @param Translator $translator */ - public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url) + public function __construct(Client $api, Factory $view, SettingsRepositoryInterface $settings, UrlGenerator $url, Translator $translator) { $this->api = $api; $this->view = $view; $this->settings = $settings; $this->url = $url; + $this->translator = $translator; } public function __invoke(Document $document, Request $request) @@ -74,6 +77,7 @@ public function __invoke(Document $document, Request $request) $apiDocument = $this->getApiDocument($request->getAttribute('actor'), $params); $defaultRoute = $this->settings->get('default_route'); + $document->title = $this->translator->trans('core.forum.index.meta_title_text'); $document->content = $this->view->make('flarum.forum::frontend.content.index', compact('apiDocument', 'page')); $document->payload['apiDocument'] = $apiDocument; $document->canonicalUrl = $defaultRoute === '/all' ? $this->url->to('forum')->base() : $request->getUri()->withQuery(''); diff --git a/src/Frontend/Document.php b/src/Frontend/Document.php index 0133e9768a..f938041c51 100644 --- a/src/Frontend/Document.php +++ b/src/Frontend/Document.php @@ -13,6 +13,7 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Support\Arr; +use Psr\Http\Message\ServerRequestInterface as Request; /** * A view which renders a HTML skeleton for Flarum's frontend app. @@ -131,14 +132,20 @@ class Document implements Renderable */ protected $forumApiDocument; + /** + * @var Request + */ + protected $request; + /** * @param Factory $view * @param array $forumApiDocument */ - public function __construct(Factory $view, array $forumApiDocument) + public function __construct(Factory $view, array $forumApiDocument, Request $request) { $this->view = $view; $this->forumApiDocument = $forumApiDocument; + $this->request = $request; } /** @@ -173,7 +180,9 @@ protected function makeView(): View */ protected function makeTitle(): string { - return ($this->title ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title'); + $onHomePage = rtrim($this->request->getUri()->getPath(), '/') === ''; + + return ($this->title && ! $onHomePage ? $this->title.' - ' : '').Arr::get($this->forumApiDocument, 'data.attributes.title'); } /** diff --git a/src/Frontend/Frontend.php b/src/Frontend/Frontend.php index e293ef3599..63257decd1 100644 --- a/src/Frontend/Frontend.php +++ b/src/Frontend/Frontend.php @@ -50,7 +50,7 @@ public function document(Request $request): Document { $forumDocument = $this->getForumDocument($request); - $document = new Document($this->view, $forumDocument); + $document = new Document($this->view, $forumDocument, $request); $this->populate($document, $request);