get()
->map(fn (Post $p) => PostData::fromPost($p));
+ $data = PostData::fromPost($post, content: $post->content);
+
return Inertia::render('Blog::Show', [
- 'post' => PostData::fromPost($post, content: $post->content),
+ 'post' => $data,
'related' => $related,
'preview' => $preview,
+ 'json_ld' => $this->jsonLd($post, $data),
])->withSSR();
}
+
+ /**
+ * The Article and BreadcrumbList blocks, ready to print. Built here once for both
+ * stacks: JSON_HEX_TAG escapes `<` and `>`, so a title holding `` cannot
+ * end the script tag the page prints them in.
+ *
+ * @return list
+ */
+ private function jsonLd(Post $post, PostData $data): array
+ {
+ $article = array_filter([
+ '@context' => 'https://schema.org',
+ '@type' => 'Article',
+ 'headline' => $data->title,
+ 'description' => $data->excerpt,
+ 'image' => $data->cover_url ?: null,
+ 'datePublished' => $data->published_at,
+ 'dateModified' => $data->updated_at,
+ 'author' => $post->author ? ['@type' => 'Person', 'name' => $post->author->name] : null,
+ 'url' => $data->url,
+ ], fn ($value) => $value !== null);
+
+ // The same trail as the visible breadcrumbs, which search results can show in place of the URL.
+ $crumbs = [
+ ['name' => __('Blog'), 'item' => route('blog.index')],
+ ...($post->category ? [['name' => $post->category->name, 'item' => route('blog.category', $post->category->slug)]] : []),
+ ['name' => $data->title, 'item' => $data->url],
+ ];
+
+ $breadcrumbs = [
+ '@context' => 'https://schema.org',
+ '@type' => 'BreadcrumbList',
+ 'itemListElement' => array_map(
+ fn (array $crumb, int $index) => ['@type' => 'ListItem', 'position' => $index + 1, ...$crumb],
+ $crumbs,
+ array_keys($crumbs),
+ ),
+ ];
+
+ $flags = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR;
+
+ return [json_encode($article, $flags), json_encode($breadcrumbs, $flags)];
+ }
}
diff --git a/tests/Feature/BlogControllerTest.php b/tests/Feature/BlogControllerTest.php
index 31ca786..3373033 100644
--- a/tests/Feature/BlogControllerTest.php
+++ b/tests/Feature/BlogControllerTest.php
@@ -189,6 +189,30 @@ public function test_show_strips_scripts_from_post_content(): void
);
}
+ /** Built once on the server for both stacks: a title can never close the script tag. */
+ public function test_show_sends_the_json_ld_with_tags_escaped(): void
+ {
+ $category = Category::factory()->create();
+ $post = Post::factory()->published()->create([
+ 'category_id' => $category->id,
+ 'title' => '',
+ ]);
+
+ $this->get($post->url())
+ ->assertOk()
+ ->assertInertia(fn ($page) => $page
+ ->has('json_ld', 2)
+ ->where('json_ld', function ($blocks) use ($post, $category) {
+ [$article, $breadcrumbs] = collect($blocks)->all();
+
+ return ! str_contains($article.$breadcrumbs, '<')
+ && json_decode($article, true)['headline'] === $post->title
+ && json_decode($article, true)['@type'] === 'Article'
+ && array_column(json_decode($breadcrumbs, true)['itemListElement'], 'name') === ['Blog', $category->name, $post->title];
+ })
+ );
+ }
+
public function test_post_resource_includes_expected_fields(): void
{
$author = User::factory()->create();