Skip to content
This repository has been archived by the owner on Nov 15, 2022. It is now read-only.

Function to get the matched route outside of React (SSR) #160

Open
judehunter opened this issue May 9, 2021 · 0 comments
Open

Function to get the matched route outside of React (SSR) #160

judehunter opened this issue May 9, 2021 · 0 comments

Comments

@judehunter
Copy link

It looks like this library does not expose a function to get the matched route (only the useRoutes hook). This would be quite useful for server side rendering.

I'll take Next.js as an example. What the framework does is:

  • match the route on request
  • await the resolved page module (it's a Promise because of code-splitting)
  • only then render React to string

So if Next.js was using this library, it would need a way to get the resolved value (usually retrieved from useRoutes), then await that value and only then render the JSX (and pass that awaited value as a prop).

If it's still unclear what I'm proposing here, the routes definition in Next.js when converted to this library could look something like this:

useRoutes({
  '/': () => import('./Index.jsx'),
  '/home': () => import('./Home.jsx')
})

The reason why this solution is needed is because Suspense is not currently supported on the server, and awaiting the page module is the only viable way of lazy loading the page.

I would imagine the API could be:

setPath(path);
const route = getRoute({
  '/': () => import('./Index.jsx'),
  '/home': () => import('./Home.jsx')
});
// then make use of the value
const html = ReactDOMServer.renderToString(<App Page={await route}/>);

Let me know if this makes sense and if it's in the scope of the library.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant