Skip to content

Commit

Permalink
chore(console): use react-safe-lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
gao-sun committed Jul 26, 2024
1 parent 1c80399 commit 87a197c
Show file tree
Hide file tree
Showing 23 changed files with 42 additions and 87 deletions.
1 change: 1 addition & 0 deletions packages/console/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
"react-modal": "^3.15.1",
"react-paginate": "^8.1.3",
"react-router-dom": "^6.25.1",
"react-safe-lazy": "^0.1.0",
"react-syntax-highlighter": "^15.5.0",
"react-timer-hook": "^3.0.5",
"recharts": "^2.1.13",
Expand Down
2 changes: 1 addition & 1 deletion packages/console/src/containers/ConsoleContent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Suspense } from 'react';
import { useOutletContext, useRoutes } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { isDevFeaturesEnabled } from '@/consts/env';
import OverlayScrollbar from '@/ds-components/OverlayScrollbar';
import { Daisy } from '@/ds-components/Spinner';
import Tag from '@/ds-components/Tag';
import { useConsoleRoutes } from '@/hooks/use-console-routes';
import { usePlausiblePageview } from '@/hooks/use-plausible-pageview';
import safeLazy from '@/utils/lazy';

import type { AppContentOutletContext } from '../AppContent/types';

Expand Down
8 changes: 5 additions & 3 deletions packages/console/src/containers/ConsoleRoutes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ossConsolePath } from '@logto/schemas';
import { Suspense } from 'react';
import { Navigate, Outlet, Route, Routes } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';
import { SWRConfig } from 'swr';

import AppLoading from '@/components/AppLoading';
import { isCloud } from '@/consts/env';
import { isCloud, isDevFeaturesEnabled } from '@/consts/env';
import AppBoundary from '@/containers/AppBoundary';
import AppContent, { RedirectToFirstItem } from '@/containers/AppContent';
import ConsoleContent from '@/containers/ConsoleContent';
Expand All @@ -14,7 +15,6 @@ import { GlobalRoute } from '@/contexts/TenantsProvider';
import useSwrOptions from '@/hooks/use-swr-options';
import Callback from '@/pages/Callback';
import CheckoutSuccessCallback from '@/pages/CheckoutSuccessCallback';
import safeLazy from '@/utils/lazy';
import { dropLeadingSlash } from '@/utils/url';

