admin管理员组文章数量:1410689
I am trying to integrate google translate dropdown widget in my Next Js application, I integrated that the widget also but when I try to redirect from one page to another where the widget will not be available, the page gets blank and throws error like NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
I tried to not clean up the component for persisting the script but it also doesn't seem to be working.
"use client";
import { useEffect } from "react";
const GoogleTranslate = () => {
useEffect(() => {
// Load the Google Translate script
const script = document.createElement("script");
script.src =
".js?cb=googleTranslateElementInit";
script.async = true;
document.body.appendChild(script);
// Initialize the Google Translate element
window.googleTranslateElementInit = () => {
new window.google.translate.TranslateElement(
{ pageLanguage: "en" },
"google_translate_element"
);
};
}, []);
return <div id="google_translate_element"></div>;
};
export default GoogleTranslate;
This is my google translate component code and I am using it in the header of the application like this,
<AppBox>
<GoogleTranslate />
</AppBox>
Note: I am using App Router
I am trying to integrate google translate dropdown widget in my Next Js application, I integrated that the widget also but when I try to redirect from one page to another where the widget will not be available, the page gets blank and throws error like NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
I tried to not clean up the component for persisting the script but it also doesn't seem to be working.
"use client";
import { useEffect } from "react";
const GoogleTranslate = () => {
useEffect(() => {
// Load the Google Translate script
const script = document.createElement("script");
script.src =
"https://translate.google/translate_a/element.js?cb=googleTranslateElementInit";
script.async = true;
document.body.appendChild(script);
// Initialize the Google Translate element
window.googleTranslateElementInit = () => {
new window.google.translate.TranslateElement(
{ pageLanguage: "en" },
"google_translate_element"
);
};
}, []);
return <div id="google_translate_element"></div>;
};
export default GoogleTranslate;
This is my google translate component code and I am using it in the header of the application like this,
<AppBox>
<GoogleTranslate />
</AppBox>
Note: I am using App Router
Share Improve this question edited Mar 12 at 8:37 Hilory 2,1577 gold badges14 silver badges30 bronze badges asked Mar 11 at 13:57 Selvakumar SekarSelvakumar Sekar 11 bronze badge 2- Can the script not be available globally? Also, you may want to consider using the nextjs Script tag- nextjs./docs/app/api-reference/components/script – Mark Hollis Commented Mar 11 at 15:00
- @MarkHollis Thanks for your reply. I tried to register it globally also, but I faced the same issue. I will try adding the next JS built-in script tag as you mentioned. Once again, thank you. – Selvakumar Sekar Commented Mar 11 at 15:22
1 Answer
Reset to default 0After hours of debugging, I came across this comment in github, which solves my problem. I hope those who face this issue can also look into this comment, which will be helpful, thank you! https://github/facebook/react/issues/11538#issuecomment-390386520
本文标签: javascriptIntegrating google translate widget in Next JS applicationStack Overflow
版权声明:本文标题:javascript - Integrating google translate widget in Next JS application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744790345a2625247.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论