From 925fd85b9576d9a06447d8996a777e541192a3f3 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Fri, 17 Jul 2026 16:24:23 -0300 Subject: [PATCH] refactor(posts): return the card display domain from the backend Compute the bare display host once in LinkCardMetadata (via Laravel's Uri::host + Str::chopStart) and return it as card.domain, so the LinkCard component renders it directly instead of parsing the URL client-side. The component is now purely presentational. --- .../Social/LinkCard/LinkCardMetadata.php | 19 ++++++++++++++++++- .../js/components/posts/previews/LinkCard.vue | 16 ++-------------- resources/js/composables/useLinkCard.ts | 1 + .../App/Post/LinkPreviewControllerTest.php | 1 + 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/Services/Social/LinkCard/LinkCardMetadata.php b/app/Services/Social/LinkCard/LinkCardMetadata.php index 2e1f47a7..53760b73 100644 --- a/app/Services/Social/LinkCard/LinkCardMetadata.php +++ b/app/Services/Social/LinkCard/LinkCardMetadata.php @@ -4,6 +4,9 @@ namespace App\Services\Social\LinkCard; +use Illuminate\Support\Str; +use Illuminate\Support\Uri; + final readonly class LinkCardMetadata { public function __construct( @@ -14,15 +17,29 @@ public function __construct( ) {} /** - * @return array{uri: string, title: string, description: string, image: ?string} + * @return array{uri: string, domain: string, title: string, description: string, image: ?string} */ public function toArray(): array { return [ 'uri' => $this->uri, + 'domain' => $this->domain(), 'title' => $this->title, 'description' => $this->description, 'image' => $this->imageUrl, ]; } + + /** + * The bare display host for the card (e.g. "nyt.com" from + * "https://www.nyt.com/x"), so the frontend renders it without parsing URLs. + */ + private function domain(): string + { + $host = Uri::of($this->uri)->host(); + + return $host === null || $host === '' + ? $this->uri + : Str::chopStart($host, 'www.'); + } } diff --git a/resources/js/components/posts/previews/LinkCard.vue b/resources/js/components/posts/previews/LinkCard.vue index bd759aae..d440cc53 100644 --- a/resources/js/components/posts/previews/LinkCard.vue +++ b/resources/js/components/posts/previews/LinkCard.vue @@ -1,19 +1,7 @@