Add content hash to JS includes (closes #2714)#2745
Open
mvanhorn wants to merge 1 commit into
Open
Conversation
The CSS link in base.html already carries `?{{ app_css_hash }}` so that
browsers refetch when the bundled file changes. The five first-party JS
files shipped with datasette did not. Cache-busting JS the same way
matches the existing CSS pattern and uses the static_hash() helper that
already powers app_css_hash().
Files updated:
- datasette/app.py: expose static_hash as a callable in template context.
- datasette/handle_exception.py: include static_hash in the error-page
template context (mirrors the existing app_css_hash entry there).
- datasette/templates/base.html: hash datasette-manager.js and
navigation-search.js.
- datasette/templates/table.html: hash column-chooser.js, table.js, and
mobile-column-actions.js.
- tests/test_html.py: new test_js_content_hash parametrized across all
five files; existing test_navigation_menu_links updated to expect the
new query string.
Vendored libraries (cm-editor-6.0.1.bundle.js, sql-formatter-2.3.3.min.js,
json-format-highlight-1.0.1.js) already carry a version in the filename
and were left unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
?{{ static_hash('foo.js') }}to the five first-party JS includes shipped with datasette so browsers refetch when the bundled file changes, matching the existing pattern used forapp.css.Vendored libraries (
cm-editor-6.0.1.bundle.js,sql-formatter-2.3.3.min.js,json-format-highlight-1.0.1.js) already carry a version in the filename and were left alone.Closes #2714
Approach
Datasette.static_hash(filename)already exists (it's whatapp_css_hashcalls under the hood). The change exposes it as a callable in the template context so templates can reuse the same SHA-1-prefix scheme asapp.csswithout hardcoding per-file context entries.handle_exception.pyalso getsstatic_hashadded to the error-page template context, the same way it currently passesapp_css_hash.Files changed
datasette/app.py— exposestatic_hashcallable.datasette/handle_exception.py— include it in the error template context.datasette/templates/base.html— hashdatasette-manager.jsandnavigation-search.js.datasette/templates/table.html— hashcolumn-chooser.js,table.js,mobile-column-actions.js.Tests
test_js_content_hashparametrized across all 5 files, asserts the rendered<script src>includes the expected?hashsuffix.test_navigation_menu_linksupdated to expect the new query string onnavigation-search.js.tests/test_html.py+tests/test_table_html.pypass locally (212 passed, 1 xpassed).