Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7670: Ensure stylesheets are removed in IsolatedComponent #8213

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/components/IsolatedComponent.tsx
Original file line number Diff line number Diff line change
@@ -23,17 +23,22 @@ import React, { Suspense } from "react";
import { Stylesheets } from "@/components/Stylesheets";
import EmotionShadowRoot from "@/components/EmotionShadowRoot";
import isolatedComponentList from "./isolatedComponentList";
import { signalFromPromise } from "abort-utils";

const MODE = process.env.SHADOW_DOM as "open" | "closed";

type LazyFactory<T> = () => Promise<{
default: React.ComponentType<T>;
}>;

// Drop the stylesheet injected by `mini-css-extract-plugin` into the main document.
// Until this is resolved https://github.com/webpack-contrib/mini-css-extract-plugin/issues/1092#issuecomment-2037540032
async function discardNewStylesheets(signal: AbortSignal) {
/**
* Drop the stylesheet injected by `mini-css-extract-plugin` into the main document.
*
* @warning The `lazyFactory` function never not be called outside `discardStylesheetsWhilePending`
twschiller marked this conversation as resolved.
Show resolved Hide resolved
* because this helper must catch the stylesheets injected when the factory is first called.
*/
async function discardStylesheetsWhilePending(
lazyFactory: LazyFactory<unknown>,
) {
const baseUrl = chrome.runtime.getURL("");

const observer = new MutationObserver((mutations) => {
@@ -52,9 +57,16 @@ async function discardNewStylesheets(signal: AbortSignal) {
childList: true,
});

signal.addEventListener("abort", () => {
// Call and discard. React.lazy() will call it again and use the result or the error.
// This is fine because import() does not re-fetch/re-run the module.
try {
// The function must be first called *after* the observer is set up.
await lazyFactory();
} catch {
// React.lazy() will take care of it
} finally {
observer.disconnect();
});
}
}

type Props<T> = {
@@ -111,8 +123,9 @@ export default function IsolatedComponent<T>({
);
}

// `discard` must be called before `React.lazy`
void discardNewStylesheets(signalFromPromise(lazy()));
// `discard` must be called before `React.lazy`.
// `discardStylesheetsWhilePending` is needed until this is resolved https://github.com/webpack-contrib/mini-css-extract-plugin/issues/1092#issuecomment-2037540032
void discardStylesheetsWhilePending(lazy);
const LazyComponent = React.lazy(lazy);

const stylesheetUrl = noStyle ? null : chrome.runtime.getURL(`${name}.css`);