-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathseo.js
196 lines (177 loc) · 6.93 KB
/
seo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const Apify = require('apify');
const Bluebird = require('bluebird');
const { injectJQuery } = Apify.utils.puppeteer;
const DEFAULT_SEO_PARAMS = {
maxTitleLength: 70,
minTitleLength: 10,
maxMetaDescriptionLength: 140,
maxLinksCount: 3000,
maxWordsCount: 350,
outputLinks: false,
workingStatusCodes: [200, 301, 302, 304],
};
/**
* @param {Puppeteer.Page} page
* @param {any} userParams
*/
async function basicSEO(page, userParams = {}) {
await injectJQuery(page);
const seoParams = {
...DEFAULT_SEO_PARAMS,
...userParams,
};
const { origin } = new URL(page.url());
const fetchInBrowser = (url) => page.evaluate(async (pUrl) => {
try {
const { status } = await window.fetch(pUrl, {
method: 'GET',
mode: 'no-cors',
headers: {
Accept: '*/*',
},
referrerPolicy: 'no-referrer',
});
return status;
} catch (e) {
return 500;
}
}, url);
const seo = await page.evaluate(async (params) => {
const $ = window.jQuery;
const result = {};
// Check flash content
if ($('script:contains(embedSWF)').length) result.isUsingFlash = true;
// -- Google Analytics
// Check for GA Object (e.g crawler can not find function(i,s,o,g,r,a,m) in meteor page like Apifier)
result.isGoogleAnalyticsObject = (typeof ga !== 'undefined');
// Check for GA function (function(i,s,o,g,r,a,m)) exists in page
result.isGoogleAnalyticsFunc = !!($('script:contains(function(i,s,o,g,r,a,m){i[\'GoogleAnalyticsObject\'])').length);
// -- Meta charset
result.isCharacterEncode = !!($('meta[charset]'));
// -- Meta description
result.isMetaDescription = !!($('meta[name=description]').length);
if (result.isMetaDescription) {
result.metaDescription = $('meta[name=description]').attr('content');
result.isMetaDescriptionEnoughLong = (result.metaDescription.length < params.maxMetaDescriptionLength);
}
// -- Doctype
result.isDoctype = !!(document.doctype);
// -- Title
if ($('title').length) {
result.isTitle = true;
result.title = $('title').text();
const titleLength = result.title.length;
result.isTitleEnoughLong = (titleLength <= params.maxTitleLength) && (titleLength >= params.minTitleLength);
} else result.isTitle = false;
// -- h1
const h1Count = $('h1').length;
result.isH1 = (h1Count > 0);
if (result.isH1) result.h1 = $('h1').text();
result.isH1OnlyOne = (h1Count === 1);
// -- h2
result.isH2 = !!($('h2').length);
// -- Links
const $allLinks = $('a');
result.linksCount = $allLinks.length;
result.isTooEnoughLinks = (result.linksCount < params.maxLinksCount);
result.internalNoFollowLinks = [];
$allLinks.each(function () {
if ($(this).attr('rel') === 'nofollow'
&& this.href.includes(window.location.hostname)) {
result.internalNoFollowLinks.push(this.href);
}
});
result.internalNoFollowLinksCount = result.internalNoFollowLinks.length;
// Check broken links
result.linkUrls = $allLinks
.filter((index, el) => {
const href = $(el).attr('href');
return href
&& !href.includes('javascript:')
&& !href.includes('mailto:');
}).map((index, el) => el.href)
.toArray();
result.internalLinks = $allLinks.filter((index, el) => {
const $el = $(el);
const href = $el.attr('href');
return $el.is('a[href]:not([target="_blank"]),a[href]:not([rel*="nofollow"]),a[href]:not([rel*="noreferrer"])')
&& href.includes(window.location.hostname)
&& !href.includes('javascript:')
&& !href.includes('mailto:');
})
.map((index, el) => el.href)
.toArray();
// -- images
result.imageUrls = [];
result.notOptimizedImages = [];
$('img').each(function () {
result.imageUrls.push(this.src);
if (!$(this).attr('alt')) result.notOptimizedImages.push(this.src);
});
result.notOptimizedImagesCount = result.notOptimizedImages.length;
// -- words count
result.wordsCount = document.body.innerText.split(/\b(\p{Letter}+)\b/gu).filter((s) => s).length;
result.isContentEnoughLong = (result.wordsCount < params.maxWordsCount);
// -- viewport
result.isViewport = !!($('meta[name=viewport]'));
// -- amp version if page
result.isAmp = !!($('html[⚡]') || $('html[amp]'));
// -- iframe check
result.isNotIframe = !($('iframe').length);
result.pageIsBlocked = $('meta[name=robots][content]')
.filter((index, s) => ['noindex', 'nofollow'].some((x) => s.content.includes(x)))
.length > 0;
return result;
}, seoParams);
const { workingStatusCodes } = seoParams;
seo.robotsFileExists = workingStatusCodes.includes(await fetchInBrowser(`${origin}/robots.txt`));
seo.faviconExists = workingStatusCodes.includes(await fetchInBrowser(`${origin}/favicon.ico`));
// Check broken links
const internalBrokenLinks = new Set();
const allBrokenLinks = new Set();
await Bluebird.map(seo.internalLinks, (url) => {
if (internalBrokenLinks.has(url)) {
return;
}
return fetchInBrowser(url).then((res) => {
if (!workingStatusCodes.includes(res)) {
internalBrokenLinks.add(url);
}
});
}, { concurrency: 2 });
seo.brokenLinksCount = internalBrokenLinks.size;
if (!seoParams.outputLinks) {
delete seo.internalLinks;
}
seo.brokenLinks = [...internalBrokenLinks];
await Bluebird.map(seo.linkUrls, (url) => {
if (internalBrokenLinks.has(url) || allBrokenLinks.has(url)) {
return;
}
return fetchInBrowser(url).then((res) => {
if (!workingStatusCodes.includes(res)) {
allBrokenLinks.add(url);
}
});
}, { concurrency: 2 });
seo.externalBrokenLinksCount = allBrokenLinks.size;
seo.externalBrokenLinks = [...allBrokenLinks];
if (!seoParams.linkUrls) {
delete seo.linkUrls;
}
// Check broken images
seo.brokenImages = [];
await Bluebird.map(seo.imageUrls, (imageUrl) => {
return fetchInBrowser(imageUrl).then((res) => {
if (!workingStatusCodes.includes(res)) {
seo.brokenImages.push(imageUrl);
}
});
}, { concurrency: 2 });
seo.brokenImagesCount = seo.brokenImages.length;
delete seo.imageUrls;
return seo;
}
module.exports = {
basicSEO,
};