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

Add HTTP headers to <SvgUri> #2320

Open
pke opened this issue Jul 1, 2024 · 4 comments
Open

Add HTTP headers to <SvgUri> #2320

pke opened this issue Jul 1, 2024 · 4 comments

Comments

@pke
Copy link

pke commented Jul 1, 2024

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.

@github-actions github-actions bot added the Missing info The user didn't precise the problem enough label Jul 1, 2024

This comment was marked as off-topic.

@github-actions github-actions bot added the Missing repro This issue need minimum repro scenario label Jul 1, 2024

This comment was marked as off-topic.

@pke
Copy link
Author

pke commented Jul 1, 2024

Hey bot, its a feature request not a bug. The template looks totally different and I filled out everything.

@bohdanprog bohdanprog added Feature request and removed Missing repro This issue need minimum repro scenario Missing info The user didn't precise the problem enough labels Jul 2, 2024
@bohdanprog
Copy link
Member

Hello @pke,
Could you provide an example of how it can be useful? For instance, it can be a private API for which we can give headers to authorize. Do you know if there is another example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants