Skip to content

Commit

Permalink
feat: add robots.txt response
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff committed Jan 20, 2022
1 parent b9e65e0 commit 6cb7668
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import { GoogleLogger } from './google-logger';
import { CoralogixLogger } from './coralogix-logger';
import { CoralogixErrorLogger } from './coralogix-error-logger';
import { respondRobots } from './robots.js';

function respondError(message, status, e, req) {
const headers = new Headers();
Expand Down Expand Up @@ -44,6 +45,9 @@ function hashCode(s) {

async function main(req) {
try {
if (req.method === 'GET' && req.url.startsWith('/robots.txt')) {
return respondRobots(req);
}
const body = req.method === 'GET'
? JSON.parse(new URL(req.url).searchParams.get('data'))
: await req.json();
Expand Down
21 changes: 21 additions & 0 deletions src/robots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2022 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-env serviceworker */
export function respondRobots() {
return new Response(`
User-agent: *
Disallow: /`, {
headers: {
'content-type': 'text/plain',
},
});
}
8 changes: 8 additions & 0 deletions test/post-deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe('Helix RUM Collector Post-Deploy Tests', () => {
expect(response).to.have.status(201);
});

it('robots.txt denies everything', async () => {
const response = await chai.request(`https://${domain}`)
.get('/robots.txt');
expect(response).to.have.status(200);
// eslint-disable-next-line no-unused-expressions
expect(response).to.be.text;
});

it('Missing id returns 400', async () => {
const response = await chai.request(`https://${domain}`)
.post('/')
Expand Down

0 comments on commit 6cb7668

Please sign in to comment.