import { __Internal__ImportError } from './internal';
Expand Down Expand Up @@ -47,7 +47,9 @@ export function ConsoleRoutes() {
<Route path="/:tenantId" element={<Layout />}>
<Route path="callback" element={<Callback />} />
<Route path="welcome" element={<Welcome />} />
<Route path="__internal__/import-error" element={<__Internal__ImportError />} />
{isDevFeaturesEnabled && (
<Route path="__internal__/import-error" element={<__Internal__ImportError />} />
)}
<Route element={<ProtectedRoutes />}>
<Route path={dropLeadingSlash(GlobalRoute.Profile) + '/*'} element={<Profile />} />
<Route element={<TenantAccess />}>
Expand Down
6 changes: 5 additions & 1 deletion packages/console/src/containers/ConsoleRoutes/internal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import safeLazy from '@/utils/lazy';
import { safeLazy } from 'react-safe-lazy';

/**
* An internal module that is used to test the lazy loading failure in the console. Normally, this
* module should not involve any production code.
*/
export const __Internal__ImportError = safeLazy(async () => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const module = await import(
Expand Down
2 changes: 1 addition & 1 deletion packages/console/src/hooks/use-console-routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { condArray } from '@silverhand/essentials';
import { useMemo } from 'react';
import { type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { isCloud } from '@/consts/env';
import NotFound from '@/pages/NotFound';
import safeLazy from '@/utils/lazy';

import { apiResources } from './routes/api-resources';
import { applications } from './routes/applications';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { ApiResourceDetailsTabs } from '@/consts';
import safeLazy from '@/utils/lazy';

const ApiResources = safeLazy(async () => import('@/pages/ApiResources'));
const ApiResourceDetails = safeLazy(async () => import('@/pages/ApiResourceDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { ApplicationDetailsTabs } from '@/consts';
import safeLazy from '@/utils/lazy';

const Applications = safeLazy(async () => import('@/pages/Applications'));
const ApplicationDetails = safeLazy(async () => import('@/pages/ApplicationDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type RouteObject } from 'react-router-dom';

import safeLazy from '@/utils/lazy';
import { safeLazy } from 'react-safe-lazy';

const AuditLogs = safeLazy(async () => import('@/pages/AuditLogs'));
const AuditLogDetails = safeLazy(async () => import('@/pages/AuditLogDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { ConnectorsTabs } from '@/consts';
import safeLazy from '@/utils/lazy';

const Connectors = safeLazy(async () => import('@/pages/Connectors'));
const ConnectorDetails = safeLazy(async () => import('@/pages/ConnectorDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type RouteObject } from 'react-router-dom';

import safeLazy from '@/utils/lazy';
import { safeLazy } from 'react-safe-lazy';

const CustomizeJwt = safeLazy(async () => import('@/pages/CustomizeJwt'));
const CustomizeJwtDetails = safeLazy(async () => import('@/pages/CustomizeJwtDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { EnterpriseSsoDetailsTabs } from '@/consts/page-tabs';
import safeLazy from '@/utils/lazy';

const EnterpriseSso = safeLazy(async () => import('@/pages/EnterpriseSso'));
const EnterpriseSsoDetails = safeLazy(async () => import('@/pages/EnterpriseSsoDetails'));
Expand Down
3 changes: 1 addition & 2 deletions packages/console/src/hooks/use-console-routes/routes/mfa.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type RouteObject } from 'react-router-dom';

import safeLazy from '@/utils/lazy';
import { safeLazy } from 'react-safe-lazy';

const Mfa = safeLazy(async () => import('@/pages/Mfa'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { OrganizationRoleDetailsTabs, OrganizationTemplateTabs } from '@/consts';
import safeLazy from '@/utils/lazy';

const OrganizationTemplate = safeLazy(async () => import('@/pages/OrganizationTemplate'));
const OrganizationRoles = safeLazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { condArray } from '@silverhand/essentials';
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { OrganizationDetailsTabs } from '@/pages/OrganizationDetails/types';
import safeLazy from '@/utils/lazy';

const Organizations = safeLazy(async () => import('@/pages/Organizations'));
const OrganizationDetails = safeLazy(async () => import('@/pages/OrganizationDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type RouteObject } from 'react-router-dom';

import safeLazy from '@/utils/lazy';
import { safeLazy } from 'react-safe-lazy';

const ChangePasswordModal = safeLazy(
async () => import('@/pages/Profile/containers/ChangePasswordModal')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { RoleDetailsTabs } from '@/consts/page-tabs';
import safeLazy from '@/utils/lazy';

const Roles = safeLazy(async () => import('@/pages/Roles'));
const RoleDetails = safeLazy(async () => import('@/pages/RoleDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { SignInExperienceTab } from '@/pages/SignInExperience/types';
import safeLazy from '@/utils/lazy';

const SignInExperience = safeLazy(async () => import('@/pages/SignInExperience'));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { condArray } from '@silverhand/essentials';
import { useContext, useMemo } from 'react';
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { TenantSettingsTabs } from '@/consts';
import { TenantsContext } from '@/contexts/TenantsProvider';
import useCurrentTenantScopes from '@/hooks/use-current-tenant-scopes';
import NotFound from '@/pages/NotFound';
import safeLazy from '@/utils/lazy';

const TenantSettings = safeLazy(async () => import('@/pages/TenantSettings'));
const TenantBasicSettings = safeLazy(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { UserDetailsTabs } from '@/consts/page-tabs';
import safeLazy from '@/utils/lazy';

const AuditLogDetails = safeLazy(async () => import('@/pages/AuditLogDetails'));
const UserDetails = safeLazy(async () => import('@/pages/UserDetails'));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Navigate, type RouteObject } from 'react-router-dom';
import { safeLazy } from 'react-safe-lazy';

import { WebhookDetailsTabs } from '@/consts';
import safeLazy from '@/utils/lazy';

const WebhookDetails = safeLazy(async () => import('@/pages/WebhookDetails'));
const AuditLogDetails = safeLazy(async () => import('@/pages/AuditLogDetails'));
Expand Down
61 changes: 0 additions & 61 deletions packages/console/src/utils/lazy.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { appendPath } from '@silverhand/essentials';

import ExpectConsole from '#src/ui-helpers/expect-console.js';
import { Trace } from '#src/ui-helpers/trace.js';
import { devFeatureTest } from '#src/utils.js';

describe('error handling', () => {
const trace = new Trace();

it('should handle dynamic import errors', async () => {
devFeatureTest.it('should handle dynamic import errors', async () => {
const expectConsole = new ExpectConsole(await browser.newPage());
const path = appendPath(expectConsole.options.endpoint, 'console/__internal__/import-error');

Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87a197c

Please sign in to comment.