Skip to content

[docs-infra] Fix AbortController error #46408

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
10 changes: 7 additions & 3 deletions docs/src/modules/utils/useLazyCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ export default function useLazyCSS(href: string, before: string, options: { laye

// With layer option, we need to fetch the CSS content and wrap it
let styleElement: HTMLStyleElement | null = null;
const abortController = new AbortController();
let canceled = false;

// Fetch the CSS content directly to avoid CORS issues with cssRules
fetch(href, { signal: abortController.signal })
fetch(href)
.then((response) => {
if (!response.ok) {
throw new Error(`Failed to fetch CSS: ${response.statusText}`);
}
return response.text();
})
.then((cssText) => {
if (canceled) {
return;
}

// Create a style element with the CSS wrapped in the specified layer
styleElement = document.createElement('style');
styleElement.setAttribute('data-href', href);
Expand Down Expand Up @@ -60,7 +64,7 @@ export default function useLazyCSS(href: string, before: string, options: { laye
// Cleanup function
return () => {
// Cancel any pending fetch
abortController.abort();
canceled = true;

// Remove the style element if it was created
if (styleElement && styleElement.parentElement) {
Expand Down