Z
Z
Ziggy Pop2018-08-18 23:50:33
JavaScript
Ziggy Pop, 2018-08-18 23:50:33

How to exclude certain paths from processing by React Router?

There is a React SPA whose routes are processed using react-route (and react-router-redux), something like this:

ReactDOM.render(
      <Provider store={store}>
        <ConnectedRouter history={history}>
              <Switch>
                <Route exact path="/" render={() => <Services />} />
                <Route path="/accounts" render={() => <div>Accounts</div>} />
                <Route render={() => <div>404</div>} />
              </Switch>
            </Content>
          </Layout>
        </ConnectedRouter>
    </Provider>,
   document.getElementById("root")
);

As soon as the user visits any of the addresses on the server, the browser starts a service worker that intercepts all the addresses in the address bar and renders certain data accordingly on the client side.
But besides this, there are routes on the server that are processed and returned directly by the backend, such as /api and /api/documentation addresses.
The service worker does not know anything about the existence of these addresses, and simply throws “404” on the render. How to explain to him that nothing needs to be done, but just needs to be transferred to the specified addresses? It would be ideal if you could do something like:
<Route path="/api/*"  render={ document.location.replace(history.location.pathname)} />

But, unfortunately, with such a solution "on the forehead" there is just a cyclic redirect. Knowledgeable people, tell me, please, how can I get around this annoying problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question