@sphido/sitemap 2.0.2 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/sitemap.js CHANGED
@@ -4,8 +4,9 @@ import {dirname} from 'node:path';
4
4
 
5
5
  /**
6
6
  * Generate XML sitemap
7
+ * @see https://www.sitemaps.org/protocol.html
7
8
  * @param {string} file
8
- * @returns {Object<{add: add, end: end}>}
9
+ * @return {Promise<{add({url: string, date?: Date, priority?: Number, changefreq?: string}=): void, end(): void}>}
9
10
  */
10
11
  export async function createSitemap(file = 'public/sitemap.xml') {
11
12
  // Create directory
@@ -20,14 +21,30 @@ export async function createSitemap(file = 'public/sitemap.xml') {
20
21
 
21
22
  // Create stream
22
23
  const sitemap = createWriteStream(file, {flags: 'a'});
23
- sitemap.write('<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n');
24
+ sitemap.write('<?xml version="1.0" encoding="UTF-8"?>\r\n');
25
+ sitemap.write('<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">\r\n');
24
26
 
25
27
  return {
28
+ /**
29
+ * Add URL to sitemap
30
+ * @param {string} url
31
+ * @param {Date} date
32
+ * @param {Number} priority
33
+ * @param {string} changefreq
34
+ */
26
35
 
27
- add(url, lastmod = new Date(), priority = 0.8) {
28
- sitemap.write(`\t<url><loc>${url}</loc><lastmod>${lastmod.toISOString()}</lastmod><priority>${priority}</priority></url>\r\n`);
36
+ add({url, date = new Date(), priority = 0.5, changefreq = 'monthly'} = {}) {
37
+ sitemap.write('\t<url>\r\n');
38
+ sitemap.write(`\t\t<loc>${url}</loc>\r\n`);
39
+ sitemap.write(`\t\t<lastmod>${date.toISOString()}</lastmod>\r\n`);
40
+ sitemap.write(`\t\t<priority>${priority}</priority>\r\n`);
41
+ sitemap.write(`\t\t<changefreq>${changefreq}</changefreq>\r\n`);
42
+ sitemap.write('\t</url>\r\n');
29
43
  },
30
44
 
45
+ /**
46
+ * Save sitemap end
47
+ */
31
48
  end() {
32
49
  sitemap.write('</urlset>');
33
50
  sitemap.end();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sphido/sitemap",
3
- "version": "2.0.2",
3
+ "version": "2.0.5",
4
4
  "author": {
5
5
  "name": "Roman Ožana",
6
6
  "email": "roman@ozana.cz",
@@ -15,10 +15,11 @@
15
15
  "license": "MIT",
16
16
  "devDependencies": {
17
17
  "@sindresorhus/slugify": "^2.1.0",
18
- "ava": "^4.2.0"
18
+ "ava": "^4.2.0",
19
+ "got": "^12.1.0"
19
20
  },
20
21
  "scripts": {
21
22
  "test": "ava"
22
23
  },
23
- "gitHead": "df860e9742cc0228338438748f62ea9733ff0872"
24
+ "gitHead": "2ec312ef7e5bf2c7c34694f66d5463ae79a00cf6"
24
25
  }
package/readme.md CHANGED
@@ -14,29 +14,65 @@ yarn add @sphido/sitemap
14
14
  import {dirname, relative, join} from 'node:path';
15
15
  import {getPages, allPages} from '@sphido/core';
16
16
  import slugify from '@sindresorhus/slugify';
17
- import {createSitemap} from '../lib/sitemap.js';
17
+ import {createSitemap, pingSitemap} from '@sphido/sitemap';
18
+ import got from 'got';
18
19
 
19
20
  const pages = await getPages({path: 'content'});
20
21
  const map = await createSitemap('sitemap.xml');
21
22
 
22
- map.add('https://sphido.org', new Date(), 1);
23
+ map.add({url: 'https://sphido.org', priority: 1});
23
24
 
24
25
  for (const page of await allPages(pages)) {
25
26
  page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
26
- map.add('https://sphido.org' + page.slug, new Date());
27
+ map.add({
28
+ url: 'https://sphido.org' + page.slug,
29
+ date: new Date(),
30
+ });
27
31
  }
28
32
 
29
33
  map.end();
34
+
35
+ // ping Google about new sitemap
36
+ await got.get('https://www.google.com/webmasters/tools/ping?sitemap=https://sphido.org/sitemap.xml');
30
37
  ```
31
38
 
32
- ## Source codes
39
+ ## Let them know
40
+
41
+ ### Google
42
+
43
+ Google expects the standard sitemap protocol in all formats.
44
+ Google does not currently consume the `<priority>` attribute in sitemaps.
45
+ You can also submit RSS or Atom files.
46
+
47
+ Google doesn't check a sitemap every time a site is crawled; a sitemap is checked only
48
+ the first time that we notice it, and thereafter only when you ping us to let us know
49
+ that it's changed. Alert Google about a sitemap only when it's new or updated;
50
+ don't submit or ping unchanged sitemaps multiple times.
51
+
52
+ You can use [got](https://github.com/sindresorhus/got) - Human-friendly and powerful HTTP request library for Node.js
53
+ or visit [URL manually](https://www.google.com/webmasters/tools/ping?sitemap=https://sphido.org/sitemap.xml)
54
+
55
+ ```javascript
56
+ import got from 'got';
57
+ await got.get('https://www.google.com/webmasters/tools/ping?sitemap=https://sphido.org/sitemap.xml');
58
+ ```
33
59
 
34
- https://github.com/sphido/sphido/tree/main/packages/sphido-sitemap
35
60
 
36
- ## TODO
61
+ ### Bing, Seznam, Yandex
37
62
 
38
- Ping Google about changes:
63
+ [IndexNow](https://www.indexnow.org/) is an open-source protocol that allows website publishers to
64
+ instantly index across participating search engines, updating results based upon the latest content changes.
65
+ [IndexNow](https://www.indexnow.org/) protocol is supported by [Microsoft Bing](https://www.bing.com/indexnow), [Seznam.cz](https://www.seznam.cz/) and Yandex.
66
+
67
+ 1. **Generate an API Key** – This is submitted alongside URLs to ensure ownership of the domain. You can use [Microsoft Bing API Key generator](https://www.bing.com/indexnow).
68
+ 2. **Host API Key** – Your API key is hosted on the root directory in `txt` format.
69
+ 3. **Submit URLs with Parameters** – You can either submit URLs individually or in bulk as sitemap file.
39
70
 
40
71
  ```text
41
- https://www.google.com/webmasters/tools/ping?sitemap=https://www.mydomain.com/sitemap.xml
42
- ```
72
+ https://www.bing.com/indexnow?url=[URL]&key=[YOUR KEY]
73
+ https://search.seznam.cz/indexnow?url=[URL]&key=[YOUR KEY]
74
+ ```
75
+
76
+ ## Source codes
77
+
78
+ https://github.com/sphido/sphido/tree/main/packages/sphido-sitemap