Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#29265 closed Cleanup/optimization (fixed)

Docs about static files should explain benefits of using {% static %} template tag

Reported by: Ryan Govostes Owned by: nobody
Component: Documentation Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The guide on managing static files says,

In your templates, either hardcode the url like /static/my_app/example.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files).

(Nit: The first "url" should be capitalized.)

The text alludes to but does not really explain that if you use a static files storage backend that inherits from ManifestStaticFilesStorage, the static template tag will use the name of the hashed file in production, which enables longer-lived caches.

Since the behavior is different in debug mode, I think many users will not even know about it, and fail to opt-in to the better caching behavior by hard-coding paths. (The files storage backend is turned on if the developer follows the WhiteNoise setup guide, or when using the Heroku app template, neither of which specifically calls out the need to use static.)

Change History (4)

comment:1 by Ryan Govostes, 7 years ago

I don't strongly see the reason to bring up the option of hardcoding the URLs at all, rather than only recommending the best practice.

comment:2 by Tim Graham, 7 years ago

Has patch: set
Triage Stage: UnreviewedAccepted

I agree there's no reason to mention hardcoding URLs. PR

comment:3 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In ba5f24e:

Fixed #29265 -- Removed the suggestion to hardcode static URLs.

comment:4 by Tim Graham <timograham@…>, 7 years ago

In a3007ecf:

[2.0.x] Fixed #29265 -- Removed the suggestion to hardcode static URLs.

Backport of ba5f24ea6491780bb245ed6f0e5f015f4defffe9 from master

Note: See TracTickets for help on using tickets.
Back to Top