Skip to content
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

Load Assets' Images #7

Open
a14d opened this issue Aug 25, 2021 · 5 comments
Open

Load Assets' Images #7

a14d opened this issue Aug 25, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@a14d
Copy link

a14d commented Aug 25, 2021

Thanks for this really helpful library.

Is there a possibility to load the static images that are inside the static folder ? Like Django's {% static %}.

For example:
When I visit an image using {% static %} it shows 404 error.

But when I change the port to 3000 it shows that django-vite imported it.

@MrBin99
Copy link
Owner

MrBin99 commented Aug 26, 2021

Thanks !

It's possible, you need to import them into you main.js file like CSS. But be aware that, last time I tested this, Vite put URLs of those static files a bit messy in the manifest.json to be load with Python safely.
For now, static files (like images, fonts ...) are not supported by django-vite. You should use the {% static %} of Django.

I keep your issue open, and if other peoples are demanding of this feature I will give it a look, or if you want to do a PR and implement it, you're welcome.

@MrBin99 MrBin99 added the enhancement New feature or request label Aug 26, 2021
@Sergdan1992
Copy link

It would be great if the django-vite supported images. Or any expample how handle images in development and build state

@aresti
Copy link

aresti commented Dec 14, 2022

I got stuck with this for a little while. Just a note for anyone looking to import images and use them elsewhere within their javascript frontend:

As per Vite docs https://vitejs.dev/guide/assets.html you can just:

import imgUrl from './img.png'
document.getElementById('hero-img').src = imgUrl

They won't work with django-vite in dev mode right away, because it'll be hitting the django dev server using a relative path, rather than your vite dev server.

To fix, just add the following option to your vite config:

  server: {
    origin: "http://localhost:5173",
  }

That way, you'll get absolute generated asset URLs during development
docs ref: https://vitejs.dev/config/server-options.html#server-origin

@emab
Copy link
Contributor

emab commented Jun 9, 2023

Another way of achieving this is to use a copy plugin (such as https://www.npmjs.com/package/rollup-plugin-copy) to copy any assets you need in Django during your Vite build process - then when Django grabs your dist output folder you can just use static to reference your assets.

This is useful if the assets themselves aren't bundled by Vite.

@pySilver
Copy link

@aresti this should be documented :) faced the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants