Files
usher-manage-stack/app/Services/SiteRevalidationService.php

61 lines
1.5 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class SiteRevalidationService
{
/**
* Revalidate article cache on the Next.js frontend.
*/
public static function revalidateArticle(?string $slug = null): void
{
static::revalidate('article', $slug);
}
/**
* Revalidate page cache on the Next.js frontend.
*/
public static function revalidatePage(?string $slug = null): void
{
static::revalidate('page', $slug);
}
/**
* Revalidate document cache on the Next.js frontend.
*/
public static function revalidateDocument(?string $slug = null): void
{
static::revalidate('document', $slug);
}
private static function revalidate(string $type, ?string $slug = null): void
{
if (app()->runningUnitTests()) {
return;
}
$url = config('services.nextjs.revalidate_url');
$token = config('services.nextjs.revalidate_token');
if (! $url || ! $token) {
return;
}
try {
$payload = ['type' => $type];
if ($slug) {
$payload['slug'] = $slug;
}
Http::timeout(5)
->withHeaders(['x-revalidate-token' => $token])
->post($url, $payload);
} catch (\Throwable $e) {
Log::warning("Site revalidation failed for {$type}/{$slug}: {$e->getMessage()}");
}
}
}