Track and display subdomains under their root domain

Adds sites.root_domain (indexed string, backfilled for existing rows)
computed via a naive "last two labels" heuristic (App\Support\RootDomain)
— good enough for example.com/somesite.es, but wrong for two-part
public suffixes like .co.uk. Doing that correctly needs the Mozilla
Public Suffix List; flagged as a known simplification rather than
built now.

Deliberately not a hard FK: like Backlink.target_domain, a subdomain
can get indexed before its root domain does (or vice versa), so
Site::subdomains()/rootSite() match on the domain string rather than
requiring the relationship to exist at write time.

Site detail page: a root domain shows a paginated "Subdomains"
section (same guest-preview cap as Pages/Backlinks); a subdomain
shows "Subdomain of {root}" linking back, when the root is itself
indexed.

Verified against real data: 21k.ro correctly lists its two known
subdomains (buzau.21k.ro, timisoara.21k.ro), each of which correctly
links back to 21k.ro. Added SubdomainsTest (root lists subdomains,
subdomain links to root, empty state) — full suite (31 tests) passes.
This commit is contained in:
m105
2026-07-25 15:44:12 +03:00
parent 6435bccb20
commit 72b87ab579
8 changed files with 196 additions and 0 deletions
+1
View File
@@ -17,6 +17,7 @@ class GuestPreviewTest extends TestCase
Site::create([
'collection' => 'CC-MAIN-TEST',
'domain' => "example{$i}.com",
'root_domain' => "example{$i}.com",
'url' => "https://example{$i}.com/",
]);
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace Tests\Feature;
use App\Models\Site;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SubdomainsTest extends TestCase
{
use RefreshDatabase;
public function test_root_domain_page_lists_its_subdomains(): void
{
$root = Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'example.com', 'root_domain' => 'example.com']);
Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'blog.example.com', 'root_domain' => 'example.com']);
Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'shop.example.com', 'root_domain' => 'example.com']);
Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'unrelated.net', 'root_domain' => 'unrelated.net']);
$response = $this->actingAs(User::factory()->create())->get(route('sites.show', $root));
$response->assertOk();
$response->assertSeeText('Subdomains (2)');
$response->assertSeeText('blog.example.com');
$response->assertSeeText('shop.example.com');
$response->assertDontSeeText('unrelated.net');
}
public function test_subdomain_page_links_back_to_its_root(): void
{
$root = Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'example.com', 'root_domain' => 'example.com']);
$sub = Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'blog.example.com', 'root_domain' => 'example.com']);
$response = $this->actingAs(User::factory()->create())->get(route('sites.show', $sub));
$response->assertOk();
$response->assertSeeText('Subdomain of');
$response->assertSeeText($root->domain);
}
public function test_root_domain_with_no_subdomains_shows_empty_state(): void
{
$root = Site::create(['collection' => 'CC-MAIN-TEST', 'domain' => 'lonely.com', 'root_domain' => 'lonely.com']);
$response = $this->actingAs(User::factory()->create())->get(route('sites.show', $root));
$response->assertOk();
$response->assertSeeText('Subdomains (0)');
$response->assertSeeText('No subdomains found.');
$response->assertDontSeeText('Subdomain of');
}
}