Skip to content

Allow for a query string to be added to local CSS/JS, for cache busting #11133

@chrisjsewell

Description

@chrisjsewell

Is your feature request related to a problem? Please describe.

When loading CSS or JS (via link/script tags) the browser will generally cache files, based on their file name.

This is a problem for updating CSS/JS files that are specified by html_css_files/html_js_files, or by Sphinx.add_css_files()/ Sphinx.add_js_files(),
since changes to the content of such files will not be seen by viewers of the website, if they have previously loaded the page.

Describe the solution you'd like

A common practice, to "cache bust" is to add a query string after the path

<link rel="stylesheet" href="style.css?v=3.4.1">

Indeed, this is what e.g. the furo theme does, via an html-context-page event, using the hash of the files: https://github.com/pradyunsg/furo/blob/4909c419ecb99308b6d46bc1e420be2980ba75ef/src/furo/__init__.py#L164 (cc @pradyunsg)

but it would be nice to not have to get so "low level" for basic use cases.

Ideally, sphinx itself would automatically compute the hash of these files and append the requisite query string.

Or at least an option would be available on the methods, like:

def setup(app):
	app.add_css_files("custom.css", query=f"v={version}")

Describe alternatives you've considered

One could change the file name, every time the content changes, but this is not very maintainable and leads e.g. to issues around merge conflicts when using git.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions