Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): allow overriding ssr/dev template meta tags #7952

Merged
merged 8 commits into from Aug 19, 2022

Conversation

slorber
Copy link
Collaborator

@slorber slorber commented Aug 12, 2022

Motivation

Related to #7944

Move robots and viewport meta to <Head> so that we can more easily override them

Keep charSet and generator in the static templates for now, until a solution is found (we want them at the very top)

Test Plan

Manual 😅 Inspecting preview metadata, comparing to prod

Test page for overrides: https://deploy-preview-7952--docusaurus-2.netlify.app/tests/pages/head-metadata/

Test links

Deploy preview:


Former solution attempt (moving everything to use Head)

Our eta (prod) / ejs (dev) templates have metadata:

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="generator" content="Docusaurus v<%= it.version %>">
    <% if (it.noIndex) { %>
      <meta name="robots" content="noindex, nofollow" />
    <% } %>

But it's not possible to override it using react-helmet-async (and thus not either using themeConfig.metadata because those are static.

Apparently our <Head> component is capable to de-duplicate meta tags with the same name

Possible alternative: keep metadata in the template, but add data-rh='true' apparently allows react-helmet-async to kick in and update the existing tags instead of creating new ones. (see staylor/react-helmet-async#108
)

For consistency that looks better to always use <Head> and move out 100% of those metadata out of the templates.

CleanShot 2022-08-12 at 13 51 51@2x

Note: this works but I find this not very convenient to debug. I like the fact title, generator etc are at the very top, it makes it easy to inspect if a site is using Docusaurus or not. Maybe it's better to use the alternative solution?

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Aug 12, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Aug 12, 2022
@Josh-Cena Josh-Cena changed the title fix(core): ssr/dev template meta tags can't be overridden => migrate to <Head> fix(core): allow overriding ssr/dev template meta tags Aug 12, 2022
@netlify
Copy link

netlify bot commented Aug 12, 2022

[V2]

Name Link
🔨 Latest commit f1de68d
🔍 Latest deploy log https://app.netlify.com/sites/docusaurus-2/deploys/62ff9500b9c6340008f68aef
😎 Deploy Preview https://deploy-preview-7952--docusaurus-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions
Copy link

github-actions bot commented Aug 12, 2022

⚡️ Lighthouse report for the deploy preview of this PR

URL Performance Accessibility Best Practices SEO PWA Report
/ 🟢 93 🟢 98 🟢 100 🟢 100 🟠 80 Report
/docs/installation 🟠 87 🟢 100 🟢 100 🟢 100 🟢 90 Report

@github-actions
Copy link

github-actions bot commented Aug 12, 2022

Size Change: +494 B (0%)

Total Size: 814 kB

Filename Size Change
website/build/assets/js/main.********.js 610 kB +480 B (0%)
ℹ️ View Unchanged
Filename Size Change
website/.docusaurus/globalData.json 52.5 kB 0 B
website/build/assets/css/styles.********.css 111 kB 0 B
website/build/index.html 40.8 kB +14 B (0%)

compressed-size-action

Copy link
Collaborator

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this!

@slorber slorber marked this pull request as draft August 12, 2022 11:54
@Josh-Cena
Copy link
Collaborator

I like the fact title, generator etc are at the very top, it makes it easy to inspect if a site is using Docusaurus or not.

I think the same. I think it's possible to promote certain tags in react-helmet?

@slorber
Copy link
Collaborator Author

slorber commented Aug 12, 2022

The problem is our plugin lifecycle uses a totally different code path, using plain strings and not <Head> 😅

  injectHtmlTags?: (args: {content: Content}) => {
    headTags?: HtmlTags;
    preBodyTags?: HtmlTags;
    postBodyTags?: HtmlTags;
  };

For example all the PWA plugin metadata also can't be overridden (no "data-rh" attribute)

@slorber
Copy link
Collaborator Author

slorber commented Aug 12, 2022

Important note: apparently the order has an impact on SEO: https://github.com/staylor/react-helmet-async#prioritizing-tags-for-seo

"generator" meta is not in the list (PR staylor/react-helmet-async#180)

@Josh-Cena
Copy link
Collaborator

Ah I see. That's not a bad thing to do—smaller JS bundle. Maybe we can keep generator in the template? (Shouldn't be overridable anyway)

@slorber
Copy link
Collaborator Author

slorber commented Aug 12, 2022

Ah I see. That's not a bad thing to do—smaller JS bundle. Maybe we can keep generator in the template? (Shouldn't be overridable anyway)

I'd like later to provide extra tooling on top of Docusaurus to make it even easier, in which case overriding the generator might be a use-case

For example a simple CLI command npx yoloblog ./blog --title 'Sebastien Lorber Blog' --color blue

@fflaten
Copy link

fflaten commented Aug 15, 2022

The problem is our plugin lifecycle uses a totally different code path, using plain strings and not <Head> 😅
...
For example all the PWA plugin metadata also can't be overridden (no "data-rh" attribute)

👍 Should definitely work as well, but that's maybe a separate issue (if not created already) since it already doesn't work? And not a blocking issue for this PR which is more realistic to get implemented?

I assume that will be a larger rewrite and should it require a different approach for SiteMetadataDefaults then that PR will fix all.

... in which case overriding the generator might be a use-case

Some users might also want to hide it. Not familiar with security risks in SSR, but company security policies may require it - especially version.

@slorber
Copy link
Collaborator Author

slorber commented Aug 19, 2022

Going to keep "generator" (see staylor/react-helmet-async#180) and "charSet" (see staylor/react-helmet-async#181) outside of Helmet system for now

@slorber
Copy link
Collaborator Author

slorber commented Aug 19, 2022

Also reverting the usage of prioritizeSeoTags for now, it produces a suspicious diff on our test snapshot 😅 we didn't use it before and it does not seem critical

see https://github.com/facebook/docusaurus/runs/7917709859?check_suite_focus=true

CleanShot 2022-08-19 at 15 32 51@2x

@slorber slorber marked this pull request as ready for review August 19, 2022 13:31
@slorber
Copy link
Collaborator Author

slorber commented Aug 19, 2022

Also found out some limitations when using data-rh=true: staylor/react-helmet-async#108 (comment)

This leads to these metas being automatically removed (if not found in <Head>) or moved in the middle of <head> at an incorrect position (if found in <Head>)

For now, let's keep it simple: charSet and generator won't be overrideable, but viewport and robots will be

@slorber
Copy link
Collaborator Author

slorber commented Aug 19, 2022

working as I want it to be for now 👍

@slorber slorber merged commit 94067ce into main Aug 19, 2022
@slorber slorber deleted the slorber/update-site-metadata branch August 19, 2022 14:47
@fflaten
Copy link

fflaten commented Aug 19, 2022

Wow, that was fast. Thanks! 🙂

@slorber slorber added the to backport This PR is planned to be backported to a stable version of Docusaurus label Aug 19, 2022
@slorber slorber added backported This PR has been backported to a stable version of Docusaurus and removed to backport This PR is planned to be backported to a stable version of Docusaurus labels Sep 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backported This PR has been backported to a stable version of Docusaurus CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants