1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-14 22:45:40 +02:00
Files
misskey/packages/frontend/src/router.ts
かっこかり d4a5048aae fix(frontend): routerがmatchAllに入った際に一度 location.href による遷移を試みる挙動に関する修正 (#17281)
* fix(frontend): follow-up of #13509

* fix: fix use of inappropriate method

* Update CHANGELOG.md [ci skip]
2026-04-07 20:35:06 +09:00

55 lines
1.5 KiB
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { inject } from 'vue';
import { page } from '@/router.definition.js';
import { $i } from '@/i.js';
import { Nirax } from '@/lib/nirax.js';
import { ROUTE_DEF } from '@/router.definition.js';
import { analytics } from '@/analytics.js';
import { DI } from '@/di.js';
export type Router = Nirax<typeof ROUTE_DEF>;
export function createRouter(fullPath: string): Router {
return new Nirax(ROUTE_DEF, fullPath, !!$i, page(() => import('@/pages/not-found.vue')));
}
export const mainRouter = createRouter(window.location.pathname + window.location.search + window.location.hash);
window.addEventListener('popstate', (event) => {
mainRouter.replaceByPath(window.location.pathname + window.location.search + window.location.hash);
});
mainRouter.addListener('push', ctx => {
window.history.pushState({ }, '', ctx.fullPath);
});
mainRouter.addListener('replace', ctx => {
window.history.replaceState({ }, '', ctx.fullPath);
});
mainRouter.addListener('forceReplace', ctx => {
window.location.replace(ctx.fullPath);
});
mainRouter.addListener('forcePush', ctx => {
window.location.href = ctx.fullPath;
});
mainRouter.addListener('change', ctx => {
if (_DEV_) console.log('mainRouter: change', ctx.fullPath);
analytics.page({
path: ctx.fullPath,
title: ctx.fullPath,
});
});
mainRouter.init();
export function useRouter(): Router {
return inject(DI.router, null) ?? mainRouter;
}