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

chore(SLB-478): noindex template apps #378

Open
wants to merge 3 commits into
base: release
Choose a base branch
from
Open
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
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
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'],
];
}

}