0

I'm having somes issues understanding how Vite works with static assets like image and fonts, and how to correctly reference them. Can anyone advise?

After reading the documentation, for static images I stored them in the "resources/images" folder and simply added the following in to app.js:

import.meta.glob([
'../images/**',
  ]);

and I can reference them in blade files using the following:

{{ Vite::asset('resources/images/someimage.jpg') }}

For fonts I've added the folder manually in to "public/fonts", and I reference them from the app.css file like so:

@font-face {
font-family: "Bebas Neue Pro";
src:url(/fonts/bebas_neue_pro/bebas_neue_pro.otf) format("opentype"),
}

...and add them to my Taiwind config like so:

theme: {
extend: {
fontFamily: {
sans: ["Bebas Neue Pro", 'sans-serif'],
"Bebas Neue Pro": ["Bebas Neue Pro"],
},
},
},

I run npm run build and when I view the site it works perfectly.

However when I run using npm run dev the images only work if I've already run "build" previously, and the fonts aren't picked up at all, I get "404 not found" errors.

Is there a way to get it working correctly for both build and dev?

2
  • is it a typo for static fonts url missing quotes? Commented Nov 6, 2023 at 18:47
  • I don'T think so because when I run "build" the fonts display correctly Commented Nov 7, 2023 at 7:31

1 Answer 1

0

To me it sounds like you want to put the static files in the public directory. Then you can reference them the same way in both dev and build mode (using their absolute path (without /public in the beginning)).

https://vitejs.dev/guide/assets.html#the-public-directory

Sign up to request clarification or add additional context in comments.

2 Comments

I was told exposing the public path anywhere in code was bad?
Not sure what you mean by "exposing public path", but there are benefits with importing file URLs instead of referencing files in the public folder, such as Vite will complain about mistyped filenames. But I now took a closer look at your question, and I realized that I missed that you use Laravel specific things, which I have no experience of, so I'm afraid my answer is not very helpful for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.