このブログの記事ページには、日付の行に「公開」と「更新」を並べて出しています。「更新」は、公開した日より後に記事を直したときだけ出すつもりでした。

ところが、11月20日に公開予定の記事をプレビューで確認していたとき、日付の行が「公開 2026年11月20日 更新 2026年11月14日」になっているのに気付きました。公開より6日前に「更新」したことになっています。

このブログでは、記事を何本か書きためて、公開日を決めて予約しています。予約したあとで本文を直すことはよくあるので、このままでは予約投稿のたびに同じ表示が出てしまいます。

「違う日」を「後の日」として扱っていた

「更新」を出すかどうかは、Post モデルのメソッドで決めています。修正前はこうでした。

/**
 * Show 「更新」 only when the last change is on a later day than publishing.
 */
public function wasUpdatedAfterPublishing(): bool
{
    return $this->updated_at !== null && $this->published_at !== null
        && $this->updated_at->format('Y-m-d') !== $this->published_at->format('Y-m-d');
}

コメントには「公開より後の日」と書いてありますが、実際の比較は !==、つまり「公開日と違う日」です。すでに公開した記事を直す場合、updated_at は必ず published_at より後になるので、この2つの違いは表に出ませんでした。

予約投稿では順番が逆になります。11月20日 8:00に公開する予約を入れたあと、11月14日に本文を直すと、updated_at は11月14日、published_at は11月20日です。日付が違うので「更新」が出て、公開日より前の日付が表示されていました。

既存のテストも「同じ日なら出さない」「後の日なら出す」の2つだけで、「前の日」の場合はありませんでした。

「更新」は後の日のときだけにする

比較を > に変えました。Y-m-d 形式の文字列なので、そのまま大小を比べられます。

public function wasUpdatedAfterPublishing(): bool
{
    return $this->updated_at !== null && $this->published_at !== null
        && $this->updated_at->format('Y-m-d') > $this->published_at->format('Y-m-d');
}

dateModified・article:modified_time・lastmodも同じ問題があった

画面の表示だけでなく、検索エンジン向けの値にも updated_at をそのまま使っていました。

  • JSON-LDの dateModified:$post->updated_at ?? $post->published_at
  • OGPの article:modified_time:$post->updated_at
  • sitemap.xmlの記事の lastmod:$post->updated_at(カテゴリー・タグ・ホームは、その中の記事の updated_at の最大値)

予約投稿の場合、どれも公開時刻より前の時刻になります。そこで、「最後に変更した時刻。ただし公開時刻より前にはしない」を返すメソッドを1つ作り、3か所ともこれを使うようにしました。

public function modifiedAt(): ?CarbonImmutable
{
    if ($this->updated_at === null || $this->published_at === null) {
        return $this->updated_at ?? $this->published_at;
    }

    return $this->updated_at->greaterThan($this->published_at) ? $this->updated_at : $this->published_at;
}

JSON-LDとOGPは、呼び出しを置き換えるだけです。

// JsonLd::blogPosting()(一部)
'datePublished' => $post->published_at?->toIso8601String(),
'dateModified' => $post->modifiedAt()?->toIso8601String(),

// SeoData::post()(一部)
'modified_time' => $post->modifiedAt()?->toIso8601String(),

sitemapは、カテゴリーとタグの lastmod を withMax(..., 'updated_at') でSQLから取っていました。modifiedAt() はPHP側の計算なので、公開記事を一度取得し、記事ごとの modifiedAt() からカテゴリー・タグごとの最大値を作る形に変えました。公開記事をすべて読み込みますが、今は30本ほどなので問題ありません。記事が大きく増えたら、見直すつもりです。

$posts = Post::query()->published()->with(['categories', 'tags'])
    ->orderByDesc('published_at')->orderByDesc('id')->get();
$newest = $posts->max(fn (Post $p): ?CarbonInterface => $p->modifiedAt());

$categoryLastmod = [];
foreach ($posts as $post) {
    $modified = $post->modifiedAt();
    if ($modified === null) {
        continue;
    }
    foreach ($post->categories as $category) {
        $categoryLastmod[$category->id] = max($categoryLastmod[$category->id] ?? $modified, $modified);
    }
    // タグも同じ形(省略)
}

テスト

修正前のコードで失敗することを確かめてから、テストを3つ(データセットの2ケースを含めると4ケース)追加しました。PostModelTestの2つを抜粋します。

it('hides 「更新」 for a scheduled post that was edited before it went out', function (): void {
    // 11/14 に直し、11/20 8:00 に予約した記事と同じ状態
    $post = Post::factory()->create(['is_published' => true, 'published_at' => '2026-11-20 08:00:00']);
    Post::query()->whereKey($post->id)->toBase()->update(['updated_at' => '2026-11-14 16:44:00']);

    $this->travelTo('2026-11-21 09:00:00');

    expect($post->fresh()->wasUpdatedAfterPublishing())->toBeFalse();
    $this->get("/post/{$post->id}")->assertOk()
        ->assertSee('公開 <time', false)->assertSee('2026年11月20日')
        ->assertDontSee('更新 <time', false)->assertDontSee('2026年11月14日');
});

it('never puts dateModified or article:modified_time before the publish time', function (string $updatedAt, string $expected): void {
    // 予約投稿を作り、updated_at を書き込み、12/1 に記事を開く(省略)
    // JSON-LD の BlogPosting を取り出す(省略)

    expect($posting['dateModified'])->toBe($expectedIso)
        ->and($html)->toContain('<meta property="article:modified_time" content="'.$expectedIso.'">');
})->with([
    'edited before publishing → publish time' => ['2026-11-14 16:44:00', '2026-11-20 08:00:00'],
    'edited after publishing → edit time' => ['2026-11-25 10:00:00', '2026-11-25 10:00:00'],
]);

残りの1つはsitemapのテストで、予約投稿の記事・カテゴリー・タグ・ホームの lastmod が公開時刻になることを確かめています。

ルールも書き換えた

このプロジェクトでは、仕様書(SPEC.md)と、Claude Codeが読むCLAUDE.mdにルールを書いています。今回は両方を直しました。

  • 仕様書:「更新」は updated_at が published_at より後の日のときだけ。sitemapの lastmod は modifiedAt()
  • CLAUDE.md:sitemapの lastmod、JSON-LDの dateModified、article:modified_time の3つは Post::modifiedAt() だけを使う

修正前は、3か所がそれぞれ updated_at を直接読んでいました。ルールに書いておけば、あとで別の場所に日付を出すときも、同じ間違いをしにくくなります。

修正は、11月17日に公開予定だった記事より前に本番へ反映しました。その記事は公開前に何度か直していましたが、公開後の日付の行は「公開 2026年11月17日」だけになっています。