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.
  • Loading branch information
caendesilva committed Nov 27, 2023
2 parents d1fdf50 + b79629e commit 82265f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Expand Up @@ -15,6 +15,7 @@ This serves two purposes:

### Changed
- The `docs.sidebar.footer` config option now accepts a Markdown string to replace the default footer in https://github.com/hydephp/develop/pull/1477
- Links in the `sitemap.xml` file are now relative when a site URL is not set, instead of using the default localhost URL in https://github.com/hydephp/develop/pull/1479

### Deprecated
- for soon-to-be removed features.
Expand Down
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 packages/framework/tests/Feature/Services/SitemapServiceTest.php
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 82265f5

Please sign in to comment.