I'm trying to do some SEO for my static React application. The problem is that react-snap
that I use for pre-rendering only pre-renders the front page of my app, because that opens to user by default and doesn't change until user pushes the button. Here is a sample of my code:
function App() {
const [page, switchPage] = useState("home");
return (
<div className="App">
<div id="logo"></div>
<div>
<div className="btn-group">
<button onClick={() => switchPage("home")}>Home</button>
<button onClick={() => switchPage("news")}>News</button>
<button onClick={() => switchPage("gallery")}>Gallery</button>
<button onClick={() => switchPage("blog")}>Blog</button>
<button onClick={() => switchPage("contact")}>Contact us</button>
</div>
{page == "home" &&
<Home/>
}
{page == "news" &&
<News/>
}
{page == "gallery" &&
<Gallery/>
}
{page == "blog" &&
<Blog/>
}
{page == "contact" &&
<Contact/>
}
</div>
</div>
);
Unfortunately, react-snap pre-renders only the "home" component. I know this because I checked my website with online search engine simulator tools and only h2
elements on the home page are visible to search engines. I have tried to solve this problem also by using React router to set routes for each page, but so far that hasn't helped. How can I pre-render my entire app to make it visible for Google? Thanks
Edit: My index.js
looks like this:
import React from 'react';
import './index.css';
import App from './App';
import { hydrate, render } from 'react-dom';
const rootElement = document.getElementById('root');
if (rootElement.hasChildNodes()) {
hydrate(<App />, rootElement);
} else {
render(<App />, rootElement);
}