Skip to content

Add AbortController to fetch call in audio component #919

@MODSetter

Description

@MODSetter

Description

The audio tool component fetches audio files without an AbortController. If a user navigates away mid-download, the request continues running in the background, wasting bandwidth.

Files to Update

  • surfsense_web/components/tool-ui/audio.tsx (line 91, inside the effect or callback that fetches audio)

What to Do

Wrap the fetch call with an AbortController and abort on cleanup:

// Inside the useEffect that fetches audio:
useEffect(() => {
  const controller = new AbortController();

  const loadAudio = async () => {
    try {
      const response = await fetch(src, { signal: controller.signal });
      // ... rest of the audio processing
    } catch (err) {
      if (err instanceof DOMException && err.name === 'AbortError') {
        return; // Expected when component unmounts
      }
      console.error('Failed to load audio:', err);
    }
  };

  loadAudio();

  return () => {
    controller.abort();
  };
}, [src]);

Reference: surfsense_web/components/homepage/github-stars-badge.tsx (lines 249–266) already uses this pattern correctly as an example.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions