-
(remix 2.8.1 using Vite) Problem exampleFor remix project I have a modal that works with on several routes. Below a simplified structure. Let's say we have one page with the modal,
This works great however I need to be able to open this from a second page under a different url, under a different layout. I can not navigate to the other page as I need the state when the modal is closed. Currently the best fix I found was copying the routes.
And then the route.tsx for page for pageB.modal.stepA` might look like this: export {
default,
loader,
action,
} from '~/routes/pageA.modal.stepA' This is how my project works for now. Does anyone have an idea on how to handle this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
The nice thing about Remix is that you control your route configuration. By default, Remix provides a file-based convention "out of the box." However, you can replace or augment existing routes. You do this via the https://remix.run/docs/en/main/file-conventions/vite-config#routes Here, I'm bypassing export default defineConfig({
plugins: [
remix({
routes: () => {
const commonPage = "pageA";
const newPages = ["pageB", "pageC"];
const steps = ["stepA", "stepB", "stepC"];
const routes: Record<string, ConfigRoute> = {};
newPages.forEach((newPage) => {
steps.forEach((step) => {
const routeId = `${newPage}.modal.${step}`;
routes[routeId] = {
id: routeId,
parentId: "root",
path: `${newPage}/modal/${step}`,
file: `routes/${commonPage}.modal.${step}.tsx`, // use the same file for all new pages
};
});
});
// new "virtual" routes will be merged with existing routes
return routes;
},
}),
tsconfigPaths(),
],
}); You can verify your new routes using <Routes>
<Route file="root.tsx">
<Route path="pageA/modal/stepA" file="routes/pageA.modal.stepA.tsx" />
<Route path="pageA/modal/stepB" file="routes/pageA.modal.stepB.tsx" />
<Route path="pageA/modal/stepC" file="routes/pageA.modal.stepC.tsx" />
<Route index file="routes/_index.tsx" />
<Route path="pageB/modal/stepA" file="routes/pageA.modal.stepA.tsx" />
<Route path="pageB/modal/stepB" file="routes/pageA.modal.stepB.tsx" />
<Route path="pageB/modal/stepC" file="routes/pageA.modal.stepC.tsx" />
<Route path="pageC/modal/stepA" file="routes/pageA.modal.stepA.tsx" />
<Route path="pageC/modal/stepB" file="routes/pageA.modal.stepB.tsx" />
<Route path="pageC/modal/stepC" file="routes/pageA.modal.stepC.tsx" />
</Route>
</Routes> NOTE: I didn't consider nested routes (setting parentId). I don't know exactly how your routes are structured, so I didn't want to make assumptions. |
Beta Was this translation helpful? Give feedback.
-
I ended up solving it using the following plugin in Some config at the top of the file: const routesToReplace = 'routes/pageA/modal'
const routesToReplaceWith = [
'routes/pageB/modal',
'routes/pageC/modal',
] The remix plugin: remix({
ignoredRouteFiles: ['**/*'], // Make sure the default router is not used
routes: async () => {
// Call the default flat route generator
const routes = flatRoutes('app', [], 'routes')
// Duplicate the route to all the new routes
for (const routeKey of Object.keys(routes)) {
if (
routeKey.startsWith(routesToReplace) &&
routeKey !== routesToReplace
) {
for (const routeKeyToReplace of routesToReplaceWith) {
// Create a copy of the route
const route = routes[routeKey]
const newRouteKey = routeKey.replace(
routesToReplace,
routeKeyToReplace,
)
const newRoute: ConfigRoute = {
file: route.file,
id: route.id.replace(routesToReplace, routeKeyToReplace),
path: route.path,
parentId: route.parentId?.replace(
routesToReplace,
routeKeyToReplace,
),
}
if (route.index) {
newRoute.index = true
}
// Add the new route to the routes object
routes[newRouteKey] = newRoute
}
}
}
// Return the new routes object
return routes
},
}) |
Beta Was this translation helpful? Give feedback.
I ended up solving it using the following plugin in
vite.config.ts
:Some config at the top of the file:
The remix plugin: