Move html/session detecting into middleware