basklee-svelte 0.9.7 → 0.9.8
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/README.md +10 -4
- package/package.json +1 -1
- package/runtime/marketplace.mjs +444 -87
- package/src/project-readme.mjs +12 -3
- package/src/sitemap.mjs +16 -7
package/README.md
CHANGED
|
@@ -347,10 +347,16 @@ npm run basklee:sitemap
|
|
|
347
347
|
```
|
|
348
348
|
|
|
349
349
|
The storefront serves `/robots.txt` and `/sitemap.xml` (public pages, categories,
|
|
350
|
-
products and journal publications in every language).
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
a
|
|
350
|
+
products and journal publications in every language). `/sitemap.xml` is a
|
|
351
|
+
sitemap index: its files (`?part=pages-1`, `products-1`, `products-2`, ...,
|
|
352
|
+
`journal-1`) hold up to 50,000 links each, within the 50,000 URLs and 50 MB a
|
|
353
|
+
search engine reads from one file, so a catalog of any size is listed. The
|
|
354
|
+
index gives each file the time its content last changed, and the files answer
|
|
355
|
+
compressed (gzip or brotli) with `ETag` and `Last-Modified`, so crawlers fetch
|
|
356
|
+
again only what changed. The sitemap is rebuilt from the backend once an hour,
|
|
357
|
+
and search engines get the previous one meanwhile; `npm run basklee:sitemap` rebuilds it at once, for
|
|
358
|
+
example after new publications, and the running storefront serves it without a
|
|
359
|
+
restart.
|
|
354
360
|
|
|
355
361
|
`npm set` belongs to npm's own configuration system; use the commands above.
|
|
356
362
|
The setup commands configure local integration files. They do not provision a
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "basklee-svelte",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.8",
|
|
4
4
|
"description": "SvelteKit 2 storefront for your Basklee marketplace: setup CLI, server API gateway, typed store and ready-made catalog, cart, checkout and account components in English, Russian and Spanish, plus an Android app build. Powered by Basklee: https://basklee.com",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
package/runtime/marketplace.mjs
CHANGED
|
@@ -6,6 +6,9 @@ import {
|
|
|
6
6
|
isIP,
|
|
7
7
|
setDefaultAutoSelectFamilyAttemptTimeout,
|
|
8
8
|
} from 'node:net';
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { promisify } from 'node:util';
|
|
11
|
+
import { brotliCompress, constants as zlibConstants, gzip } from 'node:zlib';
|
|
9
12
|
|
|
10
13
|
const environment = process.env;
|
|
11
14
|
|
|
@@ -474,16 +477,50 @@ export async function forwardMarketplace(request, configuration = {}) {
|
|
|
474
477
|
// moves it). "basklee-<framework> sitemap rebuild" rebuilds that file at
|
|
475
478
|
// once; the running storefront serves the new file without a restart.
|
|
476
479
|
// Paths in BASKLEE_NOINDEX_PATHS are left out when the sitemap is served.
|
|
480
|
+
//
|
|
481
|
+
// Search engines read at most 50,000 URLs and 50 MB from one sitemap file
|
|
482
|
+
// (Google, Bing, Yandex). /sitemap.xml is therefore a sitemap index of
|
|
483
|
+
// files of at most 50,000 links each (<loc> and hreflang alternates
|
|
484
|
+
// together, about 6 MB): /sitemap.xml?part=pages-1 (home, directories,
|
|
485
|
+
// categories), products-1, products-2, ... and journal-1, ... The files stay
|
|
486
|
+
// at the address of the index: a sitemap may list only addresses under its
|
|
487
|
+
// own folder. Products and publications go to files by ID range, so a new or
|
|
488
|
+
// removed product changes one file only. Every file remembers when its
|
|
489
|
+
// content last changed (`files` of the cache): that is its <lastmod> in the
|
|
490
|
+
// index and its Last-Modified, so a crawler reads again only the files that
|
|
491
|
+
// changed. Answers are compressed (brotli or gzip, as the crawler accepts),
|
|
492
|
+
// carry an ETag, and a crawler asking with If-None-Match or
|
|
493
|
+
// If-Modified-Since gets 304 when the file is unchanged.
|
|
494
|
+
//
|
|
495
|
+
// The products come from the backend's sitemap list, 5,000 by ID a request,
|
|
496
|
+
// so a shop of 150,000 products takes about 30 requests. A copy older than
|
|
497
|
+
// an hour is served at once while a new one is built.
|
|
477
498
|
|
|
478
499
|
export const SITEMAP_LIFETIME = 60 * 60 * 1000;
|
|
479
|
-
|
|
480
|
-
const
|
|
481
|
-
|
|
482
|
-
const
|
|
500
|
+
/** After a failed build the copy at hand is served for this long before the next try. */
|
|
501
|
+
const sitemapRetryPause = 5 * 60 * 1000;
|
|
502
|
+
/** Journal publications: the backend gives 35 a page, so up to 70,000 publications. */
|
|
503
|
+
const sitemapPageSize = 35;
|
|
504
|
+
const sitemapMaxPages = 2_000;
|
|
505
|
+
/** Pages of the backend's product list (5,000 products each). */
|
|
506
|
+
const sitemapProductPages = 4_000;
|
|
507
|
+
/** Links (<loc> and hreflang alternates) of one sitemap file. */
|
|
508
|
+
const sitemapPartLinks = 50_000;
|
|
509
|
+
const sitemapVersion = 1;
|
|
510
|
+
/** The index's own entry in `files` (no file is named so). */
|
|
511
|
+
const sitemapIndexKey = '@index';
|
|
512
|
+
const gzipAsync = promisify(gzip);
|
|
513
|
+
const brotliAsync = promisify(brotliCompress);
|
|
483
514
|
/** Built sitemaps of this process, for when the cache folder is read-only. */
|
|
484
|
-
const sitemapMemory
|
|
515
|
+
const sitemapMemory = new Map();
|
|
485
516
|
/** @type {Map<string, Promise<object>>} */
|
|
486
|
-
const sitemapBuilds
|
|
517
|
+
const sitemapBuilds = new Map();
|
|
518
|
+
/** When the last build of a domain failed (time in ms). */
|
|
519
|
+
const sitemapFailures = new Map();
|
|
520
|
+
/** The cache files read: path -> {mtimeMs, size, sitemap}, so a crawl of many files parses each once. */
|
|
521
|
+
const sitemapFiles = new Map();
|
|
522
|
+
/** The files of each sitemap object: sitemap -> {noindex, served}. */
|
|
523
|
+
const sitemapPartsMemo = new WeakMap();
|
|
487
524
|
|
|
488
525
|
/** The public origin a crawler used: the storefront host, or a local one. */
|
|
489
526
|
export function marketplacePublicOrigin(request, tenantDomain = '') {
|
|
@@ -540,7 +577,7 @@ export async function marketplaceRobotsResponse(request, configuration = {}) {
|
|
|
540
577
|
domain = '';
|
|
541
578
|
}
|
|
542
579
|
return new Response(
|
|
543
|
-
marketplaceRobots(marketplacePublicOrigin(request, domain)),
|
|
580
|
+
request.method === 'HEAD' ? null : marketplaceRobots(marketplacePublicOrigin(request, domain)),
|
|
544
581
|
{
|
|
545
582
|
headers: {
|
|
546
583
|
'Content-Type': 'text/plain; charset=utf-8',
|
|
@@ -607,20 +644,44 @@ export function sitemapApiPath(paths, name) {
|
|
|
607
644
|
return null;
|
|
608
645
|
}
|
|
609
646
|
|
|
610
|
-
/**
|
|
647
|
+
/**
|
|
648
|
+
* Public categories, parents before children. A category under a hidden one
|
|
649
|
+
* is left out: its page is not found in the storefront (findCatalogCategory
|
|
650
|
+
* looks for it through public categories only).
|
|
651
|
+
*/
|
|
611
652
|
export function sitemapCategoryIds(categories) {
|
|
612
653
|
const ids = [];
|
|
613
654
|
const visit = (category) => {
|
|
614
655
|
const id = publicId(category?.id);
|
|
615
|
-
if (id
|
|
616
|
-
|
|
656
|
+
if (!id || !category?.is_public) {
|
|
657
|
+
return;
|
|
617
658
|
}
|
|
618
|
-
|
|
659
|
+
ids.push(id);
|
|
660
|
+
(Array.isArray(category.childs) ? category.childs : []).forEach(visit);
|
|
619
661
|
};
|
|
620
662
|
(Array.isArray(categories) ? categories : []).forEach(visit);
|
|
621
663
|
return [...new Set(ids)];
|
|
622
664
|
}
|
|
623
665
|
|
|
666
|
+
/**
|
|
667
|
+
* The languages a page is listed in: those of its own `locales` (a
|
|
668
|
+
* publication written in some languages only) among the marketplace's, all
|
|
669
|
+
* of the marketplace's otherwise, [''] (no language link) when the
|
|
670
|
+
* marketplace has none. [] leaves the page out.
|
|
671
|
+
* @param {{locales?: string[]}} page @param {string[]} locales
|
|
672
|
+
*/
|
|
673
|
+
function sitemapPageCodes(page, locales) {
|
|
674
|
+
if (page.locales) {
|
|
675
|
+
return locales.filter((code) => page.locales.includes(code));
|
|
676
|
+
}
|
|
677
|
+
return locales.length ? locales : [''];
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** Links of a page listed in `count` languages: a <loc> per language, each with every language as an alternate. */
|
|
681
|
+
function sitemapPageLinks(count) {
|
|
682
|
+
return count > 1 ? count * (count + 1) : count;
|
|
683
|
+
}
|
|
684
|
+
|
|
624
685
|
/**
|
|
625
686
|
* One <url> per page and language, each with alternates for its languages.
|
|
626
687
|
* A page with its own `locales` (a publication written in some languages
|
|
@@ -639,22 +700,13 @@ export function marketplaceSitemapXml(origin, pages, locales) {
|
|
|
639
700
|
};
|
|
640
701
|
const urls = [];
|
|
641
702
|
for (const page of pages) {
|
|
642
|
-
const
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
if (page.locales && !own.length) {
|
|
646
|
-
continue;
|
|
647
|
-
}
|
|
648
|
-
const codes = own.length ? own : [''];
|
|
649
|
-
const alternates = own.length > 1
|
|
650
|
-
? own.map((code) =>
|
|
703
|
+
const codes = sitemapPageCodes(page, locales);
|
|
704
|
+
const alternates = codes.length > 1
|
|
705
|
+
? codes.map((code) =>
|
|
651
706
|
` <xhtml:link rel="alternate" hreflang="${escapeXml(code)}" href="${href(page.path, code)}" />\n`).join('')
|
|
652
707
|
: '';
|
|
653
708
|
const lastmod = page.lastmod ? ` <lastmod>${escapeXml(page.lastmod)}</lastmod>\n` : '';
|
|
654
709
|
for (const code of codes) {
|
|
655
|
-
if (urls.length >= sitemapMaxUrls) {
|
|
656
|
-
break;
|
|
657
|
-
}
|
|
658
710
|
urls.push(` <url>\n <loc>${href(page.path, code)}</loc>\n${lastmod}${alternates} </url>\n`);
|
|
659
711
|
}
|
|
660
712
|
}
|
|
@@ -664,6 +716,151 @@ export function marketplaceSitemapXml(origin, pages, locales) {
|
|
|
664
716
|
'</urlset>\n';
|
|
665
717
|
}
|
|
666
718
|
|
|
719
|
+
const sitemapSectionOrder = { pages: 0, products: 1, journal: 2 };
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* The files of a sitemap: the other pages (home, directories, categories)
|
|
723
|
+
* in order, cut into files of at most `limit` links (<loc> and hreflang
|
|
724
|
+
* alternates together); products and journal publications by ID range, so
|
|
725
|
+
* that each range of IDs fills at most one file in every language: file N
|
|
726
|
+
* of products holds IDs from (N - 1) * range + 1 to N * range, and a new or
|
|
727
|
+
* removed product changes one file only. Files with no page are not listed.
|
|
728
|
+
* A page keeps all its languages in one file.
|
|
729
|
+
* @param {{path: string, lastmod?: string | null, locales?: string[]}[]} pages
|
|
730
|
+
* @param {string[]} locales
|
|
731
|
+
* @returns {{name: string, pages: object[], urls: number}[]}
|
|
732
|
+
*/
|
|
733
|
+
export function sitemapParts(pages, locales, limit = sitemapPartLinks) {
|
|
734
|
+
const range = Math.max(1, Math.floor(limit / sitemapPageLinks(Math.max(locales.length, 1))));
|
|
735
|
+
const parts = new Map();
|
|
736
|
+
let open = null;
|
|
737
|
+
let count = 0;
|
|
738
|
+
for (const page of pages) {
|
|
739
|
+
const codes = sitemapPageCodes(page, locales);
|
|
740
|
+
if (!codes.length) {
|
|
741
|
+
continue;
|
|
742
|
+
}
|
|
743
|
+
const links = sitemapPageLinks(codes.length);
|
|
744
|
+
const match = /^\/(product|journal)\/([1-9]\d*)$/.exec(page.path);
|
|
745
|
+
let part;
|
|
746
|
+
if (match) {
|
|
747
|
+
const section = match[1] === 'product' ? 'products' : 'journal';
|
|
748
|
+
const number = Math.floor((Number(match[2]) - 1) / range) + 1;
|
|
749
|
+
const name = `${section}-${number}`;
|
|
750
|
+
part = parts.get(name);
|
|
751
|
+
if (!part) {
|
|
752
|
+
part = { name, section, number, pages: [], urls: 0, links: 0 };
|
|
753
|
+
parts.set(name, part);
|
|
754
|
+
}
|
|
755
|
+
} else {
|
|
756
|
+
if (!open || open.links + links > limit) {
|
|
757
|
+
count++;
|
|
758
|
+
open = { name: `pages-${count}`, section: 'pages', number: count, pages: [], urls: 0, links: 0 };
|
|
759
|
+
parts.set(open.name, open);
|
|
760
|
+
}
|
|
761
|
+
part = open;
|
|
762
|
+
}
|
|
763
|
+
part.pages.push(page);
|
|
764
|
+
part.urls += codes.length;
|
|
765
|
+
part.links += links;
|
|
766
|
+
}
|
|
767
|
+
return [...parts.values()]
|
|
768
|
+
.sort((left, right) =>
|
|
769
|
+
sitemapSectionOrder[left.section] - sitemapSectionOrder[right.section] || left.number - right.number)
|
|
770
|
+
.map(({ name, pages: list, urls }) => ({ name, pages: list, urls }));
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/** A short digest of what a sitemap file lists (its addresses come from these and the origin). */
|
|
774
|
+
function sitemapDigest(lines) {
|
|
775
|
+
const hash = createHash('sha256');
|
|
776
|
+
for (const line of lines) {
|
|
777
|
+
hash.update(line);
|
|
778
|
+
hash.update('\n');
|
|
779
|
+
}
|
|
780
|
+
return hash.digest('base64url').slice(0, 27);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
/** @param {{pages: object[]}} part @param {string[]} locales */
|
|
784
|
+
function partDigest(part, locales) {
|
|
785
|
+
return sitemapDigest([
|
|
786
|
+
locales.join(','),
|
|
787
|
+
...part.pages.map((page) =>
|
|
788
|
+
`${page.path}\t${page.lastmod ?? ''}\t${page.locales ? page.locales.join(',') : '*'}`),
|
|
789
|
+
]);
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/** ISO 8601 to the second, as <lastmod> gives it. */
|
|
793
|
+
function sitemapSecond(value) {
|
|
794
|
+
return new Date(Math.floor(Date.parse(value) / 1000) * 1000).toISOString().replace(/\.000Z$/, 'Z');
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* The served files of a built sitemap (BASKLEE_NOINDEX_PATHS applied), each
|
|
799
|
+
* with its digest and the time its content last changed: from the
|
|
800
|
+
* sitemap's `files` while the digest is the same, the build time otherwise.
|
|
801
|
+
* Worked out once per sitemap object.
|
|
802
|
+
* @returns {{parts: {name: string, pages: object[], urls: number, digest: string, since: string}[], index: {digest: string, since: string}}}
|
|
803
|
+
*/
|
|
804
|
+
function servedFiles(sitemap) {
|
|
805
|
+
const noindex = String(environment.BASKLEE_NOINDEX_PATHS ?? '');
|
|
806
|
+
const memo = sitemapPartsMemo.get(sitemap);
|
|
807
|
+
if (memo?.noindex === noindex) {
|
|
808
|
+
return memo.served;
|
|
809
|
+
}
|
|
810
|
+
const served = sitemapFileDates(sitemap, sitemap.files, noindex);
|
|
811
|
+
sitemapPartsMemo.set(sitemap, { noindex, served });
|
|
812
|
+
return served;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
/**
|
|
816
|
+
* The files of a sitemap with their digests, and since when each has its
|
|
817
|
+
* content: the time `known` (the files of the previous copy) gives for the
|
|
818
|
+
* same digest, the sitemap's build time for a new or changed file.
|
|
819
|
+
*/
|
|
820
|
+
function sitemapFileDates(sitemap, known, noindex = environment.BASKLEE_NOINDEX_PATHS) {
|
|
821
|
+
const built = sitemapSecond(sitemap.built);
|
|
822
|
+
const before = known && typeof known === 'object' ? known : {};
|
|
823
|
+
const since = (key, digest) => {
|
|
824
|
+
const entry = before[key];
|
|
825
|
+
return entry?.digest === digest && Number.isFinite(Date.parse(entry.since)) && Date.parse(entry.since) <= Date.parse(built)
|
|
826
|
+
? sitemapSecond(entry.since)
|
|
827
|
+
: built;
|
|
828
|
+
};
|
|
829
|
+
const parts = sitemapParts(sitemapIndexedPages(sitemap.pages, noindex), sitemap.locales).map((part) => {
|
|
830
|
+
const digest = partDigest(part, sitemap.locales);
|
|
831
|
+
return { ...part, digest, since: since(part.name, digest) };
|
|
832
|
+
});
|
|
833
|
+
const digest = sitemapDigest(parts.map((part) => `${part.name}\t${part.since}`));
|
|
834
|
+
return { parts, index: { digest, since: since(sitemapIndexKey, digest) } };
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** The `files` a new copy stores: digest and since of every file and of the index. */
|
|
838
|
+
function sitemapFilesRecord(served) {
|
|
839
|
+
const files = { [sitemapIndexKey]: served.index };
|
|
840
|
+
for (const part of served.parts) {
|
|
841
|
+
files[part.name] = { digest: part.digest, since: part.since };
|
|
842
|
+
}
|
|
843
|
+
return files;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* The sitemap index: one <sitemap> per file, at /sitemap.xml?part=<name>,
|
|
848
|
+
* with the time the file's content last changed.
|
|
849
|
+
* @param {string} origin @param {{name: string, since?: string | null}[]} parts
|
|
850
|
+
*/
|
|
851
|
+
export function marketplaceSitemapIndexXml(origin, parts) {
|
|
852
|
+
const entries = parts.map((part) => {
|
|
853
|
+
const url = new URL('/sitemap.xml', `${origin}/`);
|
|
854
|
+
url.searchParams.set('part', part.name);
|
|
855
|
+
const lastmod = part.since ? ` <lastmod>${escapeXml(part.since)}</lastmod>\n` : '';
|
|
856
|
+
return ` <sitemap>\n <loc>${escapeXml(url.toString())}</loc>\n${lastmod} </sitemap>\n`;
|
|
857
|
+
});
|
|
858
|
+
return '<?xml version="1.0" encoding="UTF-8"?>\n' +
|
|
859
|
+
'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n' +
|
|
860
|
+
entries.join('') +
|
|
861
|
+
'</sitemapindex>\n';
|
|
862
|
+
}
|
|
863
|
+
|
|
667
864
|
/** @param {{domain: string, api: URL}} tenant @param {string} path @param {unknown} body */
|
|
668
865
|
async function sitemapRequest(tenant, path, body) {
|
|
669
866
|
const response = await fetch(new URL(path, tenant.api.origin), {
|
|
@@ -711,10 +908,37 @@ async function sitemapList(tenant, path, key, filters = {}) {
|
|
|
711
908
|
throw new Error(`${path} has more than ${sitemapMaxPages} pages`);
|
|
712
909
|
}
|
|
713
910
|
|
|
714
|
-
/**
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
911
|
+
/**
|
|
912
|
+
* The products of the sitemap from the backend's list (the products a
|
|
913
|
+
* shopper of the domain can open, in public categories), 5,000 by ID a
|
|
914
|
+
* request: `after` is the last ID of the previous page.
|
|
915
|
+
*/
|
|
916
|
+
async function sitemapProducts(tenant) {
|
|
917
|
+
const path = '/api/sitemapload/products';
|
|
918
|
+
const products = new Map();
|
|
919
|
+
let after = 0;
|
|
920
|
+
for (let count = 0; count < sitemapProductPages; count++) {
|
|
921
|
+
const payload = await sitemapRequest(tenant, path, { after });
|
|
922
|
+
const list = payload?.page_data?.products;
|
|
923
|
+
if (!Array.isArray(list?.data)) {
|
|
924
|
+
throw new Error(`${path} returned no product list`);
|
|
925
|
+
}
|
|
926
|
+
for (const record of list.data) {
|
|
927
|
+
const id = publicId(record?.id);
|
|
928
|
+
if (id) {
|
|
929
|
+
products.set(id, record);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
if (list.next_after === null || list.next_after === undefined) {
|
|
933
|
+
return [...products.values()];
|
|
934
|
+
}
|
|
935
|
+
const next = publicId(list.next_after);
|
|
936
|
+
if (!next || next <= after) {
|
|
937
|
+
throw new Error(`${path} returned a wrong next page`);
|
|
938
|
+
}
|
|
939
|
+
after = next;
|
|
940
|
+
}
|
|
941
|
+
throw new Error(`${path} has more than ${sitemapProductPages} pages`);
|
|
718
942
|
}
|
|
719
943
|
|
|
720
944
|
/** Blog publications that are published. */
|
|
@@ -730,8 +954,12 @@ function postLocales(post) {
|
|
|
730
954
|
.map(([code]) => code.toLowerCase());
|
|
731
955
|
}
|
|
732
956
|
|
|
733
|
-
/**
|
|
734
|
-
|
|
957
|
+
/**
|
|
958
|
+
* Build the sitemap of a domain. `previous` (the copy at hand) gives the
|
|
959
|
+
* files whose content did not change the time they last changed.
|
|
960
|
+
* @param {{domain: string, api: URL}} tenant @param {object | null} previous
|
|
961
|
+
*/
|
|
962
|
+
async function buildMarketplaceSitemap(tenant, previous = null) {
|
|
735
963
|
const appload = await sitemapRequest(tenant, '/api/appload', {
|
|
736
964
|
detalize_mode: 'light',
|
|
737
965
|
ignore_cache: false,
|
|
@@ -744,11 +972,8 @@ async function buildMarketplaceSitemap(tenant) {
|
|
|
744
972
|
const locales = sitemapLocales(init.locale);
|
|
745
973
|
const categories = sitemapCategoryIds(init.catalog);
|
|
746
974
|
const blogPath = sitemapApiPath(paths, 'blog_posts_get');
|
|
747
|
-
const searchPath = sitemapApiPath(paths, 'products_search');
|
|
748
975
|
const [products, posts] = await Promise.all([
|
|
749
|
-
|
|
750
|
-
? sitemapList(tenant, searchPath, 'products', { categories })
|
|
751
|
-
: [],
|
|
976
|
+
categories.length ? sitemapProducts(tenant) : [],
|
|
752
977
|
blogPath ? sitemapList(tenant, blogPath, 'posts') : [],
|
|
753
978
|
]);
|
|
754
979
|
const postPages = posts
|
|
@@ -772,32 +997,32 @@ async function buildMarketplaceSitemap(tenant) {
|
|
|
772
997
|
{ path: '/privacy' },
|
|
773
998
|
...categories.map((id) => ({ path: `/category/${id}` })),
|
|
774
999
|
...products
|
|
775
|
-
.
|
|
1000
|
+
.sort((left, right) => left.id - right.id)
|
|
776
1001
|
.map((product) => ({
|
|
777
1002
|
path: `/product/${product.id}`,
|
|
778
1003
|
lastmod: sitemapLastmod(product.date_updated),
|
|
779
1004
|
})),
|
|
780
1005
|
...postPages,
|
|
781
1006
|
];
|
|
782
|
-
|
|
1007
|
+
const sitemap = {
|
|
783
1008
|
version: sitemapVersion,
|
|
784
1009
|
domain: tenant.domain,
|
|
785
1010
|
built: new Date().toISOString(),
|
|
786
1011
|
locales,
|
|
787
1012
|
pages,
|
|
788
1013
|
};
|
|
1014
|
+
sitemap.files = sitemapFilesRecord(sitemapFileDates(sitemap, previous?.files));
|
|
1015
|
+
return sitemap;
|
|
789
1016
|
}
|
|
790
1017
|
|
|
791
|
-
/** How many <url> entries a built sitemap has. */
|
|
1018
|
+
/** How many <url> entries a built sitemap has (as served). */
|
|
792
1019
|
export function sitemapUrlCount(sitemap) {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
}
|
|
800
|
-
return Math.min(count, sitemapMaxUrls);
|
|
1020
|
+
return servedFiles(sitemap).parts.reduce((count, part) => count + part.urls, 0);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
/** How many sitemap files the index of a built sitemap lists (as served). */
|
|
1024
|
+
export function sitemapFileCount(sitemap) {
|
|
1025
|
+
return servedFiles(sitemap).parts.length;
|
|
801
1026
|
}
|
|
802
1027
|
|
|
803
1028
|
/** The cache folder of built sitemaps. */
|
|
@@ -812,21 +1037,42 @@ function sitemapCachePath(domain, root) {
|
|
|
812
1037
|
return resolve(sitemapCacheDirectory(root), `${domain}.json`);
|
|
813
1038
|
}
|
|
814
1039
|
|
|
815
|
-
/**
|
|
1040
|
+
/** @param {unknown} sitemap @param {string} domain */
|
|
1041
|
+
function validSitemap(sitemap, domain) {
|
|
1042
|
+
return sitemap?.version === sitemapVersion &&
|
|
1043
|
+
sitemap.domain === domain &&
|
|
1044
|
+
Number.isFinite(Date.parse(sitemap.built)) &&
|
|
1045
|
+
Array.isArray(sitemap.locales) &&
|
|
1046
|
+
Array.isArray(sitemap.pages);
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
/**
|
|
1050
|
+
* A cached sitemap of the domain, or null. The file is parsed again only
|
|
1051
|
+
* when it changed: a crawler reads the index and every file in a row.
|
|
1052
|
+
* @param {string} domain
|
|
1053
|
+
*/
|
|
816
1054
|
export async function readSitemapCache(domain, root = process.cwd()) {
|
|
1055
|
+
const path = sitemapCachePath(domain, root);
|
|
1056
|
+
let info;
|
|
1057
|
+
try {
|
|
1058
|
+
info = await stat(path);
|
|
1059
|
+
} catch {
|
|
1060
|
+
sitemapFiles.delete(path);
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
const known = sitemapFiles.get(path);
|
|
1064
|
+
if (known && known.mtimeMs === info.mtimeMs && known.size === info.size) {
|
|
1065
|
+
return known.sitemap;
|
|
1066
|
+
}
|
|
817
1067
|
let sitemap;
|
|
818
1068
|
try {
|
|
819
|
-
sitemap = JSON.parse(await readFile(
|
|
1069
|
+
sitemap = JSON.parse(await readFile(path, 'utf8'));
|
|
820
1070
|
} catch {
|
|
821
1071
|
return null;
|
|
822
1072
|
}
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
Array.isArray(sitemap.locales) &&
|
|
827
|
-
Array.isArray(sitemap.pages)
|
|
828
|
-
? sitemap
|
|
829
|
-
: null;
|
|
1073
|
+
sitemap = validSitemap(sitemap, domain) ? sitemap : null;
|
|
1074
|
+
sitemapFiles.set(path, { mtimeMs: info.mtimeMs, size: info.size, sitemap });
|
|
1075
|
+
return sitemap;
|
|
830
1076
|
}
|
|
831
1077
|
|
|
832
1078
|
/**
|
|
@@ -845,13 +1091,15 @@ async function writeSitemapCache(sitemap, root) {
|
|
|
845
1091
|
await unlink(temporary).catch(() => {});
|
|
846
1092
|
throw error;
|
|
847
1093
|
}
|
|
1094
|
+
const info = await stat(path);
|
|
1095
|
+
sitemapFiles.set(path, { mtimeMs: info.mtimeMs, size: info.size, sitemap });
|
|
848
1096
|
if (process.getuid?.() === 0) {
|
|
849
1097
|
const owner = await stat(root);
|
|
850
1098
|
if (owner.uid !== 0) {
|
|
851
1099
|
let cursor = path;
|
|
852
1100
|
while (cursor !== root && !relative(root, cursor).startsWith('..')) {
|
|
853
|
-
const
|
|
854
|
-
if (
|
|
1101
|
+
const item = await stat(cursor);
|
|
1102
|
+
if (item.uid === 0) {
|
|
855
1103
|
await chown(cursor, owner.uid, owner.gid);
|
|
856
1104
|
}
|
|
857
1105
|
cursor = dirname(cursor);
|
|
@@ -861,19 +1109,23 @@ async function writeSitemapCache(sitemap, root) {
|
|
|
861
1109
|
return path;
|
|
862
1110
|
}
|
|
863
1111
|
|
|
864
|
-
/** @param {{domain: string, api: URL}} tenant @param {string} root */
|
|
865
|
-
function buildOnce(tenant, root) {
|
|
1112
|
+
/** @param {{domain: string, api: URL}} tenant @param {string} root @param {object | null} previous */
|
|
1113
|
+
function buildOnce(tenant, root, previous) {
|
|
866
1114
|
let build = sitemapBuilds.get(tenant.domain);
|
|
867
1115
|
if (!build) {
|
|
868
|
-
build = buildMarketplaceSitemap(tenant)
|
|
1116
|
+
build = buildMarketplaceSitemap(tenant, previous)
|
|
869
1117
|
.then(async (sitemap) => {
|
|
870
1118
|
sitemapMemory.set(tenant.domain, sitemap);
|
|
1119
|
+
sitemapFailures.delete(tenant.domain);
|
|
871
1120
|
try {
|
|
872
1121
|
await writeSitemapCache(sitemap, root);
|
|
873
1122
|
} catch (error) {
|
|
874
1123
|
console.error(`[basklee] Sitemap cache not written: ${error?.code ?? error?.message ?? error}`);
|
|
875
1124
|
}
|
|
876
1125
|
return sitemap;
|
|
1126
|
+
}, (error) => {
|
|
1127
|
+
sitemapFailures.set(tenant.domain, Date.now());
|
|
1128
|
+
throw error;
|
|
877
1129
|
})
|
|
878
1130
|
.finally(() => sitemapBuilds.delete(tenant.domain));
|
|
879
1131
|
sitemapBuilds.set(tenant.domain, build);
|
|
@@ -889,13 +1141,14 @@ function buildOnce(tenant, root) {
|
|
|
889
1141
|
export async function rebuildMarketplaceSitemap(domain, options = {}) {
|
|
890
1142
|
const root = resolve(options.root ?? process.cwd());
|
|
891
1143
|
const tenant = await readMarketplaceDomain(domain, { root });
|
|
892
|
-
const sitemap = await buildMarketplaceSitemap(tenant);
|
|
1144
|
+
const sitemap = await buildMarketplaceSitemap(tenant, await readSitemapCache(tenant.domain, root));
|
|
893
1145
|
const path = await writeSitemapCache(sitemap, root);
|
|
894
1146
|
return {
|
|
895
1147
|
domain: tenant.domain,
|
|
896
1148
|
path,
|
|
897
1149
|
built: sitemap.built,
|
|
898
1150
|
urls: sitemapUrlCount(sitemap),
|
|
1151
|
+
files: sitemapFileCount(sitemap),
|
|
899
1152
|
pages: sitemap.pages.length,
|
|
900
1153
|
publications: sitemap.pages.filter((page) => page.path.startsWith('/journal/')).length,
|
|
901
1154
|
};
|
|
@@ -919,7 +1172,104 @@ export async function marketplaceDomains(root = process.cwd()) {
|
|
|
919
1172
|
.sort();
|
|
920
1173
|
}
|
|
921
1174
|
|
|
922
|
-
/**
|
|
1175
|
+
/**
|
|
1176
|
+
* The sitemap of the domain to serve: the newest copy (file or memory)
|
|
1177
|
+
* while it is younger than an hour. An older copy is served at once while a
|
|
1178
|
+
* new one is built; without any copy the request waits for the build.
|
|
1179
|
+
* @param {{domain: string, api: URL}} tenant @param {string} root
|
|
1180
|
+
*/
|
|
1181
|
+
async function servedSitemap(tenant, root) {
|
|
1182
|
+
// The file wins when it is newer: a "sitemap rebuild" replaced it.
|
|
1183
|
+
const file = await readSitemapCache(tenant.domain, root);
|
|
1184
|
+
const memory = sitemapMemory.get(tenant.domain);
|
|
1185
|
+
const newest = [file, memory]
|
|
1186
|
+
.filter(Boolean)
|
|
1187
|
+
.sort((left, right) => Date.parse(right.built) - Date.parse(left.built))[0];
|
|
1188
|
+
if (newest && Date.now() - Date.parse(newest.built) < SITEMAP_LIFETIME) {
|
|
1189
|
+
return newest;
|
|
1190
|
+
}
|
|
1191
|
+
const failed = sitemapFailures.get(tenant.domain);
|
|
1192
|
+
const paused = failed !== undefined && Date.now() - failed < sitemapRetryPause;
|
|
1193
|
+
if (newest) {
|
|
1194
|
+
if (!paused) {
|
|
1195
|
+
buildOnce(tenant, root, newest).catch((error) => {
|
|
1196
|
+
console.error(`[basklee] Sitemap build failed: ${error?.message ?? error}`);
|
|
1197
|
+
});
|
|
1198
|
+
}
|
|
1199
|
+
return newest;
|
|
1200
|
+
}
|
|
1201
|
+
if (paused && !sitemapBuilds.has(tenant.domain)) {
|
|
1202
|
+
return null;
|
|
1203
|
+
}
|
|
1204
|
+
try {
|
|
1205
|
+
return await buildOnce(tenant, root, null);
|
|
1206
|
+
} catch (error) {
|
|
1207
|
+
console.error(`[basklee] Sitemap build failed: ${error?.message ?? error}`);
|
|
1208
|
+
return null;
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
/**
|
|
1213
|
+
* The compression a client accepts for a sitemap answer: brotli, else gzip,
|
|
1214
|
+
* else none (Accept-Encoding with its q-values; `*` stands for the codings
|
|
1215
|
+
* it does not name).
|
|
1216
|
+
* @param {string | null} header
|
|
1217
|
+
* @returns {'br' | 'gzip' | null}
|
|
1218
|
+
*/
|
|
1219
|
+
export function sitemapEncoding(header) {
|
|
1220
|
+
const qualities = new Map();
|
|
1221
|
+
for (const item of String(header ?? '').toLowerCase().split(',')) {
|
|
1222
|
+
const [name, ...parameters] = item.split(';').map((part) => part.trim());
|
|
1223
|
+
if (!name) {
|
|
1224
|
+
continue;
|
|
1225
|
+
}
|
|
1226
|
+
const q = parameters.find((parameter) => /^q=/.test(parameter));
|
|
1227
|
+
const quality = q ? Number(q.slice(2)) : 1;
|
|
1228
|
+
qualities.set(name === 'x-gzip' ? 'gzip' : name, Number.isFinite(quality) ? quality : 0);
|
|
1229
|
+
}
|
|
1230
|
+
const accepts = (name) => (qualities.get(name) ?? qualities.get('*') ?? 0) > 0;
|
|
1231
|
+
if (accepts('br')) {
|
|
1232
|
+
return 'br';
|
|
1233
|
+
}
|
|
1234
|
+
return accepts('gzip') ? 'gzip' : null;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
/** @param {Buffer} body @param {'br' | 'gzip' | null} encoding */
|
|
1238
|
+
function compressSitemap(body, encoding) {
|
|
1239
|
+
if (encoding === 'br') {
|
|
1240
|
+
return brotliAsync(body, {
|
|
1241
|
+
params: {
|
|
1242
|
+
[zlibConstants.BROTLI_PARAM_MODE]: zlibConstants.BROTLI_MODE_TEXT,
|
|
1243
|
+
[zlibConstants.BROTLI_PARAM_QUALITY]: 5,
|
|
1244
|
+
[zlibConstants.BROTLI_PARAM_SIZE_HINT]: body.length,
|
|
1245
|
+
},
|
|
1246
|
+
});
|
|
1247
|
+
}
|
|
1248
|
+
return encoding === 'gzip' ? gzipAsync(body, { level: 6 }) : Promise.resolve(body);
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Whether the crawler's copy is current: If-None-Match against the ETag
|
|
1253
|
+
* (weak comparison), else If-Modified-Since against the Last-Modified time.
|
|
1254
|
+
* @param {Request} request @param {string} etag @param {string} modified
|
|
1255
|
+
*/
|
|
1256
|
+
function sitemapNotModified(request, etag, modified) {
|
|
1257
|
+
const tags = request.headers.get('if-none-match');
|
|
1258
|
+
if (tags !== null) {
|
|
1259
|
+
const own = etag.replace(/^W\//, '');
|
|
1260
|
+
return tags.split(',').map((tag) => tag.trim().replace(/^W\//, '')).some((tag) => tag === '*' || tag === own);
|
|
1261
|
+
}
|
|
1262
|
+
const since = Date.parse(request.headers.get('if-modified-since') ?? '');
|
|
1263
|
+
return Number.isFinite(since) && Date.parse(modified) <= since;
|
|
1264
|
+
}
|
|
1265
|
+
|
|
1266
|
+
/**
|
|
1267
|
+
* /sitemap.xml: the sitemap index; /sitemap.xml?part=<name>: one of its
|
|
1268
|
+
* files (see sitemapParts). GET and HEAD. The answer is compressed here,
|
|
1269
|
+
* except inside Next.js (NEXT_RUNTIME is set): `next start` compresses route
|
|
1270
|
+
* answers itself and would compress a compressed one again.
|
|
1271
|
+
* @param {Request} request @param {{mimicFrontDomain?: string, compress?: boolean}} configuration
|
|
1272
|
+
*/
|
|
923
1273
|
export async function marketplaceSitemapResponse(request, configuration = {}) {
|
|
924
1274
|
let tenant;
|
|
925
1275
|
try {
|
|
@@ -927,33 +1277,40 @@ export async function marketplaceSitemapResponse(request, configuration = {}) {
|
|
|
927
1277
|
} catch {
|
|
928
1278
|
return failure(503, 'Storefront domain configuration is missing or invalid');
|
|
929
1279
|
}
|
|
930
|
-
const
|
|
931
|
-
const fresh = (sitemap) =>
|
|
932
|
-
sitemap && Date.now() - Date.parse(sitemap.built) < SITEMAP_LIFETIME;
|
|
933
|
-
// The file wins when it is newer: a "sitemap rebuild" replaced it.
|
|
934
|
-
const file = await readSitemapCache(tenant.domain, root);
|
|
935
|
-
const memory = sitemapMemory.get(tenant.domain);
|
|
936
|
-
const newest = [file, memory]
|
|
937
|
-
.filter(Boolean)
|
|
938
|
-
.sort((left, right) => Date.parse(right.built) - Date.parse(left.built))[0];
|
|
939
|
-
let sitemap = fresh(newest) ? newest : null;
|
|
1280
|
+
const sitemap = await servedSitemap(tenant, process.cwd());
|
|
940
1281
|
if (!sitemap) {
|
|
941
|
-
|
|
942
|
-
sitemap = await buildOnce(tenant, root);
|
|
943
|
-
} catch (error) {
|
|
944
|
-
console.error(`[basklee] Sitemap build failed: ${error?.message ?? error}`);
|
|
945
|
-
if (!newest) {
|
|
946
|
-
return failure(502, 'Marketplace backend is unavailable');
|
|
947
|
-
}
|
|
948
|
-
sitemap = newest;
|
|
949
|
-
}
|
|
1282
|
+
return failure(502, 'Marketplace backend is unavailable');
|
|
950
1283
|
}
|
|
951
|
-
const origin
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1284
|
+
const origin = marketplacePublicOrigin(request, tenant.domain);
|
|
1285
|
+
const served = servedFiles(sitemap);
|
|
1286
|
+
const name = new URL(request.url).searchParams.get('part');
|
|
1287
|
+
const part = name === null ? null : served.parts.find((item) => item.name === name);
|
|
1288
|
+
if (name !== null && !part) {
|
|
1289
|
+
return failure(404, 'No such sitemap file');
|
|
1290
|
+
}
|
|
1291
|
+
const file = part ?? served.index;
|
|
1292
|
+
const etag = `W/"${sitemapDigest([origin, file.digest])}"`;
|
|
1293
|
+
const modified = new Date(file.since).toUTCString();
|
|
1294
|
+
const headers = {
|
|
1295
|
+
'Content-Type': 'application/xml; charset=utf-8',
|
|
1296
|
+
'Cache-Control': 'public, max-age=3600',
|
|
1297
|
+
'ETag': etag,
|
|
1298
|
+
'Last-Modified': modified,
|
|
1299
|
+
'Vary': 'Accept-Encoding, Host, X-Forwarded-Host, X-Front-Domain',
|
|
1300
|
+
};
|
|
1301
|
+
if (sitemapNotModified(request, etag, modified)) {
|
|
1302
|
+
delete headers['Content-Type'];
|
|
1303
|
+
return new Response(null, { status: 304, headers });
|
|
1304
|
+
}
|
|
1305
|
+
const xml = part
|
|
1306
|
+
? marketplaceSitemapXml(origin, part.pages, sitemap.locales)
|
|
1307
|
+
: marketplaceSitemapIndexXml(origin, served.parts);
|
|
1308
|
+
const compress = configuration.compress ?? !environment.NEXT_RUNTIME;
|
|
1309
|
+
const encoding = compress ? sitemapEncoding(request.headers.get('accept-encoding')) : null;
|
|
1310
|
+
const body = await compressSitemap(Buffer.from(xml, 'utf8'), encoding);
|
|
1311
|
+
if (encoding) {
|
|
1312
|
+
headers['Content-Encoding'] = encoding;
|
|
1313
|
+
}
|
|
1314
|
+
headers['Content-Length'] = String(body.length);
|
|
1315
|
+
return new Response(request.method === 'HEAD' ? null : body, { headers });
|
|
959
1316
|
}
|
package/src/project-readme.mjs
CHANGED
|
@@ -388,9 +388,18 @@ instead, with nothing to set up.
|
|
|
388
388
|
An unknown domain never falls back to another tenant. \`/robots.txt\` points
|
|
389
389
|
crawlers to \`/sitemap.xml\`, which lists the public pages, categories, products
|
|
390
390
|
on sale and journal publications in every storefront language (each publication
|
|
391
|
-
only in the languages it is written in).
|
|
392
|
-
|
|
393
|
-
|
|
391
|
+
only in the languages it is written in). It is a sitemap index: its files
|
|
392
|
+
(\`/sitemap.xml?part=pages-1\`, \`products-1\`, \`products-2\`, ...,
|
|
393
|
+
\`journal-1\`) hold up to 50,000 links each, within the 50,000 URLs and 50 MB a
|
|
394
|
+
search engine reads from one file, so a catalog of any size is listed; submit
|
|
395
|
+
only \`/sitemap.xml\` to Google Search Console or Yandex Webmaster. The index
|
|
396
|
+
gives each file the time its content last changed, and the files answer
|
|
397
|
+
compressed (gzip or brotli; under Next.js, \`next start\` compresses them) with
|
|
398
|
+
\`ETag\` and \`Last-Modified\`, so crawlers fetch again only what changed. The sitemap
|
|
399
|
+
is built from the backend (the products by ID, 5,000 a request) and kept for an
|
|
400
|
+
hour in \`node_modules/.cache/basklee/sitemaps/\`
|
|
401
|
+
(\`BASKLEE_CACHE_DIR\` moves it); while a new one is built, crawlers get the
|
|
402
|
+
previous one. After you publish or change content, rebuild
|
|
394
403
|
it at once from the project folder; the running storefront serves the new
|
|
395
404
|
sitemap without a restart:
|
|
396
405
|
|
package/src/sitemap.mjs
CHANGED
|
@@ -23,19 +23,20 @@ async function projectRuntime(root) {
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
/**
|
|
26
|
-
* The storefront reads SITEMAPS_DIR
|
|
27
|
-
* project's .env; the command uses the same
|
|
28
|
-
*
|
|
26
|
+
* The storefront reads SITEMAPS_DIR, BASKLEE_CACHE_DIR and
|
|
27
|
+
* BASKLEE_NOINDEX_PATHS from the project's .env; the command uses the same
|
|
28
|
+
* folders and leaves out the same pages, so the files it builds are the ones
|
|
29
|
+
* the storefront serves. Variables already set in the shell win.
|
|
29
30
|
*/
|
|
30
31
|
async function projectEnvironment(root) {
|
|
31
32
|
let source = '';
|
|
32
33
|
try { source = await readFile(resolve(root, '.env'), 'utf8'); }
|
|
33
34
|
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
34
35
|
for (const line of source.split(/\r?\n/)) {
|
|
35
|
-
const match = /^\s*(?:export\s+)?(SITEMAPS_DIR|BASKLEE_CACHE_DIR)\s*=\s*(.*?)\s*$/.exec(line);
|
|
36
|
+
const match = /^\s*(?:export\s+)?(SITEMAPS_DIR|BASKLEE_CACHE_DIR|BASKLEE_NOINDEX_PATHS)\s*=\s*(.*?)\s*$/.exec(line);
|
|
36
37
|
if (!match || process.env[match[1]] !== undefined) continue;
|
|
37
38
|
const value = match[2].replace(/^(['"])(.*)\1$/, '$2');
|
|
38
|
-
if (value) process.env[match[1]] = resolve(root, value);
|
|
39
|
+
if (value) process.env[match[1]] = match[1] === 'BASKLEE_NOINDEX_PATHS' ? value : resolve(root, value);
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -50,6 +51,13 @@ async function domainsFor(runtime, root, domain) {
|
|
|
50
51
|
return domains;
|
|
51
52
|
}
|
|
52
53
|
|
|
54
|
+
/** "21 URLs in 3 sitemap files"; a project runtime from before the sitemap index counts no files. */
|
|
55
|
+
function size(runtime, sitemap) {
|
|
56
|
+
const urls = `${runtime.sitemapUrlCount(sitemap)} URLs`;
|
|
57
|
+
const files = typeof runtime.sitemapFileCount === 'function' ? runtime.sitemapFileCount(sitemap) : null;
|
|
58
|
+
return files === null ? urls : `${urls} in ${files} sitemap ${files === 1 ? 'file' : 'files'}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
53
61
|
function age(built, now = Date.now()) {
|
|
54
62
|
const minutes = Math.max(0, Math.round((now - Date.parse(built)) / 60_000));
|
|
55
63
|
return minutes < 60 ? `${minutes} min ago` : `${Math.floor(minutes / 60)} h ${minutes % 60} min ago`;
|
|
@@ -75,14 +83,15 @@ export async function sitemapCommand({ cwd, action, domain, log = console.log })
|
|
|
75
83
|
const sitemap = await runtime.readSitemapCache(name, root);
|
|
76
84
|
const stale = sitemap && Date.now() - Date.parse(sitemap.built) >= runtime.SITEMAP_LIFETIME;
|
|
77
85
|
log(sitemap
|
|
78
|
-
? `${name}: built ${sitemap.built} (${age(sitemap.built)}), ${runtime
|
|
86
|
+
? `${name}: built ${sitemap.built} (${age(sitemap.built)}), ${size(runtime, sitemap)}${stale ? '; rebuilt at the next crawl' : ''}`
|
|
79
87
|
: `${name}: not built yet; built at the first /sitemap.xml request or with: npx ${cli} sitemap rebuild`);
|
|
80
88
|
continue;
|
|
81
89
|
}
|
|
82
90
|
const started = Date.now();
|
|
83
91
|
try {
|
|
84
92
|
const done = await runtime.rebuildMarketplaceSitemap(name, { root });
|
|
85
|
-
|
|
93
|
+
const files = done.files === undefined ? '' : ` in ${done.files} sitemap ${done.files === 1 ? 'file' : 'files'}`;
|
|
94
|
+
log(`${name}: ${done.urls} URLs${files} (${done.pages} pages, ${done.publications} journal publications) in ${((Date.now() - started) / 1000).toFixed(1)} s`);
|
|
86
95
|
log(` saved ${relative(root, done.path)}; the running storefront serves it now at https://${name}/sitemap.xml`);
|
|
87
96
|
} catch (error) {
|
|
88
97
|
failed++;
|