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

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

@judehunter

Description

@judehunter

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions