Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate Twitter card tags #83

Merged
merged 3 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/Tags/TwitterCardTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,25 @@ public static function initialize(SEOData $SEOData): ?static
$collection = new static();

// No generic image that spans multiple pages
if ($SEOData->image && $SEOData->image !== secure_url(config('seo.image.fallback'))) {
if ($SEOData->imageMeta?->width - $SEOData->imageMeta?->height - 20 < 0) {
if ($SEOData->image && $SEOData->image !== secure_url(config('seo.image.fallback')) && $SEOData->imageMeta) {
// Only one Twitter card can be pushed. The `summary_large_image` card
// is tried first, then it falls back to the normal `summary` card.
$imageMetaWidthDividedByHeight = $SEOData->imageMeta->width / $SEOData->imageMeta->height;

if ($imageMetaWidthDividedByHeight < 1.5) {
// Summary large card has aspect ratio of 2:1. Aspect ratios of < 1 are closer to 1:1
// then they are to 2:1. Assuming most images are landscape, so fallback to 2:1.
$collection->push(Summary::initialize($SEOData));
}

if ($SEOData->imageMeta?->width - 2 * $SEOData->imageMeta?->height - 20 < 0) {
} else {
$collection->push(SummaryLargeImage::initialize($SEOData));
}
} else {
$collection->push(new TwitterCardTag('card', 'summary'));
if ($SEOData->image && ! $SEOData->imageMeta) {
// Image external URL...
$collection->push(SummaryLargeImage::initialize($SEOData));
} else {
$collection->push(new TwitterCardTag('card', 'summary'));
}
}

if ($SEOData->openGraphTitle) {
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Tags/TwitterCardSummaryTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@
->assertSee('<meta name="twitter:title" content="Test Page | Laravel SEO">', false)
->assertSee('<meta name="twitter:description" content="Fallback description">', false)
->assertSee('<meta name="twitter:image" content="' . secure_url($imagePath) . '">', false)
->assertDontSee('<meta name="twitter:image:width" content="' . $expectedWidth . '">', false)
->assertDontSee('<meta name="twitter:image:height" content="' . $expectedHeight . '">', false)
->assertDontSee('<meta name="twitter:image:width"', false)
->assertDontSee('<meta name="twitter:image:height"', false)
->assertDontSee('twitter:site'); // We should not display an empty '@' username.
})->with([
['summary', 'images/twitter-1743x1743.jpg', '1743', '1743'],
// Defaulting all images to summary_large_image, since external image URL cannot do aspect ratio check and most images are landscape (assumption)
['summary_large_image', 'images/twitter-1743x1743.jpg', '1743', '1743'],
['summary_large_image', 'images/twitter-3597x1799.jpg', '3597', '1799'],
]);

Expand Down
Loading