Comments (5)
Redirects in Remix and React Router are relative to the Route Hierarchy by default: https://reactrouter.com/en/main/start/overview#relative-links
Your route files create a route tree of:
> npx remix routes
<Routes>
<Route file="root.tsx">
<Route index file="routes/_index.tsx" />
<Route path="a/b/c" file="routes/a.b.c.tsx" />
<Route path="a/b/e" file="routes/a.b.e.tsx" />
</Route>
</Routes>
So when you redirect('../e')
from the action in the route for /a/b/c
you are going "up one route" to the root, then going to e
which lands you at /e
. If you added a parent route file routes/a.b.tsx
then you'd have the proper Route hierarchy for this approach.
Or, if you want URL-segment relative links, in the React component/shooks there's a relative="path"
option for this (https://reactrouter.com/en/main/components/link#relative) but that doesn't exit for redirect which is just a wrapper around new Response(null, { status: 3xx, ... })
.
For redirects, you can do it with the URL constructor:
export function loader({ request }: LoaderFunctionArgs) {
let currentUrl = request.url.endsWith("/") ? request.url : `${request.url}/`;
let redirectUrl = new URL("../e", currentUrl).pathname;
return redirect(redirectUrl);
}
from remix.
Is there any use case where redirect('../e')
pointing to a path depending on internals is useful? Now I understand the historical reason, but I'm curious if it could be a feature request, trading off how many people are happy as it is vs changing to relative path works out of the box.
from remix.
I'm not sure what you mean by "depending on internals"?
I don't think we'll extend relative="path"
to redirect
because in hindsight after we shipped that API we realized how easy it would be to do in user-land via something like the above (let to = new URL("../e", window.origin + useLocation().pathname).pathname
and <Link to={to} />
)
from remix.
"depending on internals" — depending on how the route files are structured, not on the path string itself. As you mentioned I might decide to add routes/a.b.tsx
for a different reason, and now the relative links that depended on the current behavior are now broken. It's a brittle system, and people are likely using it (if ever) without knowing it's going to break that way.
So that's why I asked "is anyone happy with how relative paths resolve today?"
Backward compatibility aside, making the public user-facing API more intuitive and unified between server and client feels like a happy path (pun intended) for everyone.
from remix.
I wouldn't consider those internals - since you define the route hierarchy.
Your described issue goes both ways - relative paths (as well as absolute paths) are always brittle if you alter your URLs.
The relative routing is currently unified - all routes are relative to the route hierarchy which is a core feature of v6 and nested routing (https://reactrouter.com/en/main/start/overview#relative-links). It's only the opt-in path=relative
behavior that is not unified in redirect
, and will likely remain as such for the reason explained above.
from remix.
Related Issues (20)
- Uncaught SyntaxError: The requested module '/app/root.tsx? does not provide an export named 'Layout' (at root.tsx) HOT 8
- [vite] Files shared between Remix and Express are loaded twice HOT 2
- `useRouteLoaderData("root")` returns `undefined` in root's `ErrorBoundary`/`Layout` when non-existent page is loaded right away. HOT 10
- `TypeError: Can't modify immutable headers.` in handleDataRequestRR (Cloudflare Pages) HOT 2
- When use custom routes, pages use `root.tsx` Layout only and skip own by prefix pattern HOT 1
- Vulnerability detected in library Braces
- [Vite] Remix probably serves malformed response HOT 4
- Node Fetch: Cannot cancel a stream that already has a reader HOT 3
- Random preloads added for images HOT 1
- `assetsDir` not properly resolved when using `basename` when using Wrangler HOT 1
- Splat Routes that would have been prioritized can be overridden by Dynamic Segments. HOT 7
- remix hydration error when using gtag
- [Vite] [Cloudflare] dev server start failed with http_proxy environment variable
- Onclick handler not working in remix and problem installing tailwindcss HOT 5
- MetaFunction data type not inferred from clientLoader HOT 3
- Application Error: failed to execute insertBefore on Node HOT 19
- Vitest looks for package.json in every workspace when using vitePlugin HOT 2
- [BUG]: cloudflareDevProxyVitePlugin: Issue in DEV with Remix/Vite/Drizzle/Cloudflare Pages/Supabase/Postgres HOT 2
- Can't import node_module SVG HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remix.