#Should images be bundled into the "public/build" directory.?

1 messages · Page 1 of 1 (latest)

manic raft
#

In the remix.config.js file, the assetsBuildDirectory uses "public/build" as the default. After I run npm run build, I would have expected to see my images in that output as well, but I'm not. For context, I'm storing all images in "public/assets".

astral palm
#

I believe it depends on whether you're importing the image


import image from "./image.jpg";
...
<img src={image} />

or just linking it as src
<img src="/assets/image.jpg" />

#

First option should get fingerprinted and auto bundled into your build, second won't and would just be served directly out of your public folder.

#

(I might have syntax on first option wrong, since I don't use it very often)

manic raft
#

@astral palm Thanks, that clarifies most of it. I just tested the first option and it does include the image in the build folder. Is there a recommended method? I wasn't expecting those two methods to behave differently like that.

For anyone else, here is the syntax for the first option:
import logo from 'public/assets/logos/logo.svg'

gritty dragon
#

You can tell browsers to cache them forever because that image URL will never be different

#

whereas if you just have them in the public dir you'll need to rename them manually or be ok with users still getting the previous version until their cache expires

gritty dragon
#

the original in /public and a hashed one in /public/build

#

generally you'd do the import from somewhere else in your codebase

zinc granite
#

I prefer to import images whenever I can, this way I get a hashed file name that I could cache forever (not really forever but almost)

acoustic knoll
#

Agreed on not importing from public folder, if you want the first method then use images located outside of public.

zinc granite
#

and when I import an image I place the image either in an app/assets folder or using v2 route file convention I put the image in the route folder like app/some.route._index/image.png

#

I fallback to using the public folder when the image for some reason needs to have a specific name, examples are favicon.ico that some browsers fetch regardless of the <link> tag indicating another path

#

or some apple images that also iOS request always from the same path

acoustic knoll
zinc granite
#

yes, definitely not in public if I'm importing them

acoustic knoll
zinc granite
acoustic knoll
#

Yeah was a handy function but just thought I’d give a heads up that 1.18.1 seems to have broken it (also apologies for changing topic)

zinc granite
#

but since remix utils still supports old version of Remix I haven't checked, I plan to do a major release of remix utils once v2 is out, and there I can update other things that are also depending on internals that changed

astral palm
drifting hound
#

I'm running into a similar issue trying to load an image as a background-image using SCSS… currently the image is in "app/images" but once built, it isn't referencing the image from the build folder. Any ideas?

zinc granite
zinc granite
drifting hound
zinc granite
drifting hound
acoustic knoll
manic raft
acoustic knoll
drifting hound
gritty dragon
#

the public directory is the one that's served