[HELP] 🙏 react-snap not working with react-router

See original GitHub issue

Hi,

I have a vanilla React app created with CRA, and on top of it with react-router. My App.js is essentially as follows:

export default function App() {
  return (
      <Router>
        <Switch>
          <Route exact path='/history'>    // History page
            <HistoryPage
              appConfig={config}
            />
          </Route>
          <Route exact path='/biology'>   // Biology page
            <BiologyPage
              appConfig={config}
            />
          </Route>
          <Route exact path='/ecology'>   // Ecology page
            <EcologyPage
              appConfig={config}
            />
          </Route>
          <Route exact path='/future'>     // Future page
            <FuturePage
              appConfig={config}
            />
          </Route>
          <Route exact path="/" >              // home page
            <HomePage
              appConfig={config}
            />
         </Route>
        </Switch>
    </Router>
  );
}

The structure of the site is very simple. Before I tried to adopt react-snap, going to / would land me at the home / landing page, going to /biology would land me at the “Biology” page, and going to /history would land me at the “History” page, etc.

Now, after I followed the instructions in the README.md, when built my app and test locally at localhost:5000, I would always land at the home page no matter what url I enter into the browser. A proof of that would be:

> curl http://localhost:5000/biology  --head
HTTP/1.1 200 OK
Content-Length: 6719
Content-Disposition: inline; filename="index.html"
Accept-Ranges: bytes
ETag: "25f8b34870c1b197f0e1edf49dd6bda1ac3e646a"
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Date: Sat, 22 Aug 2020 04:21:32 GMT
Connection: keep-alive

I’d expect NOT get the index.html, but get the compiled static build/biology/index.html, which is there.

The static compile indeed worked.

✅  crawled 1 out of 6 (/)
⚠️  warning: 404 page title does not contain "404" string
✅  crawled 2 out of 6 (/404.html)
✅  crawled 3 out of 6 (/history)
✅  crawled 4 out of 6 (/ecology)
✅  crawled 5 out of 6 (/biology)
✅  crawled 6 out of 6 (/future)

I’m on pretty much the latest of everything, and nothing special besides that.

"react": "16.13.1",
"react-dom": "16.13.1",
"react-router-dom": "5.2.0",
"react-scripts": "^3.4.3",
"react-snap": "^1.23.0",

I spent almost 4 hours on this after a bunch Googling and Stackoverflowing, but still stuck 😢.

The current version of the site is live at https://www.hope4cheetahs.org/. If you go to the site you’ll see the structure, it’s very straightforward.

Any help/pointer would be much appreciated. I really want to use react-snap. Thanks a ton in advance!!! 🙏🙏🙏

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
BlazeIsClonecommented, Nov 22, 2022

I’m also having the same issue no matter which page I’m in react-snap will hydrate from the index page instead of the requested page. React snap builds the index files correctly as well the issue happens with the hydration. I’m using react-router v6.

Hi @BlazeIsClone. Did you resolve your issue?

No actually ended up porting the application to NextJS. Apart from having to redo the routing and the data fetching layer. NextJS also provides pre-rendering out of the box aswell.

0reactions
komxvlcommented, Nov 22, 2022

Thanks for your time, but NextJS doesn’t work for me, i need to convert to ssg my react app((

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-snap and react-router together make a problem
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...
Read more >
Handling dynamic routes · Issue #501 · stereobooster/react-snap
Using React-snap with React Router (5.1.5) After the build, it creates page.html so I page without params works (http://url/page). However.
Read more >
react-snap - Best of JS
Works out-of-the-box with create-react-app - no code-changes required. Uses a real browser behind the scenes, so there are no issues with unsupported HTML5 ......
Read more >
cra-template-must-have-libraries - npm
A starter CRA professional React v17 project with must-have ReactJS libraries including TypeScript, SCSS, Redux, Toolkit, Material-UI, ...
Read more >
Setting up an opinionated starter CRA professional React 17 ...
After working & reviewing projects and libraries that help get the job done with React, it's hard to stay neutral and not to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found