-
Notifications
You must be signed in to change notification settings - Fork 73
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
Prefix URLs with custom origin to serve assets from CDN #105
Comments
vite_asset should respect |
During dev mode, django-vite will respect In your settings you should set The default formula for discovering your manifest.json is this:
And it looks like your vite assets live right under the /static/ directory, so I'm guessing that's where your manifest.json lives too. I'm also guessing there isn't a need for you to set a static_url_prefix in this case, just a STATIC_ROOT. |
As Django docs say:
The way my deployment works is it collects static files locally and then they are served by Nginx acting like CDN upstream. So I don't use custom Changing |
Whoops. My bad, @jacobb was right. Yeah STATIC_ROOT is only used for collectstatic, I think you're going to want to set your What is your STATIC_URL currently set to? Does your nginx setup work for other {% static %} django assets, and only doesn't work with assets from django-vite? |
Currently My current setup now is simple: custom tag One way I see to achieve the same behavior is to make another custom tag |
a new template tag is likely over-complicating it, depending on how you determine what environment you're in. if ENVIRONMENT == "prod":
STATIC_URL = "https://cdn.example.com/static/"
else:
STATIC_URL = "/static/"
# other handy dev static-related settings, such as DJANGO_VITE_DEV_MODE Is there a situation where you would want to serve some files from the CDN, but not everything? If so, a template tag that forces a particular STATIC_URL may be called for, but depending on the use case something else may be best. |
Joining the bandwagon here - we also have this issue due to transitioning between old(legacy) UI and new vite-based UI. Our vite stuff lives on a different CDN for various reasons. It might be worth having a setting that can take a callable or something and resolves the base url accordingly (the default implementation will just return STATIC_URL as it is now.). I'll see if there's any clever ways around this for now. I think it might be a bit more involved due to EDIT: I ended up doing a hacky monkeypatch, something like this in the settings:
And it works for me for now. Just sharing in case someone else finds this thread, since I don't think changing the staticfiles behaviour is wise for the default case, vs. introducing this to a much older commercial project. |
Thanks for all the feedback. I'm going to take another look at providing better support for CDN assets. I'll post back when I get an alpha release up for people to test. |
Here's one option: #138 Feel free to share any thoughts/suggestions. |
In my production build I'd like to add CDN prefix to URLs generated by
vite_asset
, so that/static/assets/app-[hash].js
will becomehttps://cdn.example.com/static/assets/app-[hash].js
.I tried to play with
static_url_prefix
config variable, but it seems to break manifest resolution.Is there some way to do this properly?
The text was updated successfully, but these errors were encountered: