feat(seo): 사이트맵 증분 저장소 + 리소스 리스너 재배선

사이트맵을 리소스 단위로 영속화(sitemap_urls)해 진정한 증분 갱신과
도메인 재쿼리 없는 파일 재작성을 구현했다. 리소스 공개/비공개/삭제 시
해당 항목만 색인이 갱신되고, 관리자=full·스케줄러=auto·리소스 변경=증분으로 동작한다.

- sitemap_urls 마이그레이션 + SitemapUrl 모델. loc(2048) 를 unique 에 직접 넣으면
 InnoDB 키 길이를 초과하므로 loc_hash(sha256) 컬럼을 identity 로 사용
- SitemapUrlRepository(멱등 upsert / remove / 스트리밍) + SitemapIndexer
 (+ 제3자용 sitemap.index.collect_for_resource 필터 훅)
- 4 리스너 재배선: 공개→색인 / 비공개·삭제→색인 해제 + 재생성 잡 디바운스 디스패치.
 3 기여자가 resource_type/resource_id 를 함께 emit 해 전체·증분 identity 를 일치
- 재생성 모드를 SitemapGenerationMode Backed Enum 으로 통일(Command→Job→Manager)
- seo:generate-sitemap 에 --rebuild / --mode 옵션 추가
This commit is contained in:
HeuJung committed 2026-07-18 16:19:49 +09:00
1 parent 1a6fca0d6b
commit 1629684156
34 files changed
+2027 -23

No files matched your search

