Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve search and hash in redirect #127

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions lib/jekyll-redirect-from/redirect_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,19 @@ def generate_redirect_content(item_url)
<meta charset="utf-8">
<title>Redirecting…</title>
<link rel="canonical" href="#{item_url}">
<meta http-equiv="refresh" content="0; url=#{item_url}">
<meta http-equiv="refresh" content="1; url=#{item_url}">
<h1>Redirecting…</h1>
<a href="#{item_url}">Click here if you are not redirected.</a>
<script>location="#{item_url}"</script>
<script>
var url = '#{item_url}';
if (location.search && url.indexOf('?') === -1) {
url = url.replace(/($|#)/, location.search + '$1');
}
if (location.hash && url.indexOf('#') === -1) {
url += location.hash;
}
location=url;
</script>
</html>
EOF
end
Expand Down
10 changes: 7 additions & 3 deletions spec/jekyll_redirect_from/redirect_page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@

context "#generate_redirect_content" do
it "sets the #content to the generated refresh page" do
expect(page_content).to eq("<!DOCTYPE html>\n<html lang=\"en-US\">\n<meta charset=\"utf-8\">\n<title>Redirecting…</title>\n<link rel=\"canonical\" href=\"#{item_url}\">\n<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">\n<h1>Redirecting…</h1>\n<a href=\"#{item_url}\">Click here if you are not redirected.</a>\n<script>location=\"#{item_url}\"</script>\n</html>\n")
expect(page_content).to eq("<!DOCTYPE html>\n<html lang=\"en-US\">\n<meta charset=\"utf-8\">\n<title>Redirecting…</title>\n<link rel=\"canonical\" href=\"#{item_url}\">\n<meta http-equiv=\"refresh\" content=\"1; url=#{item_url}\">\n<h1>Redirecting…</h1>\n<a href=\"#{item_url}\">Click here if you are not redirected.</a>\n<script>\n var url = '#{item_url}';\n if (location.search && url.indexOf('?') === -1) {\n url = url.replace(/($|#)/, location.search + '$1');\n }\n if (location.hash && url.indexOf('#') === -1) {\n url += location.hash; \n }\n location=url;\n</script>\n</html>\n")
end

it "contains the meta refresh tag" do
expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"0; url=#{item_url}\">")
expect(page_content).to include("<meta http-equiv=\"refresh\" content=\"1; url=#{item_url}\">")
end

it "contains the url" do
expect(page_content).to include("var url = 'http://jekyllrb.com/2014/01/03/moving-to-jekyll.md';")
end

it "contains JavaScript redirect" do
expect(page_content).to include("location=\"http://jekyllrb.com/2014/01/03/moving-to-jekyll.md\"")
expect(page_content).to include("location=url;")
end

it "contains canonical link in header" do
Expand Down
10 changes: 5 additions & 5 deletions spec/jekyll_redirect_from/redirector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,23 @@

it "generates the refresh page for the collection with one redirect_to url" do
expect(dest_dir("articles", "redirect-somewhere-else-plz.html")).to exist
expect(dest_dir("articles", "redirect-somewhere-else-plz.html").read).to include(%|<meta http-equiv="refresh" content="0; url=http://www.zombo.com">|)
expect(dest_dir("articles", "redirect-somewhere-else-plz.html").read).to include(%|<meta http-equiv="refresh" content="1; url=http://www.zombo.com">|)
end

it "generates the refresh page for the collection with one redirect_to url and a permalink" do
expect(dest_dir("tags", "our projects", "index")).not_to exist
expect(dest_dir("tags", "our projects", "index.html")).to exist
expect(dest_dir("tags", "our projects", "index.html").read).to include(%|<meta http-equiv="refresh" content="0; url=/tags/our-projects/">|)
expect(dest_dir("tags", "our projects", "index.html").read).to include(%|<meta http-equiv="refresh" content="1; url=/tags/our-projects/">|)
end

it "generates the refresh page for the page with one redirect_to url" do
expect(dest_dir("one_redirect_to.html")).to exist
expect(dest_dir("one_redirect_to.html").read).to include(%|<meta http-equiv="refresh" content="0; url=https://www.github.com">|)
expect(dest_dir("one_redirect_to.html").read).to include(%|<meta http-equiv="refresh" content="1; url=https://www.github.com">|)
end

it "generates the refresh page for the page with multiple redirect_to urls" do
expect(dest_dir("multiple_redirect_tos.html")).to exist
expect(dest_dir("multiple_redirect_tos.html").read).to include(%|<meta http-equiv="refresh" content="0; url=https://www.jekyllrb.com">|)
expect(dest_dir("multiple_redirect_tos.html").read).to include(%|<meta http-equiv="refresh" content="1; url=https://www.jekyllrb.com">|)
end

it "does not include any default layout" do
Expand All @@ -101,7 +101,7 @@
it "generates the refresh page for the page with one redirect_to url and a permalink" do
expect(dest_dir("tags", "how we work", "index")).not_to exist
expect(dest_dir("tags", "how we work", "index.html")).to exist
expect(dest_dir("tags", "how we work", "index.html").read).to include(%|<meta http-equiv="refresh" content="0; url=/tags/how-we-work/">|)
expect(dest_dir("tags", "how we work", "index.html").read).to include(%|<meta http-equiv="refresh" content="1; url=/tags/how-we-work/">|)
end
end

Expand Down