Replies: 10 comments 9 replies
-
Any hints on how to do what the op asked? I'm having the same problem and simply modifying the _document.js will not work to change the attributes per page(route), since
As mentioned in the caveats. |
Beta Was this translation helpful? Give feedback.
-
I have the same question. react-helmet has 'bodyAttributes' and 'htmlAttributes' that are very useful for getting the right attributes in place per route in SSR. Is there a way to tap into this with the next/head? |
Beta Was this translation helpful? Give feedback.
-
How about using withRouter and then extracting the current route/path to do the comparison and inject properties based on the current router/path?
|
Beta Was this translation helpful? Give feedback.
-
@srikanth912 That's a good trick! The downside to this is that the logic is now in multiple places — while the rest of the page meta info is in the component. |
Beta Was this translation helpful? Give feedback.
-
This seems like a missing feature, no? It seems like refactoring from |
Beta Was this translation helpful? Give feedback.
-
if you use something like a wrapper, you can use componentDidMount `class Container extends React.Component{
}` |
Beta Was this translation helpful? Give feedback.
-
Can you make this an issue not a discussion? It really IS a missing feature. I will continue using react-helmet-async instead because I am setting classes on body to hide certain elements on the page based on the route. |
Beta Was this translation helpful? Give feedback.
-
For best results, use both // src/pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document';
export default class MyDocument extends Document {
render() {
const pageProps = this.props?.__NEXT_DATA__?.props?.pageProps;
return (
<Html>
<Head />
<body className={ pageProps.isDark ? 'dark-mode' : 'light-mode' }>
<Main />
<NextScript />
</body>
</Html>
);
}
} // src/pages/_app.js
import React, { useEffect } from 'react';
export default function MyApp({ Component, pageProps }) {
useEffect(() => {
document.body.className = pageProps.isDark ? 'dark-mode' : 'light-mode';
});
return <Component {...pageProps} />
} // src/pages/index.js
export async function getStaticProps() {
return { props: { isDark: true } };
}
export default function IndexPage(props) {
return (
<div>
<h1>Home Page</h1>
</div>
);
} Here is the post with full explanation. |
Beta Was this translation helpful? Give feedback.
-
How can I (for example) add a custom className to
body
dynamically....or change the “dir” attribute value.
Beta Was this translation helpful? Give feedback.
All reactions