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

Clarify how to implement social image URLs #8149

Open
markoa opened this issue May 2, 2024 · 5 comments
Open

Clarify how to implement social image URLs #8149

markoa opened this issue May 2, 2024 · 5 comments
Labels
good first issue Good for newcomers help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@markoa
Copy link

markoa commented May 2, 2024

📚 Subject area/topic

Runtime API

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/reference/api-reference/#astrocanonicalurl

📋 Description of content that is out-of-date or incorrect

I was implementing logic for social image URLs in astro and md layouts and couldn't fnd any example that would point me in the right direction re. building canonical URLs.

The page I mentioned recommends using Astro.url, but this will generate localhost URLs.

Another thing is, reaching for images in src/ won't work.

So not sure where exactly in docs but would love to see this clarified in a single place:

  • assets should be in public/
  • you must set site global property
    Use URL.new(asset_path, Astro.site)

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@markoa markoa added the improve documentation Enhance existing documentation (e.g. add an example, improve description) label May 2, 2024
@Princesseuh
Copy link
Member

Princesseuh commented May 2, 2024

Why can't you use images in src? Astro.url is only localhost in dev or if site is not set in your config (in SSG), it's true that it'd be good to precise it in the section of Astro.url!

@markoa
Copy link
Author

markoa commented May 3, 2024

More precisely, I was not able to construct canonical URLs for images in src referenced in Markdown frontmatter.

Let's say here I have an MDX file where I want to declare path to an image to be used in the social card:

...
image: "image.jpg"
...

If the image is in src and I pass such a path to new URL(path, Astro.site) I get basically get a naively concatenated string, not the real build path where the image actually is (within _astro/...).

In my current code I store these types of images in public/.

@Princesseuh
Copy link
Member

Ah yes, for images in Markdown content, you'd need to use Content Collections for images to be resolved. https://docs.astro.build/en/guides/images/#images-in-content-collections

@markoa
Copy link
Author

markoa commented May 6, 2024

I haven't noticed Content Collections, will give them a try.

@sarah11918
Copy link
Member

Will accept a PR if anyone wants to add the extra context that @Princesseuh suggested! I could see a line added underneath the existing paragraph, something like:

A URL object constructed from the current Astro.request.url URL string value. Useful for interacting with individual properties of the request URL, like pathname and origin.

Astro.url will be localhost in dev mode if site is not configured for static sites, and for on-demand rendered sites using server or hybrid output.

(Would need a check to make sure that's exactly what was intended!)

@sarah11918 sarah11918 added good first issue Good for newcomers help wanted Issues looking for someone to run with them! labels Jun 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

No branches or pull requests

3 participants