Last active
May 5, 2018 15:41
-
-
Save UsamaAshraf/91d296fae65e531d6a1460f514c6c234 to your computer and use it in GitHub Desktop.
A simple, custom Rails middleware class
This file contains 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
# app/middleware/service_worker_manager.rb | |
class ServiceWorkerManager | |
# We’ll pass 'service_workers' when we register this middleware. | |
def initialize(app, service_workers) | |
@app = app | |
@service_workers = service_workers | |
end | |
def call(env) | |
# Let the next middleware classes & app do their thing first… | |
status, headers, response = @app.call(env) | |
dont_cache = @service_workers.any? { |worker_name| env['REQUEST_PATH'].include?(worker_name) } | |
# …and modify the response if a service worker was fetched. | |
if dont_cache | |
headers['Cache-Control'] = 'no-cache' | |
end | |
[status, headers, response] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment