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

Avatar.Image crossorigin does not work as expected #111

Closed
trgsv opened this issue Sep 9, 2023 · 9 comments · May be fixed by #469
Closed

Avatar.Image crossorigin does not work as expected #111

trgsv opened this issue Sep 9, 2023 · 9 comments · May be fixed by #469
Labels
bug Something isn't working Svelte 5 Roadmap This issue is planned to be addressed or fixed when the library migrates to Svelte 5.

Comments

@trgsv
Copy link

trgsv commented Sep 9, 2023

crossorigin="anonymous" does not work on Image.

Set this attribute and set the top level headers in the handle hook in hooks.server.ts:

const response = await resolve(event);

response.headers.set('Cross-Origin-Embedder-Policy', 'require-corp');
response.headers.set('Cross-Origin-Opener-Policy', 'same-origin');

return response;

You get this error:
Access to image at 'https://github.com/shadcn.png' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

@huntabyte
Copy link
Owner

Does it work with regular image tags?

@trgsv
Copy link
Author

trgsv commented Oct 31, 2023

Yes

@huntabyte huntabyte added the help wanted Open to contributions label Oct 31, 2023
@huntabyte
Copy link
Owner

I'm not able to reproduce this. I get the same behavior on the primitive img as I do on the <Avatar.Image />.

This example of course fails due to CORS error, as expected, considering the fact its set to same-origin.

<script lang="ts">
   import { Avatar } from 'bits-ui'
</script>

<Avatar.Root>
   <Avatar.Image crossorigin="anonymous" src="https://github.com/huntabyte.png" alt="@huntabyte" />
   <Avatar.Fallback>HB</Avatar.Fallback>
</Avatar.Root>

<img src="https://github.com/huntabyte.png" crossorigin="anonymous" alt="hello" />

But this one works just as expected when I have the file locally in the /static dir:

<script lang="ts">
   import { Avatar } from 'bits-ui'
</script>

<Avatar.Root>
   <Avatar.Image crossorigin="anonymous" src="/huntabyte.png" alt="@huntabyte" />
   <Avatar.Fallback>HB</Avatar.Fallback>
</Avatar.Root>

<img src="/huntabyte.png" crossorigin="anonymous" alt="hello" />

If you're able to provide me with a reproduction repo that I can take a look at, I'm happy to investigate further, but I don't see how this would point back to bits, as this is what the <Avatar.Image /> looks like under the hood:

<script lang="ts">
	import { melt } from "@melt-ui/svelte";
	import { getImage, getAttrs } from "../ctx.js";
	import type { ImageProps } from "../types.js";

	type $$Props = ImageProps;
	export let src: $$Props["src"] = undefined;
	export let alt: $$Props["alt"] = undefined;
	export let asChild = false;

	$: image = getImage(src).elements.image;
	$: builder = $image;
	const attrs = getAttrs("image");
</script>

{#if asChild}
	<slot {builder} {attrs} />
{:else}
	<img use:melt={builder} {alt} {...$$restProps} {...attrs} />
{/if}

@trgsv
Copy link
Author

trgsv commented Nov 6, 2023

Try this: https://github.com/trgsv/shadcn-svelte-avatar-test
I have an example with my described behavior.

@huntabyte
Copy link
Owner

huntabyte commented Nov 6, 2023

CleanShot 2023-11-06 at 17 24 37@2x

Is this what I'm supposed to be seeing? @trgsv

@trgsv
Copy link
Author

trgsv commented Nov 6, 2023

Screenshot 2023-11-06 at 23 40 05

@trgsv
Copy link
Author

trgsv commented Nov 6, 2023

Same behavior with Chrome, Firefox and Safari

@huntabyte
Copy link
Owner

Ah I see it now... interesting 🤔

When I get some time I will try to determine if the same behavior occurs with just Melt UI to isolate the issue.

@huntabyte huntabyte added the bug Something isn't working label Feb 17, 2024
@huntabyte huntabyte added Svelte 5 Roadmap This issue is planned to be addressed or fixed when the library migrates to Svelte 5. and removed help wanted Open to contributions labels Oct 3, 2024
@huntabyte huntabyte mentioned this issue Oct 3, 2024
3 tasks
@huntabyte
Copy link
Owner

Closing as fixed in bits-ui@next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Svelte 5 Roadmap This issue is planned to be addressed or fixed when the library migrates to Svelte 5.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants