Class: Abqari::Server::NotFoundFileHandler

Inherits:
WEBrick::HTTPServlet::FileHandler
  • Object
show all
Defined in:
lib/abqari/server.rb

Overview

WEBrick's stock FileHandler, with one change: an unknown URL serves the site's own /404.html (when the build produced one) instead of WEBrick's default error page. Static hosts — Cloudflare Pages, Netlify, GitHub Pages — all do this in production, so without it the 404 page is the one page an author can never preview with bin/serve. The status stays 404; only the body changes.

Instance Method Summary collapse

Instance Method Details

#service(req, res) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/abqari/server.rb', line 77

def service(req, res)
  super
rescue WEBrick::HTTPStatus::NotFound
  custom = File.join(@root, '404.html')
  raise unless File.file?(custom)

  res.status = 404
  res.content_type = 'text/html; charset=utf-8'
  res.body = File.read(custom)
end