Skip to content

Commit 57c19f3

Browse files
committed
Set content type to XML. Return 404 if no sitemap file
1 parent 57212e2 commit 57c19f3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

app/controllers/sitemaps_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ class SitemapsController < ApplicationController
33

44
def index
55
if TeSS::Config.feature['spaces'] && !current_space.default?
6-
render file: sitemap_base.join("#{current_space.host}/sitemap.xml"), layout: false
6+
sitemap_path = sitemap_base.join("#{current_space.host}/sitemap.xml")
77
else
8-
render file: sitemap_base.join('sitemap.xml'), layout: false
8+
sitemap_path = sitemap_base.join('sitemap.xml')
99
end
10+
11+
return head :not_found unless sitemap_path.exist?
12+
13+
render file: sitemap_path, layout: false, content_type: 'application/xml'
1014
end
1115

1216
private

test/controllers/sitemaps_controller_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class SitemapsControllerTest < ActionController::TestCase
99

1010
with_settings(feature: { spaces: false }) do
1111
get :index
12+
13+
assert_response :success
1214
end
1315

1416
urls = sitemap_urls
@@ -19,6 +21,8 @@ class SitemapsControllerTest < ActionController::TestCase
1921
test 'renders global sitemap for default space when spaces feature is enabled' do
2022
with_settings(feature: { spaces: true }) do
2123
get :index
24+
25+
assert_response :success
2226
end
2327

2428
urls = sitemap_urls
@@ -31,6 +35,8 @@ class SitemapsControllerTest < ActionController::TestCase
3135
with_settings(feature: { spaces: true }) do
3236
with_host(space.host) do
3337
get :index
38+
39+
assert_response :success
3440
end
3541
end
3642

@@ -39,6 +45,17 @@ class SitemapsControllerTest < ActionController::TestCase
3945
assert_includes urls, 'http://plants.mytess.training/about'
4046
end
4147

48+
test 'returns 404 if sitemap file does not exist' do
49+
space = spaces(:astro)
50+
with_settings(feature: { spaces: true }) do
51+
with_host(space.host) do
52+
get :index
53+
54+
assert_response :not_found
55+
end
56+
end
57+
end
58+
4259
private
4360

4461
def sitemap_urls

0 commit comments

Comments
 (0)