Current state with latest updates
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Services\Sitemaps\PublishedSitemapResolver;
|
||||
use App\Services\Sitemaps\SitemapBuildService;
|
||||
use App\Services\Sitemaps\SitemapXmlRenderer;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
@@ -14,34 +15,38 @@ final class SitemapController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly PublishedSitemapResolver $published,
|
||||
private readonly SitemapBuildService $build,
|
||||
private readonly SitemapXmlRenderer $renderer,
|
||||
) {
|
||||
}
|
||||
|
||||
public function index(): Response|BinaryFileResponse
|
||||
{
|
||||
// 1. Static file written by the build/generate commands.
|
||||
// On production nginx serves this directly via try_files without reaching PHP.
|
||||
// On dev / misconfigured servers we stream it with sendfile — no RAM load.
|
||||
$path = public_path('sitemap.xml');
|
||||
if (file_exists($path)) {
|
||||
return $this->xmlFileResponse($path);
|
||||
}
|
||||
|
||||
// 2. Published release (release management pipeline fallback).
|
||||
// 1. Published release (release management pipeline fallback).
|
||||
$published = $this->published->resolveIndex();
|
||||
if ($published !== null) {
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
|
||||
// 2. Live-build fallback when no published sitemap is available.
|
||||
if ((bool) config('sitemaps.delivery.fallback_to_live_build', true)) {
|
||||
$built = $this->build->buildIndex(force: true, persist: false);
|
||||
|
||||
return $this->renderer->xmlResponse($built['content']);
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
public function show(string $name): Response|BinaryFileResponse
|
||||
{
|
||||
if ($name === 'sitemap') {
|
||||
return $this->index();
|
||||
}
|
||||
|
||||
// 1. Static file.
|
||||
$path = public_path('sitemaps/' . $name . '.xml');
|
||||
if (file_exists($path)) {
|
||||
if ((bool) config('sitemaps.pre_generated.enabled', true) && file_exists($path)) {
|
||||
return $this->xmlFileResponse($path);
|
||||
}
|
||||
|
||||
@@ -51,6 +56,13 @@ final class SitemapController extends Controller
|
||||
return $this->renderer->xmlResponse($published['content']);
|
||||
}
|
||||
|
||||
if ((bool) config('sitemaps.delivery.fallback_to_live_build', true)) {
|
||||
$built = $this->build->buildNamed($name, force: true, persist: false);
|
||||
if ($built !== null) {
|
||||
return $this->renderer->xmlResponse($built['content']);
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
@@ -61,4 +73,4 @@ final class SitemapController extends Controller
|
||||
'Cache-Control' => 'public, max-age=' . max(60, (int) config('sitemaps.cache_ttl_seconds', 900)),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user