fix(v2): Unbreak blog post title by handling title fallback in LayoutHead
#4667
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Fix #4666.
What caused the problem
The
Head
component of docusaurus is re-export ofHelmet
. According to Helmet docs, items rendered later in Head will override the ones rendered earlier. Currently, there are two places where title is changed when blog post is rendered:<Layout>
and<Seo>
, and<Seo>
happens later than<Layout>
.When rendering a blog post page, it starts in
BlogPostPage
, which has the correct title:https://github.com/SamChou19815/docusaurus/blob/d21444b6fb03db87796068fd476f1d10af986182/packages/docusaurus-theme-classic/src/theme/BlogPostPage/index.tsx#L24-L29
Then it starts render
BlogPostItem
, which has a SEO component that will rerender header: https://github.com/SamChou19815/docusaurus/blob/792f4ac6fb8d7e1124f4b9f5151a827929e6dc50/packages/docusaurus-theme-classic/src/theme/BlogPostItem/index.tsx#L99-L102Here you can see that the title prop is not passed in. Finally, since
title
prop is not passed, it causes the blog post title to be overridden to only site title according tohttps://github.com/SamChou19815/docusaurus/blob/792f4ac6fb8d7e1124f4b9f5151a827929e6dc50/packages/docusaurus-theme-classic/src/theme/Seo/index.tsx#L15-L22
https://github.com/SamChou19815/docusaurus/blob/d21444b6fb03db87796068fd476f1d10af986182/packages/docusaurus-theme-common/src/utils/generalUtils.ts#L10-L14
My fix is simple: adding the title prop. The practice seems to be consistently with the rest of the codebase, since every other occurrences ofSeo
will pass in the title propUsing the advice from @slorber, I reverted the change in #4600 that makes the SEO component handle title fallback. Instead, the fallback is moved to
LayoutHead
and the SEO fallback will only change title if the passed intitle
prop is not null.Have you read the Contributing Guidelines on pull requests?
Yes
Test Plan
Goto
/blog/2021/03/09/releasing-docusaurus-i18n
on preview site, and the title is correct again.Related PRs
N/A