Skip to content

Add HTTP headers to <SvgUri> #2320

Open
@pke

Description

@pke

Feature Request

It would be nice to be able to specify HTTP headers for SvgUri.

Why it is needed

Sometimes the server could serve a different variant of the SVG file depending on client features like dark contrast or inverted colours.

Possible implementation

It would just need on addition property headers which is modelled after the default ImageURISource prop.

Code sample

export type UriProps = { 
  uri: string | null;
  headers?: { [name:string]: string };
} & AdditionalProps;

export async function fetchText(uri: string, headers?: {[name:string]: string}) {
  const response = await fetch(uri, { headers });
  return await response.text();
}

export function SvgUri(props: UriProps) {
  const { onError = err, uri, headers } = props;
  const [xml, setXml] = useState<string | null>(null);
  useEffect(() => {
    uri
      ? fetchText(uri, headers)
          .then(setXml)
          .catch(onError)
      : setXml(null);
  }, [onError, uri, headers]);
  return <SvgXml xml={xml} override={props} />;
}

Worth a PR?

The caller would need to ensure that headers is stable, or it would cause unnecessary re-rerenders.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions