I am trying to enable SSR in in Next.JS 14 + redux/toolkit + redux-persist but the whole page is not appearing inside the page source.
CODE: (layout.js)
"use client";
import './globals.css';
import { usePathname } from 'next/navigation';
import { Suspense, useState, useEffect } from 'react';
import classes from './App.module.css';
import { CSRReduxProviders, SSRReduxProviders } from './_store/reduxProvider'
import Navbar from './_Layouts/navbar'
import Footer from './_Layouts/footer/Footer'
import { LoaderSkeleton } from './_components'
import { ToastContainer } from 'react-toastify'
import { AppProgressBar as ProgressBar } from 'next-nprogress-bar';
import Script from 'next/script';
import { HomePage, OffCanvasPopup } from './_components';
import Head from 'next/head';
export default function RootLayout({ children }) {
const pathname = usePathname();
const [title, setTitle] = useState('Sustainable Products | Ecowiser');
useEffect(() => {
if (pathname.includes('/search')) {
setTitle(`${pathname.split('/').pop().replaceAll('%20', ' ').replaceAll('_', ' ').replaceAll('-', ' ')} | Ecowiser`);
} else if (pathname.includes('/category')) {
setTitle(`${pathname.split('/').pop().replaceAll('-', ' ')} | Ecowiser`);
} else if (pathname.includes('/lux')) {
setTitle(`Luxury Products | Ecowiser`);
} else if (pathname.includes('/how-we-rate')) {
setTitle("How We Rate | Ecowiser")
} else if (pathname.split('/')[1].includes('-')) {
setTitle(`${pathname.split('/').pop().split('-').slice(1).join(" ")} | Ecowiser`);
}
}, [pathname]);
return (
<html>
<Head>
<meta charSet="utf-8" />
<meta name="description" content={'EcoWiser Products is a directory of sustainable products, helping consumers find eco-friendly items made from renewable resources and biodegradable materials.'} />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href={`${process.env.NEXT_PUBLIC_BASE_PATH}/icon.ico`} />
<link rel="shortcut icon" href={`${process.env.NEXT_PUBLIC_BASE_PATH}/icon.ico`} />
<link rel="apple-touch-icon" href={`${process.env.NEXT_PUBLIC_BASE_PATH}/apple-icon.png`} />
<title>{title}</title>
</Head>
<body suppressHydrationWarning={true}>
<Script
id={"Skimlinks"}
strategy="beforeInteractive"
src={`https://s.skimresources.com/js/263708X1748630.skimlinks.js`}
/>
<Script
id={"GoogleTagManager"}
strategy="beforeInteractive"
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}`}
/>
<Script
id={"GoogleAnalytics"}
strategy="beforeInteractive"
>
{`
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS}', {
page_path: window.location.pathname,
});
`}
</Script>
<Script
id={"commento.io"}
strategy="beforeInteractive"
defer
src="https://ec2-44-219-132-22.compute-1.amazonaws.com/js/commento.js"
data-auto-init="true"
/>
<Script
id={"kissmetrics-cdn"}
strategy="beforeInteractive"
dangerouslySetInnerHTML={{
__html: `
var _kmq = _kmq || [];
var _kmk = _kmk || '8291a27d7270981e8c87dda4d684803f873c00e1';
function _kms(u){
setTimeout(function(){
var d = document, f = d.getElementsByTagName('script')[0],
s = d.createElement('script');
s.type = 'text/javascript'; s.async = true; s.src = u;
f.parentNode.insertBefore(s, f);
}, 1);
}
_kms('//i.kissmetrics.io/i.js');
_kms('//scripts.kissmetrics.io/' + _kmk + '.2.js');
`,
}}
/>
<ProgressBar
height="4px"
color="#025951"
options={{
showSpinner: false,
}}
shallowRouting
/>
<Navbar />
<Suspense fallback={<LoaderSkeleton />}>
{typeof window !== 'undefined' ? (
<CSRReduxProviders>
{children}
</CSRReduxProviders>
) : (
<SSRReduxProviders>
{children}
</SSRReduxProviders>
)}
</Suspense>
<Footer />
<ToastContainer />
</body>
</html>
)
}
"use client";
import { store, persistor } from './store.js';
import { PersistGate } from 'redux-persist/integration/react';
import { Provider } from "react-redux";
export function CSRReduxProviders({ children }) {
return (
<Provider store={store}>
<PersistGate loading={null} persistor={persistor}>
{children}
</PersistGate>
</Provider>
);
}
export function SSRReduxProviders({ children }) {
return (
<Provider store={store}>
{children}
</Provider>
);
}
All elements inside {children} do not appear inside the page source.
Is there any way to make the whole page visible inside the page source?
I have tried conditionally rendering the {children} based on the typeof window, but it is not working, apart from the navbar and footer nothing else appears on the page source, not even the page heading
Anurag Goswami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.