-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send
X-Robots-Tag
header in all admin/api controllers and in pulse …
…error (#423) Pages that should not be indexed, should use the header or a meta tag, not robots.txt, but we'll try both at least for now. https://support.google.com/webmasters/answer/7440203#indexed_though_blocked_by_robots_txt https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag
- Loading branch information
Showing
14 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Api\Presenters; | ||
|
||
use MichalSpacekCz\Http\Robots\Robots; | ||
use MichalSpacekCz\Http\Robots\RobotsRule; | ||
use MichalSpacekCz\Www\Presenters\BasePresenter as WwwBasePresenter; | ||
use Override; | ||
|
||
abstract class BasePresenter extends WwwBasePresenter | ||
{ | ||
|
||
private Robots $robots; | ||
|
||
|
||
/** | ||
* @internal | ||
*/ | ||
public function injectRobots(Robots $robots): void | ||
{ | ||
$this->robots = $robots; | ||
} | ||
|
||
|
||
#[Override] | ||
protected function startup(): void | ||
{ | ||
parent::startup(); | ||
$this->robots->setHeader([RobotsRule::NoIndex, RobotsRule::NoFollow]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Robots; | ||
|
||
use Nette\Http\IResponse; | ||
|
||
class Robots | ||
{ | ||
|
||
public function __construct( | ||
private readonly IResponse $httpResponse, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* @param list<RobotsRule> $rules | ||
*/ | ||
public function setHeader(array $rules): void | ||
{ | ||
$value = implode(', ', array_map(fn(RobotsRule $rule): string => $rule->value, $rules)); | ||
$this->httpResponse->addHeader('X-Robots-Tag', $value); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Robots; | ||
|
||
enum RobotsRule: string | ||
{ | ||
|
||
case NoFollow = 'nofollow'; | ||
case NoIndex = 'noindex'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace MichalSpacekCz\Http\Robots; | ||
|
||
use MichalSpacekCz\Test\Http\Response; | ||
use MichalSpacekCz\Test\TestCaseRunner; | ||
use Tester\Assert; | ||
use Tester\TestCase; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
/** @testCase */ | ||
class RobotsTest extends TestCase | ||
{ | ||
|
||
public function __construct( | ||
private readonly Response $httpResponse, | ||
private readonly Robots $robots, | ||
) { | ||
} | ||
|
||
|
||
public function testSetHeader(): void | ||
{ | ||
$this->robots->setHeader([RobotsRule::NoIndex]); | ||
Assert::same('noindex', $this->httpResponse->getHeader('X-Robots-Tag')); | ||
|
||
$this->robots->setHeader([RobotsRule::NoIndex, RobotsRule::NoFollow]); | ||
Assert::same('noindex, nofollow', $this->httpResponse->getHeader('X-Robots-Tag')); | ||
} | ||
|
||
} | ||
|
||
TestCaseRunner::run(RobotsTest::class); |