Skip to content

Commit

Permalink
feat: adding info log
Browse files Browse the repository at this point in the history
  • Loading branch information
dipratap committed Sep 2, 2024
1 parent 7d418a6 commit 531852f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
12 changes: 7 additions & 5 deletions src/metatags/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ import SeoChecks from './seo-checks.js';

async function fetchAndProcessPageObject(s3Client, bucketName, key, prefix, log) {
const object = await getObjectFromKey(s3Client, bucketName, key, log);
if (!object?.tags || typeof object.tags !== 'object') {
if (!object?.scrapeResult?.tags || typeof object.scrapeResult.tags !== 'object') {
log.error(`No Scraped tags found in S3 ${key} object, body ${JSON.stringify(object)}`);
return null;
}
const pageUrl = key.slice(prefix.length - 1).replace('scrape.json', ''); // Remove the prefix and .json suffix
return {
[pageUrl]: {
title: object.tags.title,
description: object.tags.description,
h1: object.tags.h1 || [],
title: object.scrapeResult.tags.title,
description: object.scrapeResult.tags.description,
h1: object.scrapeResult.tags.h1 || [],
},
};
}
Expand Down Expand Up @@ -66,10 +66,12 @@ export default async function auditMetaTags(message, context) {
Object.assign(extractedTags, pageMetadata);
}
}
if (Object.entries(extractedTags).length === 0) {
const extractedTagsCount = Object.entries(extractedTags).length;
if (extractedTagsCount === 0) {
log.error(`Failed to extract tags from scraped content for bucket ${bucketName} and prefix ${prefix}`);
return notFound('Site tags data not available');
}
log.info(`Performing SEO checks for ${extractedTagsCount} tags`);
// Perform SEO checks
const seoChecks = new SeoChecks(log);
for (const [pageUrl, pageTags] of Object.entries(extractedTags)) {
Expand Down
54 changes: 31 additions & 23 deletions test/audits/metatags.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ describe('Meta Tags', () => {
}))).returns({
Body: {
transformToString: () => JSON.stringify({
tags: {
title: 'Test Page',
description: '',
scrapeResult: {
tags: {
title: 'Test Page',
description: '',
},
},
}),
},
Expand All @@ -271,11 +273,13 @@ describe('Meta Tags', () => {
}))).returns({
Body: {
transformToString: () => JSON.stringify({
tags: {
title: 'Test Page',
h1: [
'This is a dummy H1 that is overly length from SEO perspective',
],
scrapeResult: {
tags: {
title: 'Test Page',
h1: [
'This is a dummy H1 that is overly length from SEO perspective',
],
},
},
}),
},
Expand Down Expand Up @@ -344,7 +348,7 @@ describe('Meta Tags', () => {
],
}));
expect(addAuditStub.calledOnce).to.be.true;
expect(logStub.info.calledThrice).to.be.true;
expect(logStub.info.callCount).to.equal(4);
});

it('should process site tags and perform SEO checks for pages with invalid H1s', async () => {
Expand Down Expand Up @@ -378,13 +382,15 @@ describe('Meta Tags', () => {
}))).returns({
Body: {
transformToString: () => JSON.stringify({
tags: {
title: 'This is an SEO optimal page1 valid title.',
description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars',
'This is second h1 tag on same page',
],
scrapeResult: {
tags: {
title: 'This is an SEO optimal page1 valid title.',
description: 'This is a dummy description that is optimal from SEO perspective for page1. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective due to its length exceeding 60 chars',
'This is second h1 tag on same page',
],
},
},
}),
},
Expand All @@ -396,12 +402,14 @@ describe('Meta Tags', () => {
}))).returns({
Body: {
transformToString: () => JSON.stringify({
tags: {
title: 'This is a SEO wise optimised page2 title.',
description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective',
],
scrapeResult: {
tags: {
title: 'This is a SEO wise optimised page2 title.',
description: 'This is a dummy description that is optimal from SEO perspective for page2. It has the correct length of characters, and is unique across all pages.',
h1: [
'This is an overly long H1 tag from SEO perspective',
],
},
},
}),
},
Expand Down Expand Up @@ -446,7 +454,7 @@ describe('Meta Tags', () => {
],
}));
expect(addAuditStub.calledOnce).to.be.true;
expect(logStub.info.calledThrice).to.be.true;
expect(logStub.info.callCount).to.equal(4);
});

it('should handle errors and return internalServerError', async () => {
Expand Down

0 comments on commit 531852f

Please sign in to comment.