diff --git a/app/controllers/sitemap_controller.rb b/app/controllers/sitemap_controller.rb new file mode 100644 index 0000000000..f5ce44ceca --- /dev/null +++ b/app/controllers/sitemap_controller.rb @@ -0,0 +1,11 @@ +class SitemapController < ApplicationController + layout nil + + def index + headers['Content-Type'] = 'application/xml' + @pages = ['shops', 'map', 'producers', 'groups'] + @enterprises = Enterprise.is_hub + @groups = EnterpriseGroup.all + respond_to :xml + end +end diff --git a/app/views/sitemap/index.xml.haml b/app/views/sitemap/index.xml.haml new file mode 100644 index 0000000000..3c07cc6ebc --- /dev/null +++ b/app/views/sitemap/index.xml.haml @@ -0,0 +1,18 @@ +!!! XML +%urlset{xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"} + - for page in @pages + %url + %loc= root_url + page + %changefreq monthly + + - for enterprise in @enterprises + %url + %loc= enterprise_shop_url(enterprise) + %lastmod= enterprise.updated_at.strftime('%Y-%m-%d') + %changefreq weekly + + - for group in @groups + %url + %loc= group_url(group) + %lastmod= enterprise.updated_at.strftime('%Y-%m-%d') + %changefreq yearly diff --git a/config/routes.rb b/config/routes.rb index 69319ae84a..607e6308a2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -169,6 +169,8 @@ Openfoodnetwork::Application.routes.draw do end end + get 'sitemap.xml', to: 'sitemap#index', defaults: { format: 'xml' } + # Mount Spree's routes mount Spree::Core::Engine, :at => '/' diff --git a/spec/features/consumer/sitemap_spec.rb b/spec/features/consumer/sitemap_spec.rb new file mode 100644 index 0000000000..6ec913de32 --- /dev/null +++ b/spec/features/consumer/sitemap_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' + +feature 'sitemap' do + let(:enterprise) { create(:distributor_enterprise) } + let!(:group) { create(:enterprise_group, enterprises: [enterprise], on_front_page: true) } + + it "renders sitemap" do + visit '/sitemap.xml' + expect(page).to have_content enterprise_shop_url(enterprise) + expect(page).to have_content group_url(group) + end +end