This is an automated email from the ASF dual-hosted git repository.
bbovenzi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new c94f4f4eff3 Keep a failing page plugin from taking down the page
(#77408)
c94f4f4eff3 is described below
commit c94f4f4eff33b8cab1d68de3d92473a4fb988fa5
Author: Brent Bovenzi <[email protected]>
AuthorDate: Wed Nov 25 10:19:28 2026 -0500
Keep a failing page plugin from taking down the page (#77408)
* Keep a failing page plugin from taking down the page
A page plugin that throws while rendering, or in an effect, reached the
route's error element and replaced the whole page it was only meant to
add to. Plugin actions already had a boundary for this; page plugins now
share it, so a failing plugin leaves the page as it would be without it
and the error goes to the console, as a bundle that fails to load already
does.
ReactPlugin also called lazy() during render, which is a new component
type on every render unless the React Compiler memoizes it.
Co-Authored-By: Claude <[email protected]>
* Give a failed plugin another try after navigating
The Dag, run and task pages keep their route element mounted when only
the params change, so a plugin that threw on one Dag stayed hidden on
every other Dag until a full reload.
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude <[email protected]>
---
.../components/PluginActions/PluginReactAction.tsx | 7 ++-
...PluginActionBoundary.tsx => PluginBoundary.tsx} | 28 ++++++---
.../src/airflow/ui/src/pages/ReactPlugin.test.tsx | 70 +++++++++++++++++++++-
.../src/airflow/ui/src/pages/ReactPlugin.tsx | 33 +++++-----
4 files changed, 111 insertions(+), 27 deletions(-)
diff --git
a/airflow-core/src/airflow/ui/src/components/PluginActions/PluginReactAction.tsx
b/airflow-core/src/airflow/ui/src/components/PluginActions/PluginReactAction.tsx
index 8d1776d0149..9595f68c0a8 100644
---
a/airflow-core/src/airflow/ui/src/components/PluginActions/PluginReactAction.tsx
+++
b/airflow-core/src/airflow/ui/src/components/PluginActions/PluginReactAction.tsx
@@ -24,7 +24,8 @@ import { IconButton } from "src/system-components";
import { loadPlugin, type PluginProps } from "src/pages/ReactPlugin";
-import { PluginActionBoundary } from "./PluginActionBoundary";
+import { PluginBoundary } from "src/components/PluginBoundary";
+
import { PluginActionIcon } from "./PluginActionIcon";
import type { PluginActionButtonProps, PluginActionProps } from "./types";
@@ -89,9 +90,9 @@ export const PluginReactAction = ({
<PluginActionIcon action={action} />
</IconButton>
{Plugin === undefined ? undefined : (
- <PluginActionBoundary name={action.name}>
+ <PluginBoundary name={action.name}>
<Plugin {...pluginProps} onClose={() => setOpen(false)} open={open}
triggerRef={triggerRef} />
- </PluginActionBoundary>
+ </PluginBoundary>
)}
</>
);
diff --git
a/airflow-core/src/airflow/ui/src/components/PluginActions/PluginActionBoundary.tsx
b/airflow-core/src/airflow/ui/src/components/PluginBoundary.tsx
similarity index 65%
rename from
airflow-core/src/airflow/ui/src/components/PluginActions/PluginActionBoundary.tsx
rename to airflow-core/src/airflow/ui/src/components/PluginBoundary.tsx
index 1546d116713..9ff44c9d2ab 100644
---
a/airflow-core/src/airflow/ui/src/components/PluginActions/PluginActionBoundary.tsx
+++ b/airflow-core/src/airflow/ui/src/components/PluginBoundary.tsx
@@ -22,22 +22,23 @@ type Props = {
readonly children: ReactNode;
/** Named in the console message, so the failing plugin is identifiable. */
readonly name: string;
+ /** A failed plugin renders again when this changes, e.g. on navigating to
another Dag. */
+ readonly resetKey?: string;
};
/**
* Keeps a plugin component's render failure inside the plugin's own slot.
*
- * Without this the error reaches the route's error element and replaces the
whole list or
- * detail page, so one plugin takes down the page it was only meant to add a
button to. A
- * plausible way in: an action named `History` or `Image` finds the browser's
own constructor
- * already on `globalThis`, which shadows the bundle's own registration, and
React throws
- * calling it.
+ * Without this the error reaches the route's error element and replaces the
whole page, so one
+ * plugin takes down the page it was only meant to add to. A plausible way in:
a plugin named
+ * `History` or `Image` finds the browser's own constructor already on
`globalThis`, which shadows
+ * the bundle's own registration, and React throws calling it.
*
- * Renders nothing once it has caught, which matches how a bundle that fails
to load behaves --
- * the button stays, its surface does not open. A class is required: there is
still no hook
+ * Renders nothing once it has caught, which matches how a bundle that fails
to load behaves: the
+ * page shows as it would without the plugin. A class is required: there is
still no hook
* equivalent of `componentDidCatch`.
*/
-export class PluginActionBoundary extends Component<Props, { hasError: boolean
}> {
+export class PluginBoundary extends Component<Props, { hasError: boolean }> {
public constructor(props: Props) {
super(props);
this.state = { hasError: false };
@@ -51,7 +52,16 @@ export class PluginActionBoundary extends Component<Props, {
hasError: boolean }
const { name } = this.props;
// eslint-disable-next-line no-console
- console.error(`Plugin action "${name}" failed to render`, error,
info.componentStack);
+ console.error(`Plugin "${name}" failed to render`, error,
info.componentStack);
+ }
+
+ public componentDidUpdate(prevProps: Props) {
+ const { resetKey } = this.props;
+ const { hasError } = this.state;
+
+ if (hasError && prevProps.resetKey !== resetKey) {
+ this.setState({ hasError: false });
+ }
}
public render() {
diff --git a/airflow-core/src/airflow/ui/src/pages/ReactPlugin.test.tsx
b/airflow-core/src/airflow/ui/src/pages/ReactPlugin.test.tsx
index 3a882b4bf9a..9f0d47c6921 100644
--- a/airflow-core/src/airflow/ui/src/pages/ReactPlugin.test.tsx
+++ b/airflow-core/src/airflow/ui/src/pages/ReactPlugin.test.tsx
@@ -17,7 +17,9 @@
* under the License.
*/
import "@testing-library/jest-dom/vitest";
-import { render } from "@testing-library/react";
+import { useEffect } from "react";
+
+import { render, screen } from "@testing-library/react";
import type * as ReactRouterDom from "react-router-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -239,3 +241,69 @@ describe("ReactPlugin loading fallback", () => {
expect(container.querySelector(".chakra-spinner")).not.toBeInTheDocument();
});
});
+
+const throwPluginError = () => {
+ throw new Error("plugin bug");
+};
+
+const ThrowsWhileRendering = () => throwPluginError();
+
+const ThrowsInEffect = () => {
+ useEffect(throwPluginError, []);
+
+ return <p>Plugin content</p>;
+};
+
+describe("ReactPlugin failures", () => {
+ afterEach(() => {
+ mockParams = {};
+ (globalThis as Record<string, unknown>)[reactApp.name] = undefined;
+ });
+
+ it.each([
+ ["while rendering", ThrowsWhileRendering],
+ ["in an effect", ThrowsInEffect],
+ ])("keeps the page and renders nothing when a plugin throws %s", (_when,
component) => {
+ const consoleError = vi.spyOn(console, "error").mockImplementation(() =>
undefined);
+
+ (globalThis as Record<string, unknown>)[reactApp.name] = component;
+
+ const { container } = render(
+ <>
+ <p>Page content</p>
+ <ReactPlugin reactApp={reactApp} />
+ </>,
+ { wrapper: Wrapper },
+ );
+
+ expect(screen.getByText("Page content")).toBeInTheDocument();
+ expect(screen.queryByText("Plugin content")).not.toBeInTheDocument();
+ expect(container).toHaveTextContent(/^Page content$/u);
+ expect(consoleError).toHaveBeenCalledWith(
+ `Plugin "${reactApp.name}" failed to render`,
+ expect.any(Error),
+ expect.any(String),
+ );
+ });
+
+ it("renders the plugin again after navigating away from where it failed", ()
=> {
+ vi.spyOn(console, "error").mockImplementation(() => undefined);
+ mockParams = { dagId: "broken_dag" };
+ (globalThis as Record<string, unknown>)[reactApp.name] = ({ dagId }:
PluginProps) => {
+ if (dagId === "broken_dag") {
+ throwPluginError();
+ }
+
+ return <p>Plugin content</p>;
+ };
+
+ const { rerender } = render(<ReactPlugin reactApp={reactApp} />, {
wrapper: Wrapper });
+
+ expect(screen.queryByText("Plugin content")).not.toBeInTheDocument();
+
+ mockParams = { dagId: "working_dag" };
+ rerender(<ReactPlugin reactApp={reactApp} />);
+
+ expect(screen.getByText("Plugin content")).toBeInTheDocument();
+ });
+});
diff --git a/airflow-core/src/airflow/ui/src/pages/ReactPlugin.tsx
b/airflow-core/src/airflow/ui/src/pages/ReactPlugin.tsx
index 6e7f4037d4f..dd6de3981b4 100644
--- a/airflow-core/src/airflow/ui/src/pages/ReactPlugin.tsx
+++ b/airflow-core/src/airflow/ui/src/pages/ReactPlugin.tsx
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-import { type ComponentType, lazy, Suspense } from "react";
+import { type ComponentType, lazy, Suspense, useState } from "react";
import { Spinner } from "@chakra-ui/react";
import { useParams } from "react-router-dom";
@@ -35,6 +35,8 @@ import type {
TaskInstanceResponse,
} from "openapi/requests/types.gen";
+import { PluginBoundary } from "src/components/PluginBoundary";
+
import { useTaskInstanceCoordinates } from
"src/hooks/useTaskInstanceCoordinates";
import { loadRouteModule, reloadAfterFailedRouteModuleLoad } from
"src/utils/routeModule";
@@ -105,6 +107,10 @@ export const loadPlugin = (
export const ReactPlugin = ({ reactApp }: { readonly reactApp:
ReactAppResponse }) => {
const { assetId, dagId, mapIndex, runId, taskId } = useParams();
const coordinates = useTaskInstanceCoordinates();
+ // A `lazy()` call in render is a new component type each render unless the
compiler memoizes it;
+ // holding it in state keeps it to one per mount without depending on that.
Callers key by plugin.
+ // oxlint-disable-next-line react/hook-use-state -- the lazy component is
never replaced
+ const [LazyPlugin] = useState(() => lazy(() => loadPlugin(reactApp)));
// Context objects are not part of the route, so resolve them from the query
cache. Each query
// is gated on the route params it needs, so it stays disabled where those
params are absent
@@ -154,22 +160,21 @@ export const ReactPlugin = ({ reactApp }: { readonly
reactApp: ReactAppResponse
// If the plugin component was already registered on the global object by a
previous load,
// render it directly without going through Suspense/lazy (avoids flashing
the spinner).
const existing = (globalThis as Record<string, unknown>)[reactApp.name];
-
- if (typeof existing === "function") {
- const Plugin = existing as PluginComponentType;
-
- return <Plugin {...pluginProps} />;
- }
-
- // Otherwise, lazy-load the bundle once. When it resolves, it must set a
function component
- // under globalThis[reactApp.name], which we then use as the default export.
- const LazyPlugin = lazy(() => loadPlugin(reactApp));
// A base plugin sits above every page, so a spinner there shows on every
full page load.
const fallback = reactApp.destination === "base" ? null : <Spinner />;
+ const Plugin = typeof existing === "function" ? (existing as
PluginComponentType) : undefined;
return (
- <Suspense fallback={fallback}>
- <LazyPlugin {...pluginProps} />
- </Suspense>
+ // The route element stays mounted across Dags, runs and tasks, so a
plugin that failed on one
+ // gets another try on the next instead of staying hidden until a full
reload.
+ <PluginBoundary name={reactApp.name} resetKey={[assetId, dagId, runId,
taskId, mapIndex].join("/")}>
+ {Plugin === undefined ? (
+ <Suspense fallback={fallback}>
+ <LazyPlugin {...pluginProps} />
+ </Suspense>
+ ) : (
+ <Plugin {...pluginProps} />
+ )}
+ </PluginBoundary>
);
};