rusackas commented on code in PR #43215:
URL: https://github.com/apache/superset/pull/43215#discussion_r3976498705


##########
superset-frontend/src/views/routes.tsx:
##########
@@ -32,161 +28,161 @@ import { RoutePaths } from './routePaths';
 // not lazy loaded since this is the home page.
 import Home from 'src/pages/Home';
 
-const ChartCreation = lazy(
+const ChartCreation = lazyWithRetry(
   () =>
     import(/* webpackChunkName: "ChartCreation" */ 'src/pages/ChartCreation'),
 );
 
-const AnnotationLayerList = lazy(
+const AnnotationLayerList = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "AnnotationLayerList" */ 
'src/pages/AnnotationLayerList'
     ),
 );
 
-const AlertReportList = lazy(
+const AlertReportList = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "AlertReportList" */ 'src/pages/AlertReportList'
     ),
 );
 
-const AnnotationList = lazy(
+const AnnotationList = lazyWithRetry(
   () =>
     import(/* webpackChunkName: "AnnotationList" */ 
'src/pages/AnnotationList'),
 );
 
-const ChartList = lazy(
+const ChartList = lazyWithRetry(
   () => import(/* webpackChunkName: "ChartList" */ 'src/pages/ChartList'),
 );
 
-const ArchivedList = lazy(
+const ArchivedList = lazyWithRetry(
   () => import(/* webpackChunkName: "ArchivedList" */ 
'src/pages/ArchivedList'),
 );
 
-const CssTemplateList = lazy(
+const CssTemplateList = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "CssTemplateList" */ 'src/pages/CssTemplateList'
     ),
 );
 
-const ThemeList = lazy(
+const ThemeList = lazyWithRetry(
   () => import(/* webpackChunkName: "ThemeList" */ 'src/pages/ThemeList'),
 );
 
-const DashboardList = lazy(
+const DashboardList = lazyWithRetry(
   () =>
     import(/* webpackChunkName: "DashboardList" */ 'src/pages/DashboardList'),
 );
 
-const Dashboard = lazy(
+const Dashboard = lazyWithRetry(
   () => import(/* webpackChunkName: "Dashboard" */ 'src/pages/Dashboard'),
 );
 
-const DatabaseList = lazy(
+const DatabaseList = lazyWithRetry(
   () => import(/* webpackChunkName: "DatabaseList" */ 
'src/pages/DatabaseList'),
 );
 
-const DatasetList = lazy(
+const DatasetList = lazyWithRetry(
   () => import(/* webpackChunkName: "DatasetList" */ 'src/pages/DatasetList'),
 );
 
-const DatasetCreation = lazy(
+const DatasetCreation = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "DatasetCreation" */ 'src/pages/DatasetCreation'
     ),
 );
 
-const ExecutionLogList = lazy(
+const ExecutionLogList = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "ExecutionLogList" */ 'src/pages/ExecutionLogList'
     ),
 );
 
-const Chart = lazy(
+const Chart = lazyWithRetry(
   () => import(/* webpackChunkName: "Chart" */ 'src/pages/Chart'),
 );
 
-const QueryHistoryList = lazy(
+const QueryHistoryList = lazyWithRetry(
   () =>
     import(
       /* webpackChunkName: "QueryHistoryList" */ 'src/pages/QueryHistoryList'
     ),
 );
 
-const SavedQueryList = lazy(
+const SavedQueryList = lazyWithRetry(
   () =>
     import(/* webpackChunkName: "SavedQueryList" */ 
'src/pages/SavedQueryList'),
 );
 
-const SqlLab = lazy(
+const SqlLab = lazyWithRetry(
   () => import(/* webpackChunkName: "SqlLab" */ 'src/pages/SqlLab'),
 );
 
-const AllEntities = lazy(
+const AllEntities = lazyWithRetry(
   () => import(/* webpackChunkName: "AllEntities" */ 'src/pages/AllEntities'),
 );
 
-const Tags = lazy(
+const Tags = lazyWithRetry(
   () => import(/* webpackChunkName: "Tags" */ 'src/pages/Tags'),
 );
 
-const Extensions = lazy(
+const Extensions = lazyWithRetry(
   () => import(/* webpackChunkName: "Tags" */ 'src/extensions/ExtensionsList'),

Review Comment:
   Good catch, fixed. Gave `Extensions` its own `webpackChunkName` instead of 
reusing `Tags`'s.



##########
superset-frontend/src/utils/lazyWithRetry.ts:
##########
@@ -0,0 +1,96 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { LazyExoticComponent, lazy } from 'react';
+
+/**
+ * `React.lazy` memoizes the *rejection* of its factory: once the dynamic
+ * `import()` behind a route fails (a chunk that 404s after a redeploy, or a
+ * transient 502/503 from the asset server), the payload is permanently marked
+ * as rejected and the component throws `ChunkLoadError` forever - even after
+ * the asset becomes reachable again and even when the user navigates away and
+ * back. Retrying the import *inside* the factory, before the promise handed to
+ * `React.lazy` settles, is the only way to recover from a transient failure.
+ *
+ * See https://github.com/apache/superset/issues/41266
+ */
+export const DEFAULT_LAZY_RETRIES = 2;
+export const DEFAULT_LAZY_RETRY_DELAY_MS = 500;
+
+export interface LazyRetryOptions {
+  /** Number of *additional* attempts made after the first one fails. */
+  retries?: number;
+  /** Base delay between attempts; doubled on every subsequent attempt. */
+  retryDelayMs?: number;
+}
+
+const sleep = (ms: number): Promise<void> =>
+  new Promise(resolve => {
+    setTimeout(resolve, ms);
+  });
+
+/**
+ * Calls `factory`, retrying with exponential backoff when it rejects. Rejects
+ * with the last error once every attempt has been exhausted.
+ */
+export async function retryImport<T>(
+  factory: () => Promise<T>,
+  {
+    retries = DEFAULT_LAZY_RETRIES,
+    retryDelayMs = DEFAULT_LAZY_RETRY_DELAY_MS,
+  }: LazyRetryOptions = {},
+): Promise<T> {
+  let lastError: unknown;
+  for (let attempt = 0; attempt <= retries; attempt += 1) {
+    try {
+      // eslint-disable-next-line no-await-in-loop
+      return await factory();
+    } catch (error) {
+      lastError = error;
+      if (attempt < retries) {
+        // eslint-disable-next-line no-await-in-loop
+        await sleep(retryDelayMs * 2 ** attempt);
+      }

Review Comment:
   Agreed, fixed. Added an `isChunkLoadError` predicate so only recognized 
chunk-load failures get retried, syntax/module errors surface immediately now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to