Skip to content

Instantly share code, notes, and snippets.

@shawndeprey
Created July 30, 2014 16:46
Show Gist options
  • Save shawndeprey/01347d1dd1dfec9b68f2 to your computer and use it in GitHub Desktop.
Save shawndeprey/01347d1dd1dfec9b68f2 to your computer and use it in GitHub Desktop.
A Helper to Serve Gzip Assets
module GzipHelper
def gzip_javascript_include_tag(asset)
# Grab the asset html include tag
tag = javascript_include_tag asset
# If we are in production and the requesting client accepts gzip encoding, swap for the gzip asset
if Rails.env.production? && request.accept_encoding =~ /gzip/i
tag = tag.gsub(/\.js/i, ".js.gz")
end
# Return the asset whether or not it was modified
tag.html_safe
end
def gzip_stylesheet_link_tag(asset)
# Grab the asset html include tag
tag = stylesheet_link_tag asset
# If we are in production and the requesting client accepts gzip encoding, swap for the gzip asset
if Rails.env.production? && request.accept_encoding =~ /gzip/i
tag = tag.gsub(/\.css/i, ".css.gz")
end
# Return the asset whether or not it was modified
tag.html_safe
end
end
@wizlif
Copy link

wizlif commented Mar 20, 2019

How can i use this helper with the turbo links option

@bmorrall
Copy link

bmorrall commented Apr 29, 2020

You could probably modify the methods to pass turbolinks instructions onto the javascript/stylesheet link tag methods,

e.g:

def gzip_javascript_include_tag(asset, *args)
    # Grab the asset html include tag
    tag = javascript_include_tag asset, *args
    ...

Then you can use the method in the same way you'd use a normal javascript/stylesheet tag, just include the same turboscript arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment