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

the head config to support function in doc theme #410

Merged
merged 1 commit into from
Mar 14, 2022

Conversation

Djuki
Copy link
Contributor

@Djuki Djuki commented Mar 14, 2022

@shuding I was solving my issue described here #379, and I found what is the issue.

This PR will add a support for passing a function in config.head configuration for Docs theme.

To support config.head as a function we need to have latest code from core branch for renderComponent metod:

const renderComponent = <T,>(
  ComponentOrNode: React.FC<T> | React.ReactNode,
  props: T,
  functionOnly?: boolean
) => {
  if (!ComponentOrNode) return null
  if (typeof ComponentOrNode === 'function') {
    if (functionOnly) return ComponentOrNode(props)
    return <ComponentOrNode {...props} />
  }
  return ComponentOrNode
}

Note that this is not a function we have in latest tag (v2.0.0-beta.5). The functionOnly is added later.

Important part is we have functionOnly and in case it is true we return ComponentOrNode(props). And that is what will make config.head as a function to be rendered into the Head.

Also we need to do what my PR suggest to call renderComponent with the functionOnly = true, because only in that case we will support both cases for config.head: function and Element.

Example of the config.head as a function we will support after this PR, and releasing new tag.

  head: ({title, meta}) => (
    <>
      <meta name="description" content={meta.description ? meta.description: "Default description"} />
    </>
  ),

@vercel
Copy link

vercel bot commented Mar 14, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

nextra – ./

🔍 Inspect: https://vercel.com/vercel/nextra/HXozmYMGQLRMt2u4dPHsJRQcoxBb
✅ Preview: Canceled

nextra-theme-docs-dev – ./examples/swr-site

🔍 Inspect: https://vercel.com/vercel/nextra-theme-docs-dev/DFjrd9UeikpuuiYgam3szMsyEw8E
✅ Preview: https://nextra-theme-docs-dev-git-fork-djuki-bugfix-head-theme-d-911781.vercel.sh

@shuding
Copy link
Owner

shuding commented Mar 14, 2022

This makes sense, thank you!

@raproid
Copy link

raproid commented Sep 22, 2022

@B2o5T @shuding What is the replacement for meta.description in beta 25? The page won't render showing me that it doesn't recognize meta.description in my theme config.

I'm using this example for testing:
head: ({title, meta}) => (
<>
<meta name="description" content={meta.description ? meta.description: "Default description"} />
</>
),

@dimaMachina
Copy link
Collaborator

config.frontMatter.description ||

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants