One of the things about Remix is that although it's opinionated, it does have some escape hatches that let you add your own customizations. In this case, it's the routes configuration where you can add your own routes in addition to the default convention.
Since Remix assumes the files in the routes folder use the default convention, you'll need to tell Remix to ignore all files in the routes folder. Finally, call the FlatRoutes function to scan the folder using the new FlatRoutes convention. In the future, you'll be able to specify which convention you want to use directly through the Remix config file.
RemixFlatRoutes changes some of the file naming conventions that we're used to. For example, for pathless layouts, use a single underscore instead of a double underscore prefix. The params prefix remains the same, the dollar sign, but I'll show you how we can change that later. Instead of using folders for routes, the filename includes the entire route, so use the dot instead of slash to separate URL segments. And finally, to handle scenarios like our user edit route, the underscore suffix specifies the segment is not a layout because it has no outlet. I'll demonstrate this later.
Remix FlatRoute supports two configurations, flat files and flat folders. Flat files will typically be used for simpler apps as everything is in the filename and there are no folders. Here is the same example we used before, but using the flat files convention. Notice that there are no folders. The entire URL structure is visible at a glance without having to drill down into separate folders. We still have to identify the index route, in this case, users slash index. Notice how we named the file users dot index. The leading underscore here is simply to sort the index route before the other child routes.
As I mentioned, we are using dots instead of folders to separate the paths. But without folders, how does Remix know what the parent layout is? To determine the parent layout, Remix Flat Routes finds the longest matching prefix, and that determines the parent layout. Here we see that public prefix of public dot about matches the public TSX layout. Same with users. Remember in the previous example, where we didn't want the edit route to nest under user ID? There, we used dots in the file name to deal with it. But how do you deal with that when everything uses dots? Remix Flat Route allows you to use a trailing underscore on a parent route to specify that this is not a layout. As you can see in the edit route, the user ID should not be treated as a parent layout, hence the underscore. This ensures that users that user ID prefix is not matched with that route. And Remix will then look to the next segment for a matching layout, which is the users layout.
Although Flat File simplifies the routing convention considerably, it does so with some drawbacks. There are no folders.
Comments