-
Notifications
You must be signed in to change notification settings - Fork 157
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
HelmetDispatcher leaks when used in combination with Suspense #226
Comments
Looks like I'm having this exact issue, which is quite annoying. I'm using @staylor Could you please look into this issue? Currently it prevents me from even starting using |
I stopped using react-helmet-async and instead changed to the following simple implementation. const DocumentTitle = ({title}: {title: string}) => {
useEffect(()=>{
document.title = title
}, [title])
return null
} |
Thanks @satoren. Your solution does work for most basic scenarios, but it does not work for scenarios when a page does not have its own title. In such cases it's expected that the document should have the "default" title, set by a parent layout component up in the tree. However, using this simple solution you get the title defined by the latest activated page, regardless of the component hierarchy 😢 |
Or it could incorporate a few improvements, such as const DocumentTitle = ({title}: {title: string}) => {
useEffect(()=>{
const prev = document.title
document.title = title
return ()=>{
document.title = prev
}
}, [title])
return null
}
// Or if you want to deal with more complex scenarios
const DocumentTitle = ({ title }: { title: string }) => {
const [titleElement] = React.useState<HTMLTitleElement>(() => document.createElement('title'))
useEffect(() => {
// The title is inserted into the head in reverse order of rendering.
document.head.prepend(titleElement);
return () => {
titleElement.remove()
}
}, [titleElement])
useEffect(() => {
titleElement.innerText = title
}, [title, titleElement])
} |
If Suspense occurs after render, componentWillUnmount is not called.
Therefore, instances added below will not be removed and will leak.
react-helmet-async/src/Dispatcher.tsx
Line 71 in 5f4da38
Also, it will not be updated properly.
https://react.dev/reference/react/Component#componentdidmount
Reference Articles (This is not my post.)
https://zenn.dev/mtblue81/articles/f88821a3c3392e
https://mtblue81.github.io/react-helmet-sample/
The text was updated successfully, but these errors were encountered: