0

I have an svg file placed in /public/assets/icons, and I try to use it from within a CSS file:

.some-icon { background-image: url("/assets/icons/arrow.svg"); } 

I get error 404 Not Found (seen in devtools), but when I try to use the same path and image from within a Blade file as <img>, it works:

<img src="/assets/icons/arrow.svg" alt=""> 

I can see that it might be related to the Vite dev server because it tries to get the images that are loaded from CSS files via http://[::1]:5173/assets/icons/arrow.svg

and when it's in the <img> it's from localhost:8000.

How can I resolve that? What is the correct way?

1 Answer 1

1

The issue is that Vite handles assets differently in development mode. When using background-image: url("/assets/icons/arrow.svg") in CSS, Vite tries to resolve it through its own dev server (port 5173), but your SVG is a static file in /public, served by Laravel's server (port 8000).

To fix this:

  • Move arrow.svg from /public/assets/icons to resources/assets/icons (or a similar folder under resources).

  • Update your CSS to use a relative path (assuming your CSS is in resources/css):

    .some-icon { background-image: url('../assets/icons/arrow.svg'); } 

Vite will now bundle and serve the asset correctly in dev mode, and it works in production too. Your <img> tag works because it's directly hitting the Laravel server.

If you must keep it in /public, add a proxy in vite.config.js to forward requests:

export default defineConfig({ // ... existing config server: { proxy: { '/assets': { target: 'http://localhost:8000', changeOrigin: true, }, }, }, }); 

Restart Vite after changes.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.