Skip to content

Commit 00538dc

Browse files
authored
fix(seo): update resource og:image and og:type to support multi-item architecture (#434)
* fix(seo): update resource og:image and og:type to support multi-item architecture * fix(seo): only check thumbnail for resource og:image
1 parent 911b4cc commit 00538dc

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

‎resources/views/app.blade.php‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,13 @@
251251
? "Study material: {$resTitle} ({$resType}) for {$subjectDisplay} on HSCStack."
252252
: "Study material: {$resTitle} ({$resType}) on HSCStack.";
253253
254-
$filePath = data_get($res, 'file_path') ?: data_get($res, 'file_url');
255-
$externalUrl = data_get($res, 'external_url') ?: data_get($res, 'file_url');
256-
257-
if ($resType === 'video' && $externalUrl) {
258-
if (preg_match('/(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/)|youtu\.be\/)([a-zA-Z0-9_-]+)/', $externalUrl, $matches)) {
259-
$ogImage = "https://img.youtube.com/vi/{$matches[1]}/hqdefault.jpg";
260-
}
254+
if ($resType === 'video') {
261255
$ogType = 'video.other';
262-
} elseif ($resType === 'image' && $filePath) {
263-
$ogImage = str_starts_with($filePath, 'http') ? $filePath : \Illuminate\Support\Facades\Storage::url($filePath);
256+
}
257+
258+
$thumbnail = data_get($res, 'thumbnail');
259+
if ($thumbnail) {
260+
$ogImage = str_starts_with($thumbnail, 'http') ? $thumbnail : \Illuminate\Support\Facades\Storage::url($thumbnail);
264261
}
265262
} elseif ($pageComponent === 'Blog/Index') {
266263
$metaTitle = 'Educational Blogs & Study Guides - ' . config('app.name', 'HSCStack');

‎tests/Feature/PublicPagesTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use App\Models\Blog;
44
use App\Models\Node;
55
use App\Models\Notice;
6+
use App\Models\Resource;
67
use App\Models\Subject;
78
use App\Models\User;
89
use Illuminate\Support\Facades\Cache;
@@ -93,6 +94,7 @@
9394
'resource_type' => 'video',
9495
'title' => 'Matrix o nirnayok 01',
9596
'status' => 'approved',
97+
'thumbnail' => 'https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg',
9698
'items' => [
9799
['format' => 'video', 'title' => 'Matrix o nirnayok 01', 'source' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'],
98100
],
@@ -111,8 +113,26 @@
111113
$appName = config('app.name', 'HSCStack');
112114
$response->assertSee("Matrix o nirnayok 01 - HSC - Higher Mathematics - {$appName}", false);
113115
$response->assertSee('Study material: Matrix o nirnayok 01 (video) for HSC - Higher Mathematics on HSCStack.', false);
116+
$response->assertSee('<meta property="og:image" content="https://img.youtube.com/vi/dQw4w9WgXcQ/hqdefault.jpg">', false);
117+
$response->assertSee('<meta property="og:type" content="video.other">', false);
114118

115119
// Ensure subsequent cache-hit request works properly without incomplete class errors
116120
$cachedResponse = $this->get("/resources/{$resource->id}");
117121
$cachedResponse->assertStatus(200);
122+
123+
// Verify resource with custom thumbnail
124+
$thumbResource = App\Models\Resource::create([
125+
'node_id' => $node->id,
126+
'resource_type' => 'note',
127+
'title' => 'Calculus Formula Sheet',
128+
'status' => 'approved',
129+
'thumbnail' => 'https://cdn.hscstack.site/thumbnails/calc.png',
130+
'items' => [
131+
['format' => 'pdf', 'title' => 'Calculus PDF', 'source' => 'https://cdn.hscstack.site/docs/calc.pdf'],
132+
],
133+
]);
134+
135+
$thumbResponse = $this->get("/resources/{$thumbResource->id}");
136+
$thumbResponse->assertStatus(200);
137+
$thumbResponse->assertSee('<meta property="og:image" content="https://cdn.hscstack.site/thumbnails/calc.png">', false);
118138
});

0 commit comments

Comments
 (0)