koo*_*osa 5 rack ruby-on-rails heroku
我管理的一个网站不断收到来自旧版本网站的对不再存在的 JavaScript 文件的请求。这些请求占用大量资源,因为它们每次都会通过 Rails 路由以返回 404。我认为让 Rack 处理该特定 URL 并返回 404 本身会更好。那是对的吗?如果是这样,我该如何设置?
我一直在查看这篇博客文章,我认为这是一种前进的方式(即从某些现有的 Rack 模块继承):
所以我最终编写了自己的一点中间件:
module Rack
class NotFoundUrls
def initialize(app, exclude)
@app = app
@exclude = exclude
end
def call(env)
status, headers, response = @app.call(env)
req = Rack::Request.new(env)
return [status, headers, response] if !@exclude.include?(URI.unescape(req.fullpath))
content = 'Not Found'
[404, {'Content-Type' => 'text/html', 'Content-Length' => content.size.to_s}, [content]]
end
end
end
Run Code Online (Sandbox Code Playgroud)
然后将其添加到 config.ru 文件中:
use Rack::NotFoundUrls, ['/javascripts/some.old.file.js']
Run Code Online (Sandbox Code Playgroud)
这是我第一次这样做,如果有任何明显的错误,请告诉我......
| 归档时间: |
|
| 查看次数: |
3121 次 |
| 最近记录: |