mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-10-31 00:12:39 +00:00
perf: stop navigating twice after signing in from the home page
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import { beforeEach, describe, expect, it } from 'vitest';
|
||||
import { saveRedirectAfterLogin } from '../auth-store';
|
||||
|
||||
// Where login sends the user back to. The app's home is where a login lands
|
||||
// anyway; saving it made the login page navigate a second time, to /en/en
|
||||
// (the locale-aware router prefixes the locale again), racing the first.
|
||||
|
||||
describe('saveRedirectAfterLogin', () => {
|
||||
beforeEach(() => sessionStorage.clear());
|
||||
|
||||
it('saves nothing on the home page, and drops an older target', () => {
|
||||
sessionStorage.setItem('redirect_after_login', '/en/settings');
|
||||
window.history.pushState({}, '', '/en');
|
||||
saveRedirectAfterLogin();
|
||||
expect(sessionStorage.getItem('redirect_after_login')).toBeNull();
|
||||
});
|
||||
|
||||
it('keeps a deep link, query and hash included', () => {
|
||||
window.history.pushState({}, '', '/en/calendar?view=day#e1');
|
||||
saveRedirectAfterLogin();
|
||||
expect(sessionStorage.getItem('redirect_after_login')).toBe('/en/calendar?view=day#e1');
|
||||
});
|
||||
|
||||
it('keeps the home page with a query, which carries a deep link', () => {
|
||||
window.history.pushState({}, '', '/en?email=abc');
|
||||
saveRedirectAfterLogin();
|
||||
expect(sessionStorage.getItem('redirect_after_login')).toBe('/en?email=abc');
|
||||
});
|
||||
});
|
||||
@@ -856,6 +856,15 @@ export function saveRedirectAfterLogin(): void {
|
||||
const loginPath = getLocaleLoginPath();
|
||||
const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;
|
||||
|
||||
// The app's home is where a login lands anyway. Saving it only made the
|
||||
// login page navigate a second time, through the locale-aware router,
|
||||
// which prefixes the locale again (/en/en) and races the first.
|
||||
const home = `${getPathPrefix()}/${getLocaleFromPath()}`;
|
||||
if (currentPath === home || currentPath === `${home}/`) {
|
||||
sessionStorage.removeItem('redirect_after_login');
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentPath !== loginPath) {
|
||||
sessionStorage.setItem('redirect_after_login', currentPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user