Skip to content

Commit

Permalink
Merge pull request #1479 from hydephp/add-sitemap-fallback-for-missin…
Browse files Browse the repository at this point in the history
…g-site-url

Links in the sitemap.xml file are now relative when a site URL is not set, instead of using the default localhost URL. hydephp/develop@82265f5
  • Loading branch information
github-actions committed Nov 27, 2023
1 parent cdb2b01 commit 8943332
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Framework/Features/XmlGenerators/SitemapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use Hyde\Foundation\Facades\Routes;
use Hyde\Framework\Concerns\TracksExecutionTime;

use function blank;
use function filemtime;
use function in_array;
use function date;
use function time;
use function str_starts_with;

/**
* @see https://www.sitemaps.org/protocol.html
Expand Down Expand Up @@ -57,7 +59,7 @@ protected function addRoute(Route $route): void
{
$urlItem = $this->xmlElement->addChild('url');

$this->addChild($urlItem, 'loc', Hyde::url($route->getOutputPath()));
$this->addChild($urlItem, 'loc', $this->resolveRouteLink($route));
$this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath()));
$this->addChild($urlItem, 'changefreq', 'daily');

Expand Down Expand Up @@ -103,4 +105,18 @@ protected function getFormattedProcessingTime(): string
{
return (string) $this->getExecutionTimeInMs();
}

protected function resolveRouteLink(Route $route): string
{
$baseUrl = Config::getNullableString('hyde.url');

if (blank($baseUrl) || str_starts_with($baseUrl, 'http://localhost')) {
// While the sitemap spec requires a full URL, we rather fall back
// to using relative links instead of using localhost links.

return $route->getLink();
} else {
return Hyde::url($route->getOutputPath());
}
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Services/SitemapServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,26 @@ public function test_all_route_types_are_discovered()
copy(Hyde::vendorPath('resources/views/homepages/welcome.blade.php'), Hyde::path('_pages/index.blade.php'));
copy(Hyde::vendorPath('resources/views/pages/404.blade.php'), Hyde::path('_pages/404.blade.php'));
}

public function testLinksFallbackToRelativeLinksWhenASiteUrlIsNotSet()
{
config(['hyde.url' => null]);

$service = new SitemapGenerator();
$service->generate();

$this->assertEquals('404.html', $service->getXmlElement()->url[0]->loc);
$this->assertEquals('index.html', $service->getXmlElement()->url[1]->loc);
}

public function testLinksFallbackToRelativeLinksWhenSiteUrlIsLocalhost()
{
config(['hyde.url' => 'http://localhost']);

$service = new SitemapGenerator();
$service->generate();

$this->assertEquals('404.html', $service->getXmlElement()->url[0]->loc);
$this->assertEquals('index.html', $service->getXmlElement()->url[1]->loc);
}
}

0 comments on commit 8943332

Please sign in to comment.