admin管理员组文章数量:1312773
I am developing this solely frontend app using react, bun, vite and typescript. This app uses react-router-dom and is composed of routes. Every route corresponds to an era and in every route there is a bunch of cards (each card corresponds to a style). Each card contains a carousel that displays some pictures. I have all the pictures inside the assets folder. The approach I used to import the corresponding pictures to every page is by creating a styles.ts file which implements a function that uses import.meta.glob
to get the pictures urls, and inside the same file I use the function to get the urls of every style. And I end up having an array corresponding to every route. Each array contains multiple objects (styles of the same era) and every object has a property "gallery" which is an array of urls that I get through the function I mentioned before.
By the way, the pictures are inside the assets folder and here is the function:
const getStyleImages = (folder: 'classic' | 'modern', styleIndex: number) => {
const prefix = (styleIndex + 1).toString()
const images = import.meta.glob('/src/assets/**/*.{png,jpg,jpeg,gif,webp}', {
eager: true,
as: 'url'
})
return Object.values(images).filter(path =>
path.includes(`/${folder}/`) &&
path.split('/').pop()?.startsWith(prefix)
)
}
The problem I am facing is that when I run the project in development (running "bun run dev"), the pictures appear, but once I build the project (bun run build) and I wanna test the project in preview or I even deploy it, the pictures aren't loaded and I don't know if the problem is in how Vite handles assets or the function.
I tried making the function async, played with the paths etc, but it doesn't seem to work.
I am developing this solely frontend app using react, bun, vite and typescript. This app uses react-router-dom and is composed of routes. Every route corresponds to an era and in every route there is a bunch of cards (each card corresponds to a style). Each card contains a carousel that displays some pictures. I have all the pictures inside the assets folder. The approach I used to import the corresponding pictures to every page is by creating a styles.ts file which implements a function that uses import.meta.glob
to get the pictures urls, and inside the same file I use the function to get the urls of every style. And I end up having an array corresponding to every route. Each array contains multiple objects (styles of the same era) and every object has a property "gallery" which is an array of urls that I get through the function I mentioned before.
By the way, the pictures are inside the assets folder and here is the function:
const getStyleImages = (folder: 'classic' | 'modern', styleIndex: number) => {
const prefix = (styleIndex + 1).toString()
const images = import.meta.glob('/src/assets/**/*.{png,jpg,jpeg,gif,webp}', {
eager: true,
as: 'url'
})
return Object.values(images).filter(path =>
path.includes(`/${folder}/`) &&
path.split('/').pop()?.startsWith(prefix)
)
}
The problem I am facing is that when I run the project in development (running "bun run dev"), the pictures appear, but once I build the project (bun run build) and I wanna test the project in preview or I even deploy it, the pictures aren't loaded and I don't know if the problem is in how Vite handles assets or the function.
I tried making the function async, played with the paths etc, but it doesn't seem to work.
Share Improve this question edited Jan 31 at 21:12 Kirstie Wilkinson 3041 gold badge2 silver badges11 bronze badges asked Jan 31 at 18:54 RemodotekaRemodoteka 11 bronze badge 2- 1 Please, can you add more information like possible errors you are facing? or when you say "It doesn't seem to work" it means you also don't receive any messages anywhere? – Daniel Aragão Commented Jan 31 at 19:21
- I have never played with Bun, but usually relative routes starting with "/" like this are wrong because you will point to your OS root folder – Daniel Aragão Commented Jan 31 at 19:24
1 Answer
Reset to default 0You might wanna check if you are already usinb Bun v.1.1.43
https://bun.sh/blog/bun-v1.1.43#html-bundler
本文标签: reactjsImages don39t load in productionStack Overflow
版权声明:本文标题:reactjs - Images don't load in production - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741898932a2403751.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论