stereobooster / react-snap

👻 Zero-configuration framework-agnostic static prerendering for SPAs
MIT License
5.04k stars 391 forks source link

Handling dynamic routes #501

Open alperunal opened 3 years ago

alperunal commented 3 years ago

Using React-snap with React Router (5.1.5)

`

<Route path="page">
    <Page />
</Route>

`

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.

urosran commented 3 years ago

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

BrightonNgema commented 3 years ago

@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.

urosran commented 3 years ago

@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>

peterdu98 commented 3 years ago

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.

huurray commented 3 years ago

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

peterdu98 commented 3 years ago

@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.

ricky11 commented 2 years ago

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 ?