T
TanStack•10mo ago
metropolitan-bronze

Beginner: wrong beforeload being trigger ?

Hey guys, so I have a file tree as follow:
|-(teams)
| |-teams.tsx
| |-teams_.$teamId.tsx
| |-teams_.$teamId_.settings.tsx
| |-teams_.$teamId_.invitations_.$token.accept.tsx
|-(teams)
| |-teams.tsx
| |-teams_.$teamId.tsx
| |-teams_.$teamId_.settings.tsx
| |-teams_.$teamId_.invitations_.$token.accept.tsx
and whenever I access the page teams_.$teamId_.invitations_.$token.accept.tsx the beforeload of teams_.$teamId.tsx is being called. Is this normal ? if so how could I truned this off. I tried to look in the docs for this but I couldn't find anything. Thanks in advanced 🙌
16 Replies
optimistic-gold
optimistic-gold•10mo ago
all beforeLoad functions of all routes that are in the current hierachy are executed upon each navigation you cannot turn this off what does it do in your case? there are several options depending on the use case
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
Thanks for getting in touch. I am just using the beforeLoad to check if my user is allowed to view page. But because the beforeLoad of the/teams/:id page will get called first, the beforeLoad of the /teams/:id/invitations/:token page will never be called. /teams/:id
export const Route = createFileRoute("/(teams)/teams_/$teamId")({
beforeLoad: async ({ context }) => {
permissions.team.view(context.user);
},
});
export const Route = createFileRoute("/(teams)/teams_/$teamId")({
beforeLoad: async ({ context }) => {
permissions.team.view(context.user);
},
});
/teams/:id/invitations/:token
export const Route = createFileRoute(
"/(teams)/teams_/$teamId_/invitations_/$token/accept",
)({
beforeLoad: ({ context, params }) => {
permissions.team.acceptInvite(context.user, Route.fullPath);
},
});
export const Route = createFileRoute(
"/(teams)/teams_/$teamId_/invitations_/$token/accept",
)({
beforeLoad: ({ context, params }) => {
permissions.team.acceptInvite(context.user, Route.fullPath);
},
});
optimistic-gold
optimistic-gold•10mo ago
sounds like your accept route should not be under the permission check then? so move it out of there
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
so you want me to move it out of the /(teams)/teams_/$teamId directory ?
optimistic-gold
optimistic-gold•10mo ago
its parent must be something that does not check the permission there are other workarounds
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
ok I see, but in this case all of the teams routes got a auth check.
optimistic-gold
optimistic-gold•10mo ago
you could also disable the auth check if the accept is currently matched
optimistic-gold
optimistic-gold•10mo ago
optimistic-gold
optimistic-gold•10mo ago
export const Route = createFileRoute('/foo')({
beforeLoad: (opts) => {
if (opts.matches.find((m) => m.routeId === '/foo/hello')) {
return;
} else {
// check auth
}
},
});
export const Route = createFileRoute('/foo')({
beforeLoad: (opts) => {
if (opts.matches.find((m) => m.routeId === '/foo/hello')) {
return;
} else {
// check auth
}
},
});
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
Awesome, that makes a lot of sense Thanks for explaining @Manuel Schiller 🙌
optimistic-gold
optimistic-gold•10mo ago
there are more options for sure you could also put the auth check into a pathless route
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
Yes Indeed, I will play around with the code to see which one fit better
optimistic-gold
optimistic-gold•10mo ago
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
very handy thanks
optimistic-gold
optimistic-gold•10mo ago
IMO the pathless route is cleaner but both work
metropolitan-bronze
metropolitan-bronzeOP•10mo ago
yes I agree

Did you find this page helpful?