Skip to content

Commit

Permalink
Rework JS static site code structure
Browse files Browse the repository at this point in the history
This splits up the generator class as was a problem suspected in this PR
review:

#15640 (review)

It creates a single class for a Browser which extends the
capybara-webkit browser to include a way to get a page's contents after
jQuery's AJAX calls are done.

This is in a StaticSite module just to namespace it and indicate where
it's used in the application, rather than it's nature. In other words
it's not a _static site browser_ but you could read it that way so maybe
this needs changing?

All the remaining procedural logic is pushed into the Rake task. From
here we could then extract that if we see fit.
  • Loading branch information
henare committed Jan 26, 2018
1 parent 9de377d commit f94f6bf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 63 deletions.
15 changes: 12 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ end
require 'bundler/audit/task'
Bundler::Audit::Task.new

require_relative 'lib/static_site_generator.rb'
require_relative 'lib/static_site/browser.rb'
require 'uri'
require 'pathname'
desc 'Build static site pages that rely on JavaScript'
task :generate_static_site_javascript_pages, [:base_url] do |_, args|
base_url = args.fetch(:base_url, 'http://localhost:4567')
javascript_pages_to_scrape = ["#{base_url}/needed.html"]
StaticSiteGenerator.new(urls: javascript_pages_to_scrape).build
pages_to_scrape = ["#{base_url}/needed.html"].map { |u| URI.parse(u) }
browser = StaticSite::Browser.new

pages_to_scrape.each do |url|
browser.visit(url)
file = Pathname.new(url.path[1..-1]).sub_ext('.html')
file.dirname.mkpath
file.write(browser.body_after_jquery_ajax)
end
end

task default: ['test', 'rubocop', 'bundle:audit']
31 changes: 31 additions & 0 deletions lib/static_site/browser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'capybara-webkit'

module StaticSite
class Browser < Capybara::Webkit::Browser
JQUERY_WAIT_TIME = 2

def initialize
super(Capybara::Webkit::Connection.new(server: Capybara::Webkit::Server.new))
allow_unknown_urls
end

def body_after_jquery_ajax
wait_for_jquery_ajax
restore_pre_js_page_classes
body
end

def wait_for_jquery_ajax
Timeout.timeout(JQUERY_WAIT_TIME) do
# Errors if jQuery isn't on the page - should we check for that?
loop until evaluate_script('jQuery.active').zero?
end
end

# Restores page classes modified by running JS on the page
def restore_pre_js_page_classes
execute_script("$('html').addClass('no-js')")
execute_script("$('html').removeClass('flexwrap')")
end
end
end
60 changes: 0 additions & 60 deletions lib/static_site_generator.rb

This file was deleted.

0 comments on commit f94f6bf

Please sign in to comment.