Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React routing between static and dynamic pages

So normally our S3 hosted Web app index.html looks like:

 <div class=app></div>

That also references the JS bundle & which React renders to and that's fine. But some elements of the site are statically generated for speed & SEO and look like:

 <!-- dynamically generated content -->
 <div class=app></div>
 <!-- statically generated content -->
 <h1>Title</h1>
 <p>Information, blah blah blah</p>
 <p>Lots of content</p>

Conventional wisdom might suggest having the static stuff in a ReactJS component and then reactDOM.renderToString(), but I don't want to do it that way since it's rather complex to do it that way since the static component pulls from many several APIs.

So I am struggling to find documentation for what I want. I want to be able to say for certain URLs, that a full page load is necessary (window.location). Similarly, when navigating away from a static page with content, a full page load is needed or the content need to zapped back to <div class=app>.

How do I achieve this with a react router?

like image 235
hendry Avatar asked Sep 23 '16 04:09

hendry


People also ask

Can you use both static and dynamic routing?

Both static and dynamic routing can be used in one network.

Is React Router static or dynamic?

Frameworks like Angular, Ember and React Router library (in the past) supported static routing. But recently, React Router introduced dynamic routing to address some of the core limitations with static routing.

How do you navigate between two pages in React?

To switch between pages in React, we can use the React Router package. First, we create the Foo and Bar page components. Next, we add the Route s to map the URLs to the components to render. Then we add the NavLink s to add links that lets us set the class name of the active link.


1 Answers

this is something I would go with off the top of my head. I apologize if doesn't suit your needs/purpose. I think I understand what you are going for. I might have this wrong, but I am thinking that your static pages do not have react-route on them. Literal static pages, outside the react environment.

  1. I'd create a whitelist for these static pages.

    const whitelist = ['aboutus', 'help']

  2. then in my routes, I'd have the fallthru, check for the path.

    //psuedo code
    {
        path: '*',
        onEnter: () => {
          if(whitelist.includes(path)) {
              window.location = /path
          }
        },
        component: Four0Four,
    },
    

or you could just prepend the static pages like so:

  1. perhaps a url like "/static?aboutus.html"

    //psuedo code
    {
        path: 'static',
        onEnter: () => {
          if(whitelist.includes(path)) {
              window.location = `/static/${param}`
          }
        },
        component: Four0Four,
    },
    

When you come back to the "react-route react" app, I wouldn't think you'd have to do anything as the react-router will pick up from the url you move back to.

  1. You could also use the "Listener" on the location event.

    browserHistory.listen(location => {
        // do your checking for the static pages here
    });
    

I hope I am not too far off base on my interpretation, if I am. Let me know and I'll delete my response.

like image 109
james emanon Avatar answered Oct 21 '22 15:10

james emanon