-
Notifications
You must be signed in to change notification settings - Fork 44
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
Allow loading images from /public #54
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ |
Tests will fail until #64 is merged and this is rebased on it. Otherwise, I think it's ready to go. I guess we need to write the astro config to a file because of SSR? Am I correct in thinking that sometimes this code runs in node, and sometimes in the browser, which is why we can't just create a variable in memory and rely on closures to pass it around? |
I also added an |
@RafidMuhymin okay, this is ready for a review, thanks! |
LGTM 👍 ! Thanks for contributing @IanVS ! |
Fixes #1
Might help with some of the problems in #41
I am in the process of adding this tool, and found it difficult to do because normally astro loads images from
/public
, and so all my existing images live there, including images that I reference from markdown files. I hit errors during build likeInput file is missing
, without knowing why (because I didn't realize it was my images in markdown that were being automatically processed, and the error message does not say the filepath that failed).So, this change allows markdown and components to specify images in
/public
(or other configuredpublicDir
), without the need for explicitly adding/public
to the start of the url. I've tested it in my own app in dev and build, with a<Picture />
element loading from/src
and from public, as well as markdown files pointing to public images as well as http images.I'd like to add some better tests around the method, though, to be sure that it is handling the edge cases properly (I'm not sure it will deal with relative paths correctly, for instance). What would you think about adding some vitest tests? They are easy to set up, support esm natively, and run nice and quick. Would you be open to a few basic tests, to see how it works?Tests were added in #58, but are currently failing until #64 is merged. I've added some test cases here as well. Ideally, we'll add some higher-level tests soon too, to be sure that it's all working end-to-end.I also wonder if you might consider a breaking change to treat all absolute paths as being insideTalked in discord, and @RafidMuhymin prefers that we try to find files relative to both locations, which is what I've done in this PR/public
, and relative paths like./src/foo.jpg
as relative to the project root. Or ideally relative to the file itself, but that might be a little trickier.lastly, it might be better to add a try/catch around the call to sharp with a warning there when files can't be found, rather than throwing a warning in the function I made. I'll experiment with that approach as well.I've taken out the warning, and opened #66 to track as a separate feature request.