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,9 +4,12 @@ namespace Modules\Sirsoft\Board\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\Board\Enums\PostStatus;
/**
* 게시글 변경 시 SEO 캐시 무효화 리스너
@@ -65,6 +68,8 @@ class SeoBoardCacheListener implements HookListenerInterface
// 게시글 상세 페이지 캐시 즉시 재생성
$this->regenerateDetailCache($args);
$this->syncPostSitemapIndex($args, false);
}
/**
@@ -78,6 +83,8 @@ class SeoBoardCacheListener implements HookListenerInterface
// 게시글 상세 페이지 캐시 즉시 재생성
$this->regenerateDetailCache($args);
$this->syncPostSitemapIndex($args, false);
}
/**
@@ -90,6 +97,8 @@ class SeoBoardCacheListener implements HookListenerInterface
public function onPostDelete(...$args): void
{
$this->invalidateRelatedCaches($args);
$this->syncPostSitemapIndex($args, true);
}
/**
@@ -141,6 +150,96 @@ class SeoBoardCacheListener implements HookListenerInterface
'board_slug' => $slug,
]);
}
$this->syncBoardSitemapIndex($board);
}
/**
* 게시글의 사이트맵 색인을 증분 갱신합니다.
*
* 게시 상태(Published)이고 비밀글이 아니며 'SEO 제공 페이지(게시글 상세)' 토글이 켜져 있으면
* 색인(upsert), 아니면 색인 해제(remove)한 뒤 사이트맵 재생성 잡을 디바운스 디스패치합니다.
* 색인 규칙은 BoardSitemapContributor 의 게시글 URL 규칙과 일치해야 합니다.
*
* @param array $args 훅 인자 배열 (첫 번째: Post 모델, 두 번째: 게시판 slug)
* @param bool $deleted 삭제 이벤트 여부
*/
private function syncPostSitemapIndex(array $args, bool $deleted): void
{
$post = $args[0] ?? null;
$slug = $args[1] ?? null;
if (! $post || ! isset($post->id) || ! $slug) {
return;
}
try {
$indexer = app(SitemapIndexer::class);
$visible = ! $deleted
&& ($post->status ?? null) === PostStatus::Published
&& ! (bool) ($post->is_secret ?? false)
&& (bool) g7_module_settings('sirsoft-board', 'seo.seo_post_detail', true);
if ($visible) {
$indexer->indexResource('board_post', $post->id, 'sirsoft-board', [[
'url' => "/board/{$slug}/{$post->id}",
'lastmod' => $post->updated_at?->toW3cString(),
'changefreq' => 'monthly',
'priority' => 0.5,
]]);
} else {
$indexer->deindexResource('board_post', $post->id);
}
GenerateSitemapJob::dispatch();
} catch (\Throwable $e) {
Log::warning('[SEO] Board post sitemap index sync failed', [
'error' => $e->getMessage(),
'post_id' => is_object($post) ? ($post->id ?? null) : null,
]);
}
}
/**
* 게시판의 사이트맵 색인을 증분 갱신합니다.
*
* 활성(is_active) 이며 'SEO 제공 페이지(게시판)' 토글이 켜져 있으면 색인(upsert),
* 아니면 색인 해제(remove)한 뒤 사이트맵 재생성 잡을 디바운스 디스패치합니다.
* 색인 규칙은 BoardSitemapContributor 의 게시판 URL 규칙과 일치해야 합니다.
*
* @param mixed $board Board 모델
*/
private function syncBoardSitemapIndex(mixed $board): void
{
if (! is_object($board) || ! isset($board->id) || ! isset($board->slug)) {
return;
}
try {
$indexer = app(SitemapIndexer::class);
$visible = (bool) ($board->is_active ?? false)
&& (bool) g7_module_settings('sirsoft-board', 'seo.seo_board', true);
if ($visible) {
$indexer->indexResource('board', $board->id, 'sirsoft-board', [[
'url' => "/board/{$board->slug}",
'lastmod' => $board->updated_at?->toW3cString(),
'changefreq' => 'daily',
'priority' => 0.6,
]]);
} else {
$indexer->deindexResource('board', $board->id);
}
GenerateSitemapJob::dispatch();
} catch (\Throwable $e) {
Log::warning('[SEO] Board sitemap index sync failed', [
'error' => $e->getMessage(),
'board_id' => is_object($board) ? ($board->id ?? null) : null,
]);
}
}
/**
@@ -84,6 +84,8 @@ class BoardSitemapContributor extends AbstractSitemapContributor
'url' => '/boards',
'changefreq' => 'weekly',
'priority' => 0.5,
'resource_type' => 'board_index',
'resource_id' => null,
];
}
@@ -100,6 +102,8 @@ class BoardSitemapContributor extends AbstractSitemapContributor
'lastmod' => $board->updated_at?->toW3cString(),
'changefreq' => 'daily',
'priority' => 0.6,
'resource_type' => 'board',
'resource_id' => (string) $board->id,
];
}
@@ -114,6 +118,8 @@ class BoardSitemapContributor extends AbstractSitemapContributor
'lastmod' => $post->updated_at?->toW3cString(),
'changefreq' => 'monthly',
'priority' => 0.5,
'resource_type' => 'board_post',
'resource_id' => (string) $post->id,
];
}
}