@@ -4,7 +4,9 @@ namespace Modules\Sirsoft\Ecommerce\Listeners;
use App\Contracts\Extension\CacheInterface;
use App\Contracts\Extension\HookListenerInterface;
use App\Jobs\GenerateSitemapJob;
use App\Seo\Contracts\SeoCacheManagerInterface;
use App\Seo\SitemapIndexer;
use Illuminate\Support\Facades\Log;
/**
@@ -32,7 +34,7 @@ class SeoCategoryCacheListener implements HookListenerInterface
'priority' => 20,
],
'sirsoft-ecommerce.category.after_delete' => [
'method' => 'onCategoryChange',
'method' => 'onCategoryDelete',
'priority' => 20,
],
];
@@ -87,5 +89,75 @@ class SeoCategoryCacheListener implements HookListenerInterface
'error' => $e->getMessage(),
]);
}
$this->syncSitemapIndex($category);
}
/**
* 카테고리 삭제 시 캐시를 무효화하고 사이트맵 색인을 제거합니다.
*
* after_delete 훅은 모델이 아닌 카테고리 ID(int)를 전달합니다.
*
* @param mixed ...$args 훅 인자 (첫 번째: 카테고리 ID)
*/
public function onCategoryDelete(...$args): void
{
$this->onCategoryChange(...$args);
$categoryId = $args[0] ?? null;
if ($categoryId === null) {
return;
}
try {
app(SitemapIndexer::class)->deindexResource('category', is_object($categoryId) ? $categoryId->id : $categoryId);
GenerateSitemapJob::dispatch();
} catch (\Throwable $e) {
Log::warning('[SEO] Category sitemap index removal failed', [
'error' => $e->getMessage(),
]);
}
}
/**
* 카테고리의 사이트맵 색인을 증분 갱신합니다.
*
* 활성(is_active) 이며 'SEO 제공 페이지(카테고리)' 토글이 켜져 있으면 색인(upsert),
* 아니면 색인 해제(remove)한 뒤, 사이트맵 재생성 잡을 디바운스 디스패치합니다.
* 색인 규칙은 EcommerceSitemapContributor 의 카테고리 URL 규칙과 일치해야 합니다.
*
* @param mixed $category Category 모델 (create/update)
*/
private function syncSitemapIndex(mixed $category): void
{
if (! is_object($category) || ! isset($category->id) || ! isset($category->slug)) {
return;
}
try {
$indexer = app(SitemapIndexer::class);
$visible = (bool) ($category->is_active ?? false)
&& (bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_category', true);
if ($visible) {
$routePath = g7_module_settings('sirsoft-ecommerce', 'basic_info.route_path', 'shop');
$indexer->indexResource('category', $category->id, 'sirsoft-ecommerce', [[
'url' => "/{$routePath}/category/{$category->slug}",
'lastmod' => $category->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.6,
]]);
} else {
$indexer->deindexResource('category', $category->id);
}
GenerateSitemapJob::dispatch();
} catch (\Throwable $e) {
Log::warning('[SEO] Category sitemap index sync failed', [
'error' => $e->getMessage(),
'category_id' => $category->id ?? null,
]);
}
}
}
@@ -4,9 +4,12 @@ namespace Modules\Sirsoft\Ecommerce\Listeners;
use App\Contracts\Extension\CacheInterface;
use App\Contracts\Extension\HookListenerInterface;
use App\Jobs\GenerateSitemapJob;
use App\Seo\Contracts\SeoCacheManagerInterface;
use App\Seo\SeoCacheRegenerator;
use App\Seo\SitemapIndexer;
use Illuminate\Support\Facades\Log;
use Modules\Sirsoft\Ecommerce\Enums\ProductDisplayStatus;
/**
* 상품 변경 시 SEO 캐시 무효화 리스너
@@ -59,6 +62,7 @@ class SeoProductCacheListener implements HookListenerInterface
{
$this->invalidateRelatedCaches($args);
$this->regenerateDetailCache($args);
$this->syncSitemapIndex($args, false);
}
/**
@@ -70,6 +74,7 @@ class SeoProductCacheListener implements HookListenerInterface
{
$this->invalidateRelatedCaches($args);
$this->regenerateDetailCache($args);
$this->syncSitemapIndex($args, false);
}
/**
@@ -82,6 +87,7 @@ class SeoProductCacheListener implements HookListenerInterface
public function onProductDelete(...$args): void
{
$this->invalidateRelatedCaches($args);
$this->syncSitemapIndex($args, true);
}
/**
@@ -127,6 +133,53 @@ class SeoProductCacheListener implements HookListenerInterface
}
}
/**
* 상품의 사이트맵 색인을 증분 갱신합니다.
*
* 전시(display_status=VISIBLE)이며 'SEO 제공 페이지(상품 상세)' 토글이 켜져 있으면 색인(upsert),
* 비공개/토글 OFF/삭제이면 색인 해제(remove)한 뒤, 사이트맵 재생성 잡을 디바운스 디스패치합니다.
* 색인 규칙은 EcommerceSitemapContributor 의 상품 URL 규칙과 일치해야 합니다.
*
* @param array $args 훅 인자 배열 (첫 번째: Product 모델)
* @param bool $deleted 삭제 이벤트 여부
*/
private function syncSitemapIndex(array $args, bool $deleted): void
{
$product = $args[0] ?? null;
if (! $product || ! isset($product->id)) {
return;
}
try {
$indexer = app(SitemapIndexer::class);
$visible = ! $deleted
&& ($product->display_status ?? null) === ProductDisplayStatus::VISIBLE
&& (bool) g7_module_settings('sirsoft-ecommerce', 'seo.seo_product_detail', true);
if ($visible) {
$routePath = g7_module_settings('sirsoft-ecommerce', 'basic_info.route_path', 'shop');
$indexer->indexResource('product', $product->id, 'sirsoft-ecommerce', [[
'url' => "/{$routePath}/products/{$product->id}",
'lastmod' => $product->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.8,
]]);
} else {
$indexer->deindexResource('product', $product->id);
}
// 파일 재작성은 잡에 위임 (ShouldBeUnique 로 디바운스). auto 모드 → 저장소 기반 증분.
GenerateSitemapJob::dispatch();
} catch (\Throwable $e) {
Log::warning('[SEO] Product sitemap index sync failed', [
'error' => $e->getMessage(),
'product_id' => $product->id ?? null,
]);
}
}
/**
* 상품 상세 페이지의 SEO 캐시를 즉시 재생성합니다.
*
@@ -81,6 +81,8 @@ class EcommerceSitemapContributor extends AbstractSitemapContributor
'url' => "/{$routePath}/products",
'changefreq' => 'daily',
'priority' => 0.7,
'resource_type' => 'shop_index',
'resource_id' => null,
];
}
@@ -92,6 +94,8 @@ class EcommerceSitemapContributor extends AbstractSitemapContributor
'lastmod' => $category->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.6,
'resource_type' => 'category',
'resource_id' => (string) $category->id,
];
}
}
@@ -104,6 +108,8 @@ class EcommerceSitemapContributor extends AbstractSitemapContributor
'lastmod' => $product->updated_at?->toW3cString(),
'changefreq' => 'weekly',
'priority' => 0.8,
'resource_type' => 'product',
'resource_id' => (string) $product->id,
];
}
}