Skip to content

Commit d65c365

Browse files
authored
Merge pull request #65 from ArkeeAgency/antoinekm/force-reindex
add custom urls option
2 parents b3ccccc + e0c31f8 commit d65c365

File tree

6 files changed

+65
-12
lines changed

6 files changed

+65
-12
lines changed

.changeset/new-rivers-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"google-indexing-script": patch
3+
---
4+
5+
Fix siteUrls convertions

.changeset/pretty-masks-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"google-indexing-script": minor
3+
---
4+
5+
Add custom URLs option

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ index("seogets.com", {
131131
.catch(console.error);
132132
```
133133

134-
Read the [API documentation](https://paka.dev/npm/google-indexing-script) for more details.
134+
Read the [API documentation](https://jsdocs.io/package/google-indexing-script) for more details.
135135

136136
</details>
137137

src/index.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getPageIndexingStatus,
88
convertToFilePath,
99
checkSiteUrl,
10+
checkCustomUrls,
1011
} from "./shared/gsc";
1112
import { getSitemapPages } from "./shared/sitemap";
1213
import { Status } from "./shared/types";
@@ -26,6 +27,7 @@ export type IndexOptions = {
2627
client_email?: string;
2728
private_key?: string;
2829
path?: string;
30+
urls?: string[];
2931
quota?: {
3032
rpmRetry?: boolean; // read requests per minute: retry after waiting time
3133
};
@@ -53,6 +55,9 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
5355
if (!options.path) {
5456
options.path = args["path"] || process.env.GIS_PATH;
5557
}
58+
if (!options.urls) {
59+
options.urls = args["urls"] ? args["urls"].split(",") : undefined;
60+
}
5661
if (!options.quota) {
5762
options.quota = {
5863
rpmRetry: args["rpm-retry"] === "true" || process.env.GIS_QUOTA_RPM_RETRY === "true",
@@ -72,15 +77,24 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
7277

7378
siteUrl = await checkSiteUrl(accessToken, siteUrl);
7479

75-
const [sitemaps, pages] = await getSitemapPages(accessToken, siteUrl);
80+
let pages = options.urls || [];
81+
if (pages.length === 0) {
82+
console.log(`🔎 Fetching sitemaps and pages...`);
83+
const [sitemaps, pagesFromSitemaps] = await getSitemapPages(accessToken, siteUrl);
7684

77-
if (sitemaps.length === 0) {
78-
console.error("❌ No sitemaps found, add them to Google Search Console and try again.");
79-
console.error("");
80-
process.exit(1);
81-
}
85+
if (sitemaps.length === 0) {
86+
console.error("❌ No sitemaps found, add them to Google Search Console and try again.");
87+
console.error("");
88+
process.exit(1);
89+
}
90+
91+
pages = pagesFromSitemaps;
8292

83-
console.log(`👉 Found ${pages.length} URLs in ${sitemaps.length} sitemap`);
93+
console.log(`👉 Found ${pages.length} URLs in ${sitemaps.length} sitemap`);
94+
} else {
95+
pages = checkCustomUrls(siteUrl, pages);
96+
console.log(`👉 Found ${pages.length} URLs in the provided list`);
97+
}
8498

8599
const statusPerUrl: Record<string, { status: Status; lastCheckedAt: string }> = existsSync(cachePath)
86100
? JSON.parse(readFileSync(cachePath, "utf8"))
@@ -109,7 +123,7 @@ export const index = async (input: string = process.argv[2], options: IndexOptio
109123
const shouldRecheck = (status: Status, lastCheckedAt: string) => {
110124
const shouldIndexIt = indexableStatuses.includes(status);
111125
const isOld = new Date(lastCheckedAt) < new Date(Date.now() - CACHE_TIMEOUT);
112-
return shouldIndexIt && isOld;;
126+
return shouldIndexIt && isOld;
113127
};
114128

115129
await batch(

src/shared/gsc.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ export async function checkSiteUrl(accessToken: string, siteUrl: string) {
9292
// Convert the site URL into all possible formats
9393
if (siteUrl.startsWith("https://")) {
9494
formattedUrls.push(siteUrl);
95-
formattedUrls.push(convertToHTTP(siteUrl));
95+
formattedUrls.push(convertToHTTP(siteUrl.replace("https://", "")));
9696
formattedUrls.push(convertToSCDomain(siteUrl));
9797
} else if (siteUrl.startsWith("http://")) {
9898
formattedUrls.push(siteUrl);
99-
formattedUrls.push(convertToHTTPS(siteUrl));
99+
formattedUrls.push(convertToHTTPS(siteUrl.replace("http://", "")));
100100
formattedUrls.push(convertToSCDomain(siteUrl));
101101
} else if (siteUrl.startsWith("sc-domain:")) {
102102
formattedUrls.push(siteUrl);
@@ -121,6 +121,35 @@ export async function checkSiteUrl(accessToken: string, siteUrl: string) {
121121
process.exit(1);
122122
}
123123

124+
/**
125+
* Checks if the given URLs are valid.
126+
* @param siteUrl - The URL of the site.
127+
* @param urls - The URLs to check.
128+
* @returns An array containing the corrected URLs if found, otherwise the original URLs
129+
*/
130+
export function checkCustomUrls(siteUrl: string, urls: string[]) {
131+
const protocol = siteUrl.startsWith("http://") ? "http://" : "https://";
132+
const domain = siteUrl.replace("https://", "").replace("http://", "").replace("sc-domain:", "");
133+
const formattedUrls: string[] = urls.map((url) => {
134+
url = url.trim();
135+
if (url.startsWith("/")) {
136+
// the url is a relative path (e.g. /about)
137+
return `${protocol}${domain}${url}`;
138+
} else if (url.startsWith("http://") || url.startsWith("https://")) {
139+
// the url is already a full url (e.g. https://domain.com/about)
140+
return url;
141+
} else if (url.startsWith(domain)) {
142+
// the url is a full url without the protocol (e.g. domain.com/about)
143+
return `${protocol}${url}`;
144+
} else {
145+
// the url is a relative path without the leading slash (e.g. about)
146+
return `${protocol}${domain}/${url}`;
147+
}
148+
});
149+
150+
return formattedUrls;
151+
}
152+
124153
/**
125154
* Retrieves the indexing status of a page.
126155
* @param accessToken - The access token for authentication.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"sourceMap": true,
88
"outDir": "./dist",
99
"moduleResolution": "node",
10-
"removeComments": true,
10+
"removeComments": false,
1111
"noImplicitAny": false,
1212
"strictNullChecks": true,
1313
"strictFunctionTypes": true,

0 commit comments

Comments
 (0)