How to use GraphQL queried data in SSR rendered html.js
?
#38930
-
We are using Strapi as CMS, and Gatsby as static site generator, all HTMLs, JS, CSS, images and other resources will be packaged and deployed independently from the original CMS, as a static site. We use React and GraphQL static queries already for client components. Now I want to add custom (global) stylesheets which can be maintained in a CMS (Strapi) which is renderend on build time into the HTML. We have our custom import React from "react"
import PropTypes from "prop-types"
import SsrCustomStyles from "./ssr/SsrCustomStyles"
export default function HTML(props) {
return (
<html {...props.htmlAttributes}>
<head>
...
{props.headComponents}
<SsrCustomStyles />
</head>
<body {...props.bodyAttributes}>
.... In How do I fetch data with GraphQL at build time in a component included in What I tried:
Variable
Doesn't build, I get following error:
An example for the latter case: export default function SsrCustomStyles({ data }) {
return (
<StaticQuery query={query} render={data => {
const settings = data.allStrapiAppSettings.nodes[0]
const files = data.allCssFiles.nodes[0]
return <>
{getStyleSheets(settings, files).map(
({ id, href }) => <StyleSheet key={id} id={id} href={href} />
)}
</> }
} />
)
}
export const query = graphql`
query {
allStrapiAppSettings { nodes { ... } }
allCssFiles: allFile(filter: {extension: {in: ["css"]}}) { nodes { ... } }
}
` |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi Paul, Using As mention in Gatsby Head Api Doc :
I think this is the best option in Gatsby. |
Beta Was this translation helpful? Give feedback.
Have you try to use Head Api ?
I show you an example, using bootstrap css cdn, and a simple static query as a Proof of Concept…
First, create a
DefaultHead
component, like this