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

Use staticfiles.storage to generate URLs #52

Merged
merged 1 commit into from
May 22, 2023

Conversation

blighj
Copy link
Contributor

@blighj blighj commented Aug 17, 2022

For Vite production mode it seems like it would be useful to use django staticfiles.storage.staticfiles_storage.url method, if staticfiles is installed,
similar to how Django's own static template tag works. https://github.com/django/django/blob/0dd29209091280ccf34e07c9468746c396b7778e/django/templatetags/static.py

@ajslater
Copy link

ajslater commented Sep 19, 2022

This change should let people use the WHITENOISE_KEEP_ONLY_HASHED_FILES = True option if they like.

One suggestion: I'd change line 59 of this PR to:

DJANGO_VITE_STATIC_ROOT / staticfiles_storage.stored_name("manifest.json"),

as a sensible default.

@ajslater
Copy link

Sadly WHITENOISE_KEEP_ONLY_HASHED_FILES = True is only useful in the case where everything is one big main module. If you have chunks, then javascript will be generating those module links directly in the browser and it will have no access to the django staticfiles lookup.

However, even in that case a large amount of code, likely the majority, will still be contained in main.js and this patch would still give a speedup to the compressed main.<vitehash>.<whitenoisehash>.js.br

@blighj
Copy link
Contributor Author

blighj commented Dec 8, 2022

django 4.2 will add support for processing import statements in javascript files when using ManifestStaticFilesStorage.
This MR is now the last piece of the jigsaw, once django-vite supports staticfiles at the entry point, you will be able to set far future cache headers on all your javascript files without having to worry about cache busting issues when updating your code.

django.contrib.staticfiles

  • ManifestStaticFilesStorage now replaces paths to JavaScript modules in import and export statements with their hashed counterparts.

@thijskramer
Copy link
Collaborator

@blighj this looks very promising to me! Could you rebase your PR? Then I'll take a look in the next few days and try to merge this PR.

For Vite production mode it seems like it would be useful to use django staticfiles.storage.staticfiles_storage.url method, if staticfiles is installed,
similar to how Django's own static template tag works. https://github.com/django/django/blob/0dd29209091280ccf34e07c9468746c396b7778e/django/templatetags/static.py
@blighj
Copy link
Contributor Author

blighj commented May 14, 2023

@thijskramer After a bit of messing, I've rebased that now.

@devwaseem
Copy link

If you have chunks, then javascript will be generating those module links directly in the browser and it will have no access to the django staticfiles lookup.

I'm facing the same problem. I'm using s3 as static file storage and the URLs are not resolved correctly for dynamic imports. I guess this PR solves the problem.

@thijskramer thijskramer merged commit 0e8c9ff into MrBin99:master May 22, 2023
@asg86260
Copy link

Is there a planned next release to include this?

@thijskramer
Copy link
Collaborator

thijskramer commented May 24, 2023 via email

@petrprikryl
Copy link
Contributor

Btw. this will no longer work:

DJANGO_VITE_STATIC_URL_PREFIX = 'bundler'

Due https://github.com/MrBin99/django-vite/pull/52/files#diff-340facb9cc1393cad64ce6b5408114ea99338657f8386e4a0d99e97c4b01e2d8R459

urljoin("bundler", "entry.js")
>  'entry.js'
urljoin("bundler/", "entry.js")
> 'bundler/entry.js'

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

Successfully merging this pull request may close these issues.

6 participants