fix(login): keep the form when the showcase fails to load

The showcase chunk loaded under Suspense with no error boundary, so a
failed import blanked the whole sign-in page. LoadBoundary takes an
optional fallback, and the login page gives it the same empty panel it
shows while the chunk loads; the form beside it keeps working.
This commit is contained in:
Zakk committed 2026-09-28 03:46:04 +10:00
1 parent 53822f25b3
commit d4e54f3f6c
3 files changed
+40 -6

No files matched your search

+26
View File
@@ -135,6 +135,32 @@ for (const chunk of ['Policies', 'AreaChart', 'Sparkline', 'Donut']) {
});
}
test('a rejected showcase import leaves its panel empty and the sign-in form working', async ({browser}) => {
const context = await browser.newContext({serviceWorkers: 'block', viewport: {width: 1280, height: 800}});
const page = await context.newPage();
const uncaught: string[] = [];
page.on('pageerror', error => uncaught.push(error.message));
await page.addInitScript(() => {
localStorage.setItem('doona-profiles', JSON.stringify([{id: 'demo', name: 'Demo', api: 'mock', token: ''}]));
localStorage.setItem('doona-profile', 'demo');
localStorage.setItem('doona-lang', 'en');
});
const rejected = page.waitForRequest(/\/LoginShowcase-[^/]+\.js$/);
await page.route('**/assets/LoginShowcase-*.js', route => route.abort());
try {
await page.goto('/#/activity');
await rejected;
const login = page.locator('.rp-login-page');
await expect(login.locator('.rp-login-showcase[aria-hidden="true"]')).toBeVisible();
await expect(login.locator('.rp-alert')).toHaveCount(0);
await Promise.all([page.waitForEvent('load'), login.getByRole('button', {name: 'Sign in', exact: true}).click()]);
await expect(page.locator('.rp-nav[href="#/activity"]')).toHaveAttribute('aria-current', 'page');
expect(uncaught).toEqual([]);
} finally {
await context.close();
}
});
test('the idle warm-up loads the search dialog before any interaction', async ({page}) => {
const requested = page.waitForRequest(/\/SearchDialog-[^/]+\.js$/);
await page.goto('/#/activity');
+9 -4
View File
@@ -3,13 +3,16 @@ import {useT, type Lang} from '../i18n';
import {useLogin} from './useLogin';
import {Button, ErrorMessage, InlineAlert, Link, Loading, TextField} from '../ui/ui';
import {useMediaQuery} from '../ui/hooks';
import {LoadBoundary} from '../ui/LoadBoundary';
import LinkOut from '../ui/icons/LinkOut';
import logo from '../logo.svg';
import {href} from './route';
import {LanguageMenu, SchemeToggle} from './AppearanceControls';
// The panel beside the form loads only where it is shown, so the sign-in chunk stays the same size on a phone.
// The panel beside the form loads only where it is shown, so the sign-in chunk stays the same size on a phone. Until it
// arrives, or if it never does, the panel stays empty and the form works as before.
const LoginShowcase = lazy(() => import('./LoginShowcase'));
const emptyShowcase = <div className="rp-login-showcase" aria-hidden="true" />;
type LoginProps = {
profileId: string;
@@ -149,9 +152,11 @@ export function Login({profileId, api, backend, rejected, lang, pickLang, dark,
</main>
</div>
{wide && (
<Suspense fallback={<div className="rp-login-showcase" aria-hidden="true" />}>
<LoginShowcase />
</Suspense>
<LoadBoundary fallback={emptyShowcase}>
<Suspense fallback={emptyShowcase}>
<LoginShowcase />
</Suspense>
</LoadBoundary>
)}
</div>
);
+5 -2
View File
@@ -1,7 +1,9 @@
import {Component, type ReactNode} from 'react';
import {ErrorMessage} from './Feedback';
export class LoadBoundary extends Component<{children: ReactNode}, {error: Error | null}> {
// A lazy part that fails to load reports it with a retry. `fallback` stands in silently instead, for a part the page
// works without.
export class LoadBoundary extends Component<{children: ReactNode; fallback?: ReactNode}, {error: Error | null}> {
override state: {error: Error | null} = {error: null};
static getDerivedStateFromError(error: unknown) {
@@ -10,6 +12,7 @@ export class LoadBoundary extends Component<{children: ReactNode}, {error: Error
override render() {
// Reload clears both React.lazy's rejected promise and the browser's failed module entry.
return this.state.error ? <ErrorMessage error={this.state.error} onRetry={() => location.reload()} /> : this.props.children;
if (!this.state.error) return this.props.children;
return this.props.fallback ?? <ErrorMessage error={this.state.error} onRetry={() => location.reload()} />;
}
}