-
Notifications
You must be signed in to change notification settings - Fork 262
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
<Image />
component throws TypeError
with imported local assets during development
#2540
Comments
<Image />
component throws TypeError
with imported local assets during development
You should be able to override the loader with // export type LoaderParams = {
// /** The base URL of the image */
// src?: ImageType['url'];
// /** The URL param that controls width */
// width?: number;
// /** The URL param that controls height */
// height?: number;
// /** The URL param that controls the cropping region */
// crop?: Crop;
// };
<Image loader={({src}: LoaderParams) => (src)} src={YOUR_LOCAL_ASSET} /> |
That would work if we could determine the full asset URL from the imported path, which is just the path to the file relative to the root of the project. However, Vite hashes the filenames of static assets ( |
It works for me .. Image file in
|
Sorry, I thought you were suggesting we write a custom loader that creates a fully formed URL for the asset and then calls the default The point of raising this issue was to highlight that 1) there's a difference between dev time and deployment behaviour, 2) this difference wasn't there before Vite, and 3) considering that loading static assets is a perfectly normal use case, the dev time behaviour seems like a bug. It makes sense to me that the default |
But static assets from project won't get the same width/height/crop treatments even if you have those query parameters. Only image assets served from It's why we had the loader to be overridable. Since there are devs require images from other source (other image service) that may have different url construction requirement to reconstruct the url in the proper format. It would have been better off to just use the image tag for static assets since each srcset defined is pulling in the exact same file, despite the query parameters |
After deployment, static assets are served from The above is a pre-Vite deployment, but it's the same for a Vite preview deployment I just did - static assets are served from Shopify's CDN. |
Ah that makes sense now - I totally forgot that static assets also got mapped from Oxygen. Most likely a small fix for the
|
What is the location of your example repository?
https://github.com/tomglaize/hydrogen-local-image
Which package or tool is having this issue?
Hydrogen
What version of that package or tool are you using?
2024.7.6
What version of Remix are you using?
2.10.1
Steps to Reproduce
<Image />
componentExpected Behavior
See the image on the page. This was the behaviour prior to Vite.
Actual Behavior
The default
shopifyLoader
used by<Image />
throws aTypeError
:This is because the asset url created by the import is just a path (e.g.
/app/assets/image.png
), which causesnew URL()
to throw a TypeError because it's missing a protocol and host. Before Vite, asset imports were replaced with fully formed mini-oxygen urls during development.Note this only happens during local development. On deployment to Oxygen, the behaviour is as expected.
The text was updated successfully, but these errors were encountered: