From 655c957cc3004c4b4841538486f009511573cdf2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 8 Oct 2021 11:06:02 +0200 Subject: [PATCH] Stop indexing /about/peertube --- server/controllers/client.ts | 10 ++++++++-- server/controllers/static.ts | 1 + server/middlewares/index.ts | 1 + server/middlewares/robots.ts | 13 +++++++++++++ server/tests/client.ts | 10 ++++++++++ 5 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 server/middlewares/robots.ts diff --git a/server/controllers/client.ts b/server/controllers/client.ts index d81e35ec3..cdc556da2 100644 --- a/server/controllers/client.ts +++ b/server/controllers/client.ts @@ -5,12 +5,12 @@ import { join } from 'path' import { logger } from '@server/helpers/logger' import { CONFIG } from '@server/initializers/config' import { Hooks } from '@server/lib/plugins/hooks' -import { HttpStatusCode } from '@shared/models' import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' +import { HttpStatusCode } from '@shared/models' import { root } from '../helpers/core-utils' import { STATIC_MAX_AGE } from '../initializers/constants' import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' -import { asyncMiddleware, embedCSP } from '../middlewares' +import { asyncMiddleware, disableRobots, embedCSP } from '../middlewares' const clientsRouter = express.Router() @@ -81,6 +81,12 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => res.status(HttpStatusCode.NOT_FOUND_404).end() }) +// No index exceptions +clientsRouter.all('/about/peertube', + disableRobots, + asyncMiddleware(serveIndexHTML) +) + // Always serve index client page (the client is a single page application, let it handle routing) // Try to provide the right language index.html clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) diff --git a/server/controllers/static.ts b/server/controllers/static.ts index 1c04e4b11..fe1629910 100644 --- a/server/controllers/static.ts +++ b/server/controllers/static.ts @@ -69,6 +69,7 @@ staticRouter.get('/robots.txt', cacheRoute(ROUTE_CACHE_LIFETIME.ROBOTS), (_, res: express.Response) => { res.type('text/plain') + return res.send(CONFIG.INSTANCE.ROBOTS) } ) diff --git a/server/middlewares/index.ts b/server/middlewares/index.ts index a0035f623..d2ed079b6 100644 --- a/server/middlewares/index.ts +++ b/server/middlewares/index.ts @@ -4,6 +4,7 @@ export * from './activitypub' export * from './async' export * from './auth' export * from './pagination' +export * from './robots' export * from './servers' export * from './sort' export * from './user-right' diff --git a/server/middlewares/robots.ts b/server/middlewares/robots.ts new file mode 100644 index 000000000..b22b24a9f --- /dev/null +++ b/server/middlewares/robots.ts @@ -0,0 +1,13 @@ +import express from 'express' + +function disableRobots (req: express.Request, res: express.Response, next: express.NextFunction) { + res.setHeader('X-Robots-Tag', 'noindex') + + return next() +} + +// --------------------------------------------------------------------------- + +export { + disableRobots +} diff --git a/server/tests/client.ts b/server/tests/client.ts index a91bec906..6c32c81db 100644 --- a/server/tests/client.ts +++ b/server/tests/client.ts @@ -482,6 +482,16 @@ describe('Test a client controllers', function () { } } }) + + it('Should add noindex header for some paths', async function () { + const paths = [ '/about/peertube' ] + + for (const path of paths) { + const { headers } = await makeHTMLRequest(servers[0].url, path) + + expect(headers['x-robots-tag']).to.equal('noindex') + } + }) }) describe('Embed HTML', function () {