Next/Head - return null for conditional head scripts
See original GitHub issueFeature request
When using next/head in a page we should be able to render head scripts conditionally by returning null from a custom component.
Is your feature request related to a problem? Please describe.
If a custom component that can return null (depending on its internal logic) is added as a child of Next/Head the page breaks.
Example:
<Head>
<CustomComponent booleanProp={true} />
</Head>
Where CustomComponent is something like:
const CustomComponent = ({booleanProp}) => {
if(booleanProp){
return <script>some js script</script>
}
return null;
};
The last ‘return null’ will break the page since a file called headManager.js has this logic:
for (var i = 0, j = headCountEl.previousElementSibling; i < headCount; i++, j = j.previousElementSibling) {
if (j.tagName.toLowerCase() === type) {
oldTags.push(j);
}
}
Therefore j.tagName is undefined.
Describe the solution you’d like
I would like to be able to return null (as it’s very common in react components) when my condition is not met; this will let NextJs skip the script.
Something simple like:
if (j.tagName && j.tagName.toLowerCase() === type) {
oldTags.push(j);
}
As an added bonus we can log a warning for head scripts returning null.
Describe alternatives you’ve considered
headManager.js should handle ‘null’ script tags.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:12 (2 by maintainers)
Top Results From Across the Web
How to use Next/Head to render a script - Stack Overflow
js file: it runs the script correctly without rendering extra stuff to the DOM. It looks like this: import Head from "next/head"; function...
Read more >next/head - Next.js
The contents of head get cleared upon unmounting the component, so make sure each page completely defines what it needs in head ,...
Read more >Find first node of loop in a linked list - GeeksforGeeks
Else it returns NULL. Example : Input : Head of below linked list. Output : Pointer to node 2. Recommended Problem.
Read more >next.js head script Code Example - Code Grepper
import Head from 'next/head' function IndexPage() { return ( My page title Hello world! ) } export default IndexPage.
Read more >Script Condition Returns null and not work with Default flow ...
Hello, i am using script both as flow conditions and tasks. Problem1: when i call setTimeout() it returns setTimeout is not defined ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lfades I’ve just gave it a try and unfortunately it still is an issue (although a different one?):
and here’s my
<Head>
component:So now I can’t even use a custom component within
<Head>
, regardless of what it returns (whether a valid <head> DOM element child or a custom React component). I’m on"next": "^9.5.4-canary.20",
I am getting the same issue as @klapec in 10.0.0