I use App Routing and Page Routing in my aplication and need add Script for VWO. For pages with Page Routing I added to _app : Import the Script module from 'next/script'.
Added these two properties on the Script tag: a. id = "vwoCode" b. strategy="beforeInteractive" 3.Added script inside "dangerouslySetInnerHTML” property.
In pages with App router:
Added script tag directly in head tag of the RootLayout component in layout.tsx file.
Added these two properties on the script tag: a. id = "vwoCode" b. type="text/javascript" 3.Added script inside "dangerouslySetInnerHTML” property.
But now I need extract it to a separate component if possible. Root layout should be clean and tidy.
Code for layout.tsx
const RootLayout = ({ children }: PropsWithChildren) => {
return (
<ReactQueryClientProvider>
<html lang="en">
<head>
<Script
id="vwoCode"
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
<!-- Start VWO Async SmartCode -->
<link rel="preconnect" href="" />
<script >
// code
</script>
<!-- End VWO Async SmartCode -->
`,
}}
/>
</head>
<body>
<ReactQueryProvider>
</ReactQueryProvider>
</body>
</html>
</ReactQueryClientProvider>
);
};
export default RootLayout;
Code for _app.tsx
const App = ({ Component, pageProps }: AppPropsWithLayout) => {
const getLayout = Component.getLayout || ((page) => <Layout>{page}</Layout>);
return (
<>
<Script
id="vwoCode"
strategy="beforeInteractive"
dangerouslySetInnerHTML={{
__html: `
<!-- Start VWO Async SmartCode -->
<link rel="preconnect" href="" />
<script >
//code
</script>
<!-- End VWO Async SmartCode -->
`,
}}
/>
<AuthProvider>
///
</AuthProvider>
</>
);
};
export default api.withTRPC(App);
How create separate component for Script and add for this 2 files?