Skip to content

How to get current route in loader? #9740

Answered by kiliman
jasonkuhrt asked this question in Q&A
Discussion options

You must be logged in to vote

Are you referring to the route ID?

Remix doesn't provide an easy way to access the route configuration directly.

However, if you're using one of the custom adapters, like Express, you can include the routes config from the server build using getLoadContext.

const build = viteDevServer
  ? () => viteDevServer.ssrLoadModule("virtual:remix/server-build")
  : await import("./build/server/index.js");

const remixHandler = createRequestHandler({
  build,
  getLoadContext: async () => {
    const serverBuild = await build();
    return { routes: serverBuild.routes };
  },
});

ServerBuild.routes returns a route object that looks something like this:

{
  root: {
    id: 'root',
    parentId: undef…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@jasonkuhrt
Comment options

@jasonkuhrt
Comment options

@gamliela
Comment options

@jasonkuhrt
Comment options

Answer selected by jasonkuhrt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants