Skip to content

Commit 82265f5

Browse files
authored
Merge pull request #1479 from hydephp/add-sitemap-fallback-for-missing-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.
2 parents d1fdf50 + b79629e commit 82265f5

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

RELEASE_NOTES.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This serves two purposes:
1515

1616
### Changed
1717
- The `docs.sidebar.footer` config option now accepts a Markdown string to replace the default footer in https://github.com/hydephp/develop/pull/1477
18+
- 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
1819

1920
### Deprecated
2021
- for soon-to-be removed features.

packages/framework/src/Framework/Features/XmlGenerators/SitemapGenerator.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
use Hyde\Foundation\Facades\Routes;
1818
use Hyde\Framework\Concerns\TracksExecutionTime;
1919

20+
use function blank;
2021
use function filemtime;
2122
use function in_array;
2223
use function date;
2324
use function time;
25+
use function str_starts_with;
2426

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

60-
$this->addChild($urlItem, 'loc', Hyde::url($route->getOutputPath()));
62+
$this->addChild($urlItem, 'loc', $this->resolveRouteLink($route));
6163
$this->addChild($urlItem, 'lastmod', $this->getLastModDate($route->getSourcePath()));
6264
$this->addChild($urlItem, 'changefreq', 'daily');
6365

@@ -103,4 +105,18 @@ protected function getFormattedProcessingTime(): string
103105
{
104106
return (string) $this->getExecutionTimeInMs();
105107
}
108+
109+
protected function resolveRouteLink(Route $route): string
110+
{
111+
$baseUrl = Config::getNullableString('hyde.url');
112+
113+
if (blank($baseUrl) || str_starts_with($baseUrl, 'http://localhost')) {
114+
// While the sitemap spec requires a full URL, we rather fall back
115+
// to using relative links instead of using localhost links.
116+
117+
return $route->getLink();
118+
} else {
119+
return Hyde::url($route->getOutputPath());
120+
}
121+
}
106122
}

packages/framework/tests/Feature/Services/SitemapServiceTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,26 @@ public function test_all_route_types_are_discovered()
158158
copy(Hyde::vendorPath('resources/views/homepages/welcome.blade.php'), Hyde::path('_pages/index.blade.php'));
159159
copy(Hyde::vendorPath('resources/views/pages/404.blade.php'), Hyde::path('_pages/404.blade.php'));
160160
}
161+
162+
public function testLinksFallbackToRelativeLinksWhenASiteUrlIsNotSet()
163+
{
164+
config(['hyde.url' => null]);
165+
166+
$service = new SitemapGenerator();
167+
$service->generate();
168+
169+
$this->assertEquals('404.html', $service->getXmlElement()->url[0]->loc);
170+
$this->assertEquals('index.html', $service->getXmlElement()->url[1]->loc);
171+
}
172+
173+
public function testLinksFallbackToRelativeLinksWhenSiteUrlIsLocalhost()
174+
{
175+
config(['hyde.url' => 'http://localhost']);
176+
177+
$service = new SitemapGenerator();
178+
$service->generate();
179+
180+
$this->assertEquals('404.html', $service->getXmlElement()->url[0]->loc);
181+
$this->assertEquals('index.html', $service->getXmlElement()->url[1]->loc);
182+
}
161183
}

0 commit comments

Comments
 (0)