Skip to content

Commit

Permalink
Merge pull request #378 from AmazeeLabs/SLB-478-noindex
Browse files Browse the repository at this point in the history
chore(SLB-478): noindex template apps
  • Loading branch information
colorfield authored Jan 30, 2025
2 parents ec0cb8c + 48052f6 commit 62339ca
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
6 changes: 6 additions & 0 deletions apps/preview/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ if (isSessionRequired()) {
// Authentication middleware based on the configuration.
const authMiddleware = getAuthenticationMiddleware();

// Prevent indexing.
app.use((_, res, next) => {
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
next();
});

app.get('/endpoint.js', (_, res) => {
res.send(
`window.GRAPHQL_ENDPOINT = "${
Expand Down
2 changes: 1 addition & 1 deletion apps/publisher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@custom/publisher",
"private": true,
"dependencies": {
"@amazeelabs/publisher": "^2.5.9",
"@amazeelabs/publisher": "^2.5.10",
"typescript": "^5.7.3"
},
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion apps/website/gatsby-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ process.env.CLOUDINARY_API_KEY = process.env.CLOUDINARY_API_KEY || 'test';
process.env.CLOUDINARY_API_SECRET = process.env.CLOUDINARY_API_SECRET || 'test';
process.env.CLOUDINARY_CLOUDNAME = process.env.CLOUDINARY_CLOUDNAME || 'demo';

process.env.NOINDEX = process.env.NOINDEX || 'false';

/**
*
* @type {import('gatsby').GatsbyConfig['plugins']}
Expand Down Expand Up @@ -42,6 +44,12 @@ const plugins = [
options: {
// To avoid "X-Frame-Options: DENY" in Drupal iframes.
mergeSecurityHeaders: false,
headers:
process.env.NOINDEX === 'true'
? {
'/*': ['X-Robots-Tag: noindex, nofollow'],
}
: {},
},
},
{
Expand All @@ -50,7 +58,10 @@ const plugins = [
{
resolve: 'gatsby-plugin-robots-txt',
options: {
policy: [{ userAgent: '*', allow: '/', disallow: [] }],
policy:
process.env.NOINDEX === 'true'
? [{ userAgent: '*', disallow: '/' }]
: [{ userAgent: '*', allow: '/', disallow: [] }],
},
},
{
Expand Down
5 changes: 5 additions & 0 deletions packages/drupal/custom/custom.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ services:
arguments: ['@language_manager', '@current_route_match']
tags:
- { name: event_subscriber }

custom.event_subscriber:
class: Drupal\custom\EventSubscriber\RobotsSubscriber
tags:
- { name: event_subscriber }
36 changes: 36 additions & 0 deletions packages/drupal/custom/src/EventSubscriber/RobotsSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Drupal\custom\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Prevents any Drupal page to be indexed by search engines.
*
* Additional measure as robots.txt could be overridden by a scaffold process.
*/
final class RobotsSubscriber implements EventSubscriberInterface {

/**
* Kernel response event handler.
*/
public function onKernelResponse(ResponseEvent $event): void {
$response = $event->getResponse();
$response->headers->set('X-Robots-Tag', 'noindex, nofollow');
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::RESPONSE => ['onKernelResponse'],
];
}

}
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62339ca

Please sign in to comment.