Skip to content

Studio: mascot images degrade gracefully instead of showing alt text - #6146

Merged
danielhanchen merged 4 commits into
mainfrom
studio-mascot-fallback
Jun 10, 2026
Merged

danielhanchen merged 4 commits into
mainfrom
studio-mascot-fallback

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jun 10, 2026 •

Copy link
Copy Markdown
Member

What this does

Some users hit an empty mascot with raw alt text on the training start screen (and potentially anywhere a sloth renders). Mascots were plain <img> tags pointing at public-folder URLs with no error handling, so any failed load painted the alt text and stayed that way.

Why the image can fail

  • Version skew while unsloth studio update swaps the frontend on disk, or a custom frontend_path missing public assets: the file 404s.
  • Hardcoded absolute /... paths break when Studio is mounted behind a subpath; some call sites already prefixed BASE_URL, others did not.
  • Transient network blips on LAN or proxied access fail the request once and the <img> never retries.

Fix

A shared MascotImg component used by every mascot call site (chat greeting, 404 page, training start overlay, auth form, onboarding splash, onboarding wizard, artifact generating panel):

  • Resolves the source against import.meta.env.BASE_URL and URI-encodes the spaced sloth filenames, one code path for all call sites.
  • On error, retries once with a cache-buster, which covers transient failures and stale caches.
  • If the retry also fails, swaps to a 4 KB fallback sloth bundled into the JS as a data URI, which cannot 404 because it ships with the code that renders it.
  • Defaults to an empty alt: the mascots are decorative, so even the instant before the fallback applies paints nothing.

Testing

  • tsc -b and vite build pass; biome and eslint clean on the new files apart from the codebase-standard react-refresh warning for the exported helper.
  • Live Studio session with Chromium: the greeting mascot loads normally from the public folder. With all mascot URLs intercepted and forced to 404, exactly two requests fire (original plus retry) and the bundled fallback renders seamlessly at the same size, with no alt text at any point. Screenshot comparison shows no visual difference in the failure case beyond the fallback art.

Mascots loaded from raw public-folder URLs with no error handling, so a
missing or unreachable file (version skew during updates, subpath
mounts, transient blips) painted the alt text, like the empty mascot on
the training start overlay. A shared MascotImg now resolves against
BASE_URL, retries once with a cache-buster, then swaps to a 4 KB
fallback sloth bundled as a data URI that cannot 404. Applied to the
chat greeting, 404 page, training start overlay, auth form, onboarding
splash and wizard.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new MascotImg component to replace standard img tags for mascot images across the application. This component handles asset resolution against the deploy base and implements a graceful degradation strategy, retrying failed loads with a cache-buster before falling back to a bundled inline mascot. Feedback suggests refactoring MascotImg to leverage React's key prop behavior to reset state automatically when the src changes, rather than manually tracking state during render.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +23 to +53
export function MascotImg({
src,
alt = "",
...rest
}: { src: string } & Omit<ComponentProps<"img">, "src" | "onError">) {
const [stage, setStage] = useState<Stage>("primary");
// Reset during render when the source changes (greeting sloths rotate).
const [lastSrc, setLastSrc] = useState(src);
if (src !== lastSrc) {
setLastSrc(src);
setStage("primary");
}

const url = publicAssetUrl(src);
const effectiveSrc =
stage === "primary"
? url
: stage === "retry"
? `${url}${url.includes("?") ? "&" : "?"}retry=1`
: fallbackMascot;

return (
<img
draggable={false}
{...rest}
src={effectiveSrc}
alt={alt}
onError={() => setStage((s) => (s === "primary" ? "retry" : "fallback"))}
/>
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Instead of manually tracking lastSrc in state and performing a state update during render (which can be harder to reason about and is less idiomatic), we can leverage React's built-in key prop behavior. By splitting MascotImg into a lightweight wrapper and an inner component, and passing key={src} to the inner component, React will automatically reset the inner component's state (including stage) whenever src changes.

type MascotImgProps = { src: string } & Omit<ComponentProps<"img">, "src" | "onError">;

export function MascotImg({ src, ...props }: MascotImgProps) {
  return <MascotImgInner key={src} src={src} {...props} />;
}

function MascotImgInner({
  src,
  alt = "",
  ...rest
}: MascotImgProps) {
  const [stage, setStage] = useState<Stage>("primary");

  const url = publicAssetUrl(src);
  const effectiveSrc =
    stage === "primary"
      ? url
      : stage === "retry"
        ? `${url}${url.includes("?") ? "&" : "?"}retry=1`
        : fallbackMascot;

  return (
    <img
      draggable={false}
      {...rest}
      src={effectiveSrc}
      alt={alt}
      onError={() => setStage((s) => (s === "primary" ? "retry" : "fallback"))}
    />
  );
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, switched to the key remount so React resets the retry state per source and the manual previous-src tracking is gone. Re-verified with the blocked-asset test: normal loads come from the public folder and forced 404s still end on the bundled fallback.

@danielhanchen

Copy link
Copy Markdown
Member Author

pre-commit.ci run

# Conflicts:
#	studio/frontend/src/features/studio/training-start-overlay.tsx
@danielhanchen
danielhanchen merged commit 18d851b into main Jun 10, 2026
29 of 33 checks passed
@danielhanchen
danielhanchen deleted the studio-mascot-fallback branch June 10, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant