Skip to content

Commit

Permalink
Simplify routes
Browse files Browse the repository at this point in the history
  • Loading branch information
robdekort committed May 2, 2024
1 parent 9002d22 commit 7108d35
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 115 deletions.
92 changes: 48 additions & 44 deletions resources/views/sitemap/sitemap.antlers.xml
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
{{ xml_header }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" {{ yield:namespace }}>
{{ seo:sitemap_collections }}
{{ collection from="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results where (x => x.permalink !== null) }}
{{ if permalink }}
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ updated_at format="Y-m-d"}}</lastmod>
<changefreq>{{ sitemap_change_frequency ? sitemap_change_frequency : 'weekly' }}</changefreq>
<priority>{{ sitemap_priority ? sitemap_priority : '0.5' }}</priority>
{{ if {locales:count} > 1 }}
{{ once }}
{{ section:namespace}}
xmlns:xhtml="http://www.w3.org/1999/xhtml"
{{ /section:namespace}}
{{ /once }}
{{ locales }}
<xhtml:link rel="alternate" hreflang="{{ locale:full }}" href="{{ permalink }}"/>
{{ /locales }}
{{ /if }}
</url>
{{ /if }}
{{ /results }}
{{ /collection }}
{{ /seo:sitemap_collections }}
{{ seo:sitemap_taxonomies }}
{{ taxonomy from="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results where (x => x.permalink !== null) }}
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ updated_at format="Y-m-d"}}</lastmod>
<changefreq>{{ sitemap_change_frequency ? sitemap_change_frequency : 'weekly' }}</changefreq>
<priority>{{ sitemap_priority ? sitemap_priority : '0.5' }}</priority>
</url>
{{ /results }}
{{ /taxonomy }}
{{ /seo:sitemap_taxonomies }}
{{ seo:sitemap_collection_taxonomies }}
{{ collections }}
{{ taxonomy from="{taxonomy}" collection="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results }}
{{ unless seo:use_sitemap }}
{{ 404 }}
{{ else }}
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" {{ yield:namespace }}>
{{ seo:sitemap_collections }}
{{ collection from="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results where (x => x.permalink !== null) }}
{{ if permalink }}
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ updated_at format="Y-m-d"}}</lastmod>
<changefreq>{{ sitemap_change_frequency ? sitemap_change_frequency : 'weekly' }}</changefreq>
<priority>{{ sitemap_priority ? sitemap_priority : '0.5' }}</priority>
{{ if {locales:count} > 1 }}
{{ once }}
{{ section:namespace}}
xmlns:xhtml="http://www.w3.org/1999/xhtml"
{{ /section:namespace}}
{{ /once }}
{{ locales }}
<xhtml:link rel="alternate" hreflang="{{ locale:full }}" href="{{ permalink }}"/>
{{ /locales }}
{{ /if }}
</url>
{{ /if }}
{{ /results }}
{{ /collection }}
{{ /seo:sitemap_collections }}
{{ seo:sitemap_taxonomies }}
{{ taxonomy from="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results where (x => x.permalink !== null) }}
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ updated_at format="Y-m-d"}}</lastmod>
Expand All @@ -48,6 +38,20 @@
</url>
{{ /results }}
{{ /taxonomy }}
{{ /collections }}
{{ /seo:sitemap_collection_taxonomies }}
</urlset>
{{ /seo:sitemap_taxonomies }}
{{ seo:sitemap_collection_taxonomies }}
{{ collections }}
{{ taxonomy from="{taxonomy}" collection="{handle}" seo_noindex:isnt="true" as="results" }}
{{ results }}
<url>
<loc>{{ permalink }}</loc>
<lastmod>{{ updated_at format="Y-m-d"}}</lastmod>
<changefreq>{{ sitemap_change_frequency ? sitemap_change_frequency : 'weekly' }}</changefreq>
<priority>{{ sitemap_priority ? sitemap_priority : '0.5' }}</priority>
</url>
{{ /results }}
{{ /taxonomy }}
{{ /collections }}
{{ /seo:sitemap_collection_taxonomies }}
</urlset>
{{ /unless }}
18 changes: 11 additions & 7 deletions resources/views/sitemap/sitemaps.antlers.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{{ xml_header }}
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ sites }}
<sitemap>
<loc>{{ permalink | ensure_right('/') }}sitemap.xml</loc>
</sitemap>
{{ /sites }}
</sitemapindex>
{{ unless seo:use_sitemap }}
{{ 404 }}
{{ else }}
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ sites }}
<sitemap>
<loc>{{ permalink | ensure_right('/') }}sitemap.xml</loc>
</sitemap>
{{ /sites }}
</sitemapindex>
{{ /unless }}
50 changes: 27 additions & 23 deletions resources/views/social_images.antlers.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
@desc The template for generating Social Images. By default it includes the size for the OG protocol.
#}}

<!-- statamic-peak-seo::social_images.antlers.html -->
<!doctype html>
<html class="h-full">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="robots" content="noindex">
<title>Social Images - {{ get_content:id }}{{ title }}{{ /get_content:id }}</title>
{{ vite src="resources/css/site.css" }}
<style>
.phpdebugbar { display: none !important; }
</style>
</head>
{{ unless seo:use_social_image_generation }}
{{ 404 }}
{{ else }}
<!-- statamic-peak-seo::social_images.antlers.html -->
<!doctype html>
<html class="h-full">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="robots" content="noindex">
<title>Social Images - {{ get_content:id }}{{ title }}{{ /get_content:id }}</title>
{{ vite src="resources/css/site.css" }}
<style>
.phpdebugbar { display: none !important; }
</style>
</head>
<body class="py-8 flex flex-col items-center space-y-8">
{{# The OG template. #}}
<div>
<h2 class="mb-4 text-xs text-neutral font-bold uppercase tracking-widest">OG</h2>
{{ partial:statamic-peak-seo::components/social_image selector="og" style="width: 1200px; height: 630px" }}
</div>
<body class="py-8 flex flex-col items-center space-y-8">
{{# The OG template. #}}
<div>
<h2 class="mb-4 text-xs text-neutral font-bold uppercase tracking-widest">OG</h2>
{{ partial:statamic-peak-seo::components/social_image selector="og" style="width: 1200px; height: 630px" }}
</div>
{{# Repeat for future OG sizes. RIP Twitter. #}}
</body>
</html>
<!-- End: statamic-peak-seo::social_images.antlers.html -->
{{# Repeat for future OG sizes. RIP Twitter. #}}
</body>
</html>
<!-- End: statamic-peak-seo::social_images.antlers.html -->
{{ /unless }}
61 changes: 20 additions & 41 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,26 @@
<?php

use Illuminate\Support\Facades\Route;
use Statamic\Exceptions\NotFoundHttpException;
use Statamic\Facades\GlobalSet;
use Statamic\Facades\Site;
use Statamic\Facades\URL;

// The Sitemap Index route for listing sitemaps of all (multi)sites.
Route::statamic('/sitemaps.xml', 'statamic-peak-seo::sitemap/sitemaps', function () {
if (! GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_sitemap')) {
throw new NotFoundHttpException;
}

return [
'layout' => null,
'content_type' => 'application/xml',
];
});

// Register routes for each site.
Site::all()->each(function (\Statamic\Sites\Site $site) {
$relativeSiteUrl = URL::makeRelative($site->url());

// The Sitemap route.
Route::statamic(URL::tidy($relativeSiteUrl.'/sitemap.xml'), 'statamic-peak-seo::sitemap/sitemap', function () {
if (! GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_sitemap')) {
throw new NotFoundHttpException;
}

return [
'layout' => null,
'content_type' => 'application/xml',
];
});

// The Social Image route to generate social images.
Route::statamic(URL::tidy($relativeSiteUrl.'/social-images/{id}'), 'statamic-peak-seo::social_images', function ($id) {
if (! GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_social_image_generation')) {
throw new NotFoundHttpException;
}

return [
'layout' => null,
];
});
});
Route::statamic(URL::tidy('/sitemaps.xml'), 'statamic-peak-seo::sitemap/sitemaps', [
'layout' => null,
'content_type' => 'application/xml',
]);

// The Sitemap routes.
Route::statamic(URL::tidy('sitemap.xml'), 'statamic-peak-seo::sitemap/sitemap', [
'layout' => null,
'content_type' => 'application/xml',
]);

Route::statamic(URL::tidy('{current_site}/sitemap.xml'), 'statamic-peak-seo::sitemap/sitemap', [
'layout' => null,
'content_type' => 'application/xml',
]);

// The Social Image route to generate social images.
Route::statamic(URL::tidy('{current_site}/social-images/{id}'), 'statamic-peak-seo::social_images', [
'layout' => null,
]);

0 comments on commit 7108d35

Please sign in to comment.