Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions assets/javascripts/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ app.Router = class Router extends Events {
if (location.pathname === "/") {
if ((path = this.getInitialPathFromHash())) {
page.replace(path + location.search, null, true);
} else if ((path = this.getInitialPathFromCookie())) {
page.replace(path + location.search + location.hash, null, true);
}
}
}
Expand All @@ -191,14 +189,6 @@ app.Router = class Router extends Events {
} catch (error) {}
}

getInitialPathFromCookie() {
const path = Cookies.get("initial_path");
if (path) {
Cookies.expire("initial_path");
return path;
}
}

replaceHash(hash) {
page.replace(
location.pathname + location.search + (hash || ""),
Expand Down
32 changes: 2 additions & 30 deletions lib/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,6 @@ def service_worker_cache_name
end
end

def redirect_via_js(path)
response.set_cookie :initial_path, value: path, expires: Time.now + 15, path: '/'
redirect '/', 302
end

def supports_js_redirection?
modern_browser?(browser) && !memoized_cookies.empty?
end

# https://github.com/fnando/browser#detecting-modern-browsers
# https://github.com/fnando/browser/blob/v2.6.1/lib/browser/browser.rb
# This restores the old browser gem `#modern?` functionality as it was in 2.6.1
# It's possible this isn't even really needed any longer, these versions are quite old now
def modern_browser?(browser)
[
browser.webkit?,
browser.firefox? && browser.version.to_i >= 17,
browser.ie? && browser.version.to_i >= 9 && !browser.compatibility_view?,
browser.edge? && !browser.compatibility_view?,
browser.opera? && browser.version.to_i >= 12,
browser.firefox? && browser.device.tablet? && browser.platform.android? && b.version.to_i >= 14
].any?
end
end

before do
Expand Down Expand Up @@ -285,11 +262,8 @@ def modern_browser?(browser)

%w(settings offline about news help).each do |page|
get "/#{page}" do
if supports_js_redirection?
redirect_via_js "/#{page}"
else
redirect "/#/#{page}", 302
end
response.headers['Content-Security-Policy'] = settings.csp if settings.csp
erb :index
end
end

Expand Down Expand Up @@ -424,8 +398,6 @@ def modern_browser?(browser)
redirect "/#{doc}#{type}/#{query_string_for_redirection}"
elsif rest.length > 1 && rest.end_with?('/')
redirect "/#{doc}#{type}#{rest[0...-1]}#{query_string_for_redirection}"
elsif user_has_docs?(doc) && supports_js_redirection?
redirect_via_js(request.path)
else
response.headers['Content-Security-Policy'] = settings.csp if settings.csp
erb :other
Expand Down
32 changes: 10 additions & 22 deletions test/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,11 @@ def app
end

describe "/[static-page]" do
it "redirects to /#/[static-page] by default" do
%w(offline about news help).each do |page|
it "renders the app shell directly" do
%w(settings offline about news help).each do |page|
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal "https://example.org/#/#{page}", last_response['Location']
end
end

it "redirects via JS cookie when a cookie exists" do
%w(offline about news help).each do |page|
set_cookie('foo=bar')
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2F#{page}; path=/; expires=")
assert last_response.ok?
assert_nil last_response['Set-Cookie']
end
end
end
Expand Down Expand Up @@ -90,25 +80,23 @@ def app
assert last_response.ok?
end

it "redirects via JS cookie when the doc exists and is enabled" do
it "renders when the doc exists and is enabled" do
set_cookie('docs=html~5')
get '/html~5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7E5%2F; path=/; expires=")
assert last_response.ok?
assert_nil last_response['Set-Cookie']
end

it "renders when the doc exists, has no version in the path, and isn't enabled" do
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.ok?
end

it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do
it "renders when the doc exists, has no version in the path, and a version is enabled" do
set_cookie('docs=html~5')
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
assert last_response.redirect?
assert_equal 'https://example.org/', last_response['Location']
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
assert last_response.ok?
assert_nil last_response['Set-Cookie']
end

it "renders when the doc exists and is enabled, and the request is from Googlebot" do
Expand Down
Loading