react-snap icon indicating copy to clipboard operation
react-snap copied to clipboard

Handling dynamic routes

Open alperunal opened this issue 4 years ago • 7 comments

Using React-snap with React Router (5.1.5)

<Switch> <Route path="page/:id"> <Page /> </Route> <Route path="page"> <Page /> </Route> </Switch>

After the build, it creates page.html so I page without params works (http://url/page). However, if I try something like this http://url/page/3, I get 404 page.

How can I make the dynamic routes work?

Edit: Normally using HashRouter instead of BrowserRouter solves the problem without SSR.

alperunal avatar Oct 02 '20 09:10 alperunal

I have a similar issue in that react-snap will only crawl the homepage at route "/" and none of the links that are leading from it to other pages such as /:pageid

urosran avatar Nov 30 '20 17:11 urosran

@urosran I resolved it by creating hidden <Link/> tags with all the dynamic links as static. Unfortunately its gonna be quite a process if you have a lot of links.

BrightonNgema avatar Nov 30 '20 19:11 BrightonNgema

@BrightonNgema thanks for the answer! As I have an album component and each image is a route to a store (in an e-mall) the following worked: <Link to={'/' + yourUrl} style={{display: 'none'}}>linkToStore</Link>

urosran avatar Dec 02 '20 14:12 urosran

Thanks all, I resolved it by adding hidden <Link to={path} />. For those who are building a static Github Pages, one drawback of this is that you will need to rebuild every time when you have a new post.

peterdu98 avatar Jan 10 '21 03:01 peterdu98

@urosran @peterdu98 i can't understand it. could you show me any example?

huurray avatar Feb 16 '21 08:02 huurray

@huurray In my case, I add <Link to={path} /> in the render() of the App class:

render() {
    <React.Fragment>
           // Here, if you have more than 3 links, I would suggest using `state`
           // and `componentDidMount` to return them dynamically.
          <Link to="/path_1" />
          <Link to="/path_2" />
          
           // Other blocks of code below
    </React.Fragment>
}

Note: Since <Link /> is used, it won't show in your UI but will render <a href="/path"></a> in your browser. You can double-check this via the developer tool in your browser.

peterdu98 avatar Feb 16 '21 11:02 peterdu98

How would this work if we dont know the dynamic link path?

for example /download/slug*
where slug is a 5 digit random sequence of letters

I'm not sure how react-snap will create a index.html in a /download/??? folder ?

ricky11 avatar Aug 09 '21 18:08 ricky11