Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sitemap to use relative links instead of localhost when missing site URL #1479

Merged
merged 7 commits into from
Nov 27, 2023
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
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
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 packages/framework/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);
}
}
Loading