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 cache-busted URLs for image assets #4128

Merged
merged 1 commit into from
Nov 29, 2016
Merged

Conversation

robertknight
Copy link
Member

Replace hard-coded '/asset' URL paths in templates with cache-busted
URLs generated from the site's static asset manifest.

  • Add asset_url helper for retrieving the cache-busted URL for an
    asset with a given path and use it in templates.

  • Fix missing entries in asset manifest by including subdirectories
    (eg. 'images/icons/*.svg') and all extensions (eg. sourcemaps)

See hypothesis/h-assets#21

Copy link
Contributor

@nickstenning nickstenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of comments to address!

@@ -147,3 +151,5 @@ def includeme(config):
# integration can be configured in app.py
config.registry['assets_env'] = assets_env
config.registry['assets_client_env'] = assets_client_env

config.add_request_method(assets_env.asset_url, 'asset_url')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These asset urls are used only in the templates so perhaps a neater way of exposing this function there would be to follow the pattern in app.py and add the asset_url function as a Jinja2 global.

return [asset_url(path) for path in bundles[bundle]]
return [self.asset_url(path) for path in bundles[bundle]]

def asset_url(self, path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is h.assets.Environment, so asset_url is possibly a bit redundant. The method above is called urls, so maybe call this one url?

"""
Return the cache-busted URL for an asset with a given path.
"""
manifest = self.manifest.load()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to be a little careful here, as previously we were doing one call to load() per bundle, and now we're doing one call to load per file (including when those files are referenced from a bundle).

The manifest is cached, sure, but this is still going to do at least one stat() call, which isn't ideal if we're doing it dozens of times during a single request. Especially in production, where we know the manifest isn't going to change, it might be nice to load it just once.

@@ -208,7 +208,7 @@ gulp.task('watch-images', function () {
gulp.watch(imageFiles, ['build-images']);
});

var MANIFEST_SOURCE_FILES = 'build/@(fonts|images|scripts|styles)/*.@(js|css|woff|jpg|png|svg)';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to already cover the relevant file extensions, so do we need to change it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't cover sourcemaps (.{js,css}.map) or subdirectories (images/icons/foo.svg)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!

Replace hard-coded '/asset' URL paths in templates with cache-busted
URLs generated from the site's static asset manifest.

 * Add `asset_url` helper for retrieving the cache-busted URL for an
   asset with a given path and use it in templates.

 * Fix missing entries in asset manifest by including subdirectories
   (eg. 'images/icons/*.svg') and all extensions (eg. sourcemaps)

See #4117
@codecov-io
Copy link

Current coverage is 83.54% (diff: 80.00%)

Merging #4128 into master will increase coverage by 0.13%

@@             master      hypothesis/h#4128   diff @@
==========================================
  Files           158        158          
  Lines          6167       6192    +25   
  Methods           0          0          
  Messages          0          0          
  Branches        698        704     +6   
==========================================
+ Hits           5144       5173    +29   
+ Misses          948        944     -4   
  Partials         75         75          

Powered by Codecov. Last update 425a14b...a98a39e

@nickstenning
Copy link
Contributor

Ok, changes LGTM. I'd like you to review the performance of the application when this goes out and ensure that all the extra stat() calls we'll be doing don't have an adverse effect.

@nickstenning nickstenning merged commit 23bc599 into master Nov 29, 2016
@nickstenning nickstenning deleted the cache-bust-image-urls branch November 29, 2016 08:44
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.

3 participants