Skip to content

Commit

Permalink
+ isPathsFirstDirectorySame() to share with `plugins/routerGuard.cl…
Browse files Browse the repository at this point in the history
…ient.ts` & `app/router.options.ts` @ utils/router.ts

@ fe
  • Loading branch information
n0099 committed Jun 20, 2024
1 parent a7fc3b0 commit d5fcc95
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 7 deletions.
3 changes: 1 addition & 2 deletions fe/src/app/router.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export default {
assertRouteNameIsStr(to.name);
assertRouteNameIsStr(from.name);

// scroll to top when the prefix of route name changed
if (to.name.split('/')[0] !== from.name.split('/')[0])
if (isPathsFirstDirectorySame(to.path, from.path))
return { top: 0 };
}

Expand Down
6 changes: 1 addition & 5 deletions fe/src/plugins/routerGuard.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ export default defineNuxtPlugin(() => {

const router = useRouter();
router.beforeEach((to, from) => {
const getPathFirstDirectory = (path: string) => {
const secondSlashIndex = path.indexOf('/', 1);
return secondSlashIndex === -1 ? path : path.substring(0, secondSlashIndex);
};
if (getPathFirstDirectory(to.path) === getPathFirstDirectory(from.path))
if (isPathsFirstDirectorySame(to.path, from.path))
return;
start();
});
Expand Down
2 changes: 2 additions & 0 deletions fe/src/utils/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const getNextCursorRoute = (route: RouteLocationNormalized, nextCursor?:
return { query, name, params: { ...params, cursor: nextCursor } };
};

export const isPathsFirstDirectorySame = (a: string, b: string) =>
a.split('/')[1] === b.split('/')[1];
const exports = {
routes: () => [
{
Expand Down

0 comments on commit d5fcc95

Please sign in to comment.