Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-snap and react-router together make a problem

I need to start a react application and I need pre-rendering and routing, so I installed react-snap and react-router. (The react-router to do the routing and react-snap for pre-rendering obviously).

Everything looks fine in the local with 'npm start' but as I make a production build and serve it, the routing links make the page redirect to a new url, so all i see always, is the homepage.

My render looks like this:

 render() {
    return (
      <Router>
        <React.Fragment>
          <MainNav/>
          <Route exact path="/" component={Home}/>
          <Route path="/greeting/:name/:surname" render={(props) => <Greetings text="Hello, " {...props} />} />
          <Route path="/About" component={About}/>
        </React.Fragment>
      </Router>
    );
  }

and this is my index.js as suggested by react-snap

import React from 'react';
import { hydrate, render } from "react-dom";
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

const rootElement = document.getElementById("root");
if (rootElement.hasChildNodes()) {
  hydrate(<App />, rootElement);
} else {
  render(<App />, rootElement);
}

serviceWorker.unregister();

any ideas?

like image 859
Amir Shahbabaie Avatar asked Nov 11 '18 13:11

Amir Shahbabaie


People also ask

How to add react router in react app?

React Router is the most popular solution. To add React Router in your application, run this in the terminal from the root directory of the application: Note: This tutorial uses React Router v6.

What is react routing and how to use it?

Many consider React the best overall JavaScript framework for building web applications. One of the basics that you need to master is React routing. React Router allows your web application to be organized into pages on different URLs, as you are used to with any other pages on the internet.

How to make react bootstrap navbar work with react router?

To make React Bootstrap Navbar and React Router work together, we can add the as prop to the Nav.Link component. to render Nav.Libk as a React Router NavLink by setting the as prop to NavLink. The to prop takes a URL path string just like the React Router to prop.

What are nested routes in react router?

To recap, nested routes allow you to, at the route level, have a parent component control the rendering of a child component. Twitter's /messages route is the perfect example of this. With React Router, you have two options for creating nested routes.


2 Answers

I've been able to fix a similar issue by adding the following snippet to my package.json

"reactSnap": {
  "fixWebpackChunksIssue": false
}

Check the following link for more information and other options

https://github.com/stereobooster/react-snap/issues/264

like image 160
olecve Avatar answered Oct 17 '22 12:10

olecve


I've been battling this for a couple weeks now. The main thing I've noticed is that if I call registerServiceWorker() in index.js, the app will function normally after building. If I comment out this line the app only routes to '/' no matter what.

It appears you are unregistering the service worker in your index.js, this might cause an issue.

A catch 22 I've run into and haven't been able to solve is that if I use the registerServiceWorker() call in index.js, react-snap doesn't properly prerender all the routes, and if I do comment out the registerServiceWorker() line, react-snap prerenders all routes perfectly, but the app doesnt navigate.

It's also worth noting that my project was created using 'create-react-app' and hasnt been ejected.

like image 43
JoelMercurio Avatar answered Oct 17 '22 10:10

JoelMercurio