lib/seo_cache/middleware.rb in seo_cache-0.13.0 vs lib/seo_cache/middleware.rb in seo_cache-0.14.0

- old
+ new

@@ -31,22 +31,26 @@ return response.finish end else Thread.new do prerender_data = page_render(env) - # Extract status from render page - status = prerender_data&.scan(/<!--status:(\d+)-->/)&.last&.first + # Extract status from render page (return 500 if status cannot be found, some problems happen somewhere) + status = prerender_data&.scan(/<!--status:(\d+)-->/)&.last&.first || 500 after_render(env, prerender_data, status || 200) end end elsif prerender_params?(env) env['seo_mode'] = true # Add status to render page because Selenium doesn't return http headers or status... status, headers, response = @app.call(env) status_code = "<!--status:#{status}-->" # Cannot add at the top of file, Chrome removes leading comments... - body_code = response.body.sub('<head>', "<head>#{status_code}") - return [status, headers, [body_code]] + begin + body_code = response.body.sub('<head>', "<head>#{status_code}") + return [status, headers, [body_code]] + rescue + return [status, headers, [nil]] + end end return @app.call(env) end