kiliman / remix-flat-routes

Remix package to define routes using the flat-routes convention
MIT License
695 stars 24 forks source link

How to define a route if extension of the file is variable? #129

Closed lilouartz closed 3 months ago

lilouartz commented 3 months ago

Both of these should be valid URLs:

I tried:

supplement-facts.$format/_route.tsx
supplement-facts[.]$format/_route.tsx
supplement-facts[.$format]/_route.tsx

None of the above work.

lilouartz commented 3 months ago

Not a clue. Tried everything. Would be really handy to have this in the documentation.

kiliman commented 3 months ago

Unfortunately, a dynamic param $format must be the entire URL segment, not just a part of it. This is a limitation of React Router.

So if you need to be able to handle any type of file extension, you'll need to do something like this:

// routes/supplements.$product.images.$filename
export async function loader({ params }: LoaderFunctionArgs) {
  const { filename } = params
  if (!/^supplement-facts\.(webp|avif)$/.test(filename)) {
    throw new Response('Not Found', { status: 404 })
  }
  // return image file as a response
}
lilouartz commented 3 months ago

ah this explains it! maybe worth adding a callout to the docs because I've spent like two hours experimenting with this haha

lilouartz commented 3 months ago

Thank you for the response