nextjs-sitemap-generator
Advanced tools
Comparing version 0.2.1 to 0.3.0
219
core.js
@@ -6,129 +6,142 @@ const fs = require("fs"); | ||
class SiteMapper { | ||
constructor({ | ||
alternateUrls, | ||
baseUrl, | ||
ignoreIndexFiles, | ||
ignoredPaths, | ||
pagesDirectory, | ||
sitemapPath, | ||
targetDirectory, | ||
nextConfigPath | ||
}) { | ||
this.alternatesUrls = alternateUrls || {}; | ||
this.baseUrl = baseUrl; | ||
this.ignoredPaths = ignoredPaths || []; | ||
this.ignoreIndexFiles = ignoreIndexFiles || false; | ||
this.pagesdirectory = pagesDirectory; | ||
this.sitemapPath = sitemapPath; | ||
this.targetDirectory = targetDirectory; | ||
this.nextConfigPath = nextConfigPath; | ||
this.sitemap = `<?xml version="1.0" encoding="UTF-8"?> | ||
constructor({ | ||
alternateUrls, | ||
baseUrl, | ||
ignoreIndexFiles, | ||
ignoredPaths, | ||
pagesDirectory, | ||
sitemapPath, | ||
targetDirectory, | ||
nextConfigPath, | ||
ignoredExtensions | ||
}) { | ||
this.alternatesUrls = alternateUrls || {}; | ||
this.baseUrl = baseUrl; | ||
this.ignoredPaths = ignoredPaths || []; | ||
this.ignoreIndexFiles = ignoreIndexFiles || false; | ||
this.ignoredExtensions = ignoredExtensions || []; | ||
this.pagesdirectory = pagesDirectory; | ||
this.sitemapPath = sitemapPath; | ||
this.targetDirectory = targetDirectory; | ||
this.nextConfigPath = nextConfigPath; | ||
this.sitemap = `<?xml version="1.0" encoding="UTF-8"?> | ||
<urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml"> | ||
`; | ||
if (this.nextConfigPath) { | ||
this.nextConfig = require(nextConfigPath); | ||
if (this.nextConfigPath) { | ||
this.nextConfig = require(nextConfigPath); | ||
} | ||
} | ||
} | ||
preLaunch() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
this.sitemap, | ||
{ | ||
flag: "w" | ||
} | ||
); | ||
} | ||
preLaunch() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
this.sitemap, | ||
{ | ||
flag: "w" | ||
} | ||
); | ||
} | ||
finish() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
"</urlset>", | ||
{ | ||
flag: "as" | ||
} | ||
); | ||
} | ||
finish() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
"</urlset>", | ||
{ | ||
flag: "as" | ||
} | ||
); | ||
} | ||
/** | ||
* | ||
*/ | ||
buildPathMap(dir) { | ||
var pathMap = {}; | ||
/** | ||
* | ||
*/ | ||
buildPathMap(dir) { | ||
var pathMap = {}; | ||
let data = fs.readdirSync(dir); | ||
for (let site of data) { | ||
// Filter directories | ||
if (site[0] === "_" || site[0] === ".") continue; | ||
let toIgnore = false; | ||
for (let path of this.ignoredPaths) { | ||
if (site.includes(path)) toIgnore = true; | ||
} | ||
if (toIgnore) continue; | ||
let data = fs.readdirSync(dir); | ||
for (let site of data) { | ||
// Filter directories | ||
if (site[0] === "_" || site[0] === ".") continue; | ||
let toIgnore = false; | ||
for (let path of this.ignoredPaths) { | ||
if (site.includes(path)) toIgnore = true; | ||
} | ||
if (toIgnore) continue; | ||
// Handle recursive paths | ||
if (fs.lstatSync(dir + path.sep + site).isDirectory()) { | ||
pathMap = { | ||
...pathMap, | ||
...this.buildPathMap(dir + path.sep + site) | ||
}; | ||
// Handle recursive paths | ||
if (fs.lstatSync(dir + path.sep + site).isDirectory()) { | ||
pathMap = { | ||
...pathMap, | ||
...this.buildPathMap(dir + path.sep + site) | ||
}; | ||
continue; | ||
} | ||
continue; | ||
} | ||
// Is file | ||
let fileExtension = site.split(".").pop().length; | ||
let fileNameWithoutExtension = site.substring( | ||
0, | ||
site.length - (fileExtension + 1) | ||
); | ||
fileNameWithoutExtension = | ||
this.ignoreIndexFiles && fileNameWithoutExtension === "index" | ||
? "" | ||
: fileNameWithoutExtension; | ||
let newDir = dir.replace(this.pagesdirectory, "").replace(/\\/g, "/"); | ||
// Is file | ||
let fileExtension = site.split(".").pop(); | ||
let pagePath = newDir + "/" + fileNameWithoutExtension; | ||
pathMap[pagePath] = { | ||
page: pagePath | ||
}; | ||
} | ||
//Ignoring file extension by user config | ||
let toIgnoreExtension = false; | ||
return pathMap; | ||
} | ||
for (let extensionToIgnore of this.ignoredExtensions) { | ||
if (extensionToIgnore === fileExtension) toIgnoreExtension = true; | ||
} | ||
async sitemapMapper(dir) { | ||
var pathMap = this.buildPathMap(dir); | ||
const exportPathMap = this.nextConfig && this.nextConfig.exportPathMap; | ||
if (toIgnoreExtension) continue; | ||
// | ||
if (exportPathMap) { | ||
pathMap = await exportPathMap(pathMap, {}); | ||
let fileNameWithoutExtension = site.substring( | ||
0, | ||
site.length - (fileExtension.length + 1) | ||
); | ||
fileNameWithoutExtension = | ||
this.ignoreIndexFiles && fileNameWithoutExtension === "index" | ||
? "" | ||
: fileNameWithoutExtension; | ||
let newDir = dir.replace(this.pagesdirectory, "").replace(/\\/g, "/"); | ||
let pagePath = newDir + "/" + fileNameWithoutExtension; | ||
pathMap[pagePath] = { | ||
page: pagePath | ||
}; | ||
} | ||
return pathMap; | ||
} | ||
const paths = Object.keys(pathMap); | ||
const date = dateFns.format(new Date(), "YYYY-MM-DD"); | ||
async sitemapMapper(dir) { | ||
var pathMap = this.buildPathMap(dir); | ||
const exportPathMap = this.nextConfig && this.nextConfig.exportPathMap; | ||
for (var i = 0, len = paths.length; i < len; i++) { | ||
let pagePath = paths[i]; | ||
let alternates = ""; | ||
if (exportPathMap) { | ||
pathMap = await exportPathMap(pathMap, {}); | ||
} | ||
for (let langSite in this.alternatesUrls) { | ||
alternates += `<xhtml:link rel="alernate" hreflang="${langSite}" href="${this.alternatesUrls[langSite]}${pagePath}" />`; | ||
} | ||
const paths = Object.keys(pathMap); | ||
const date = dateFns.format(new Date(), "YYYY-MM-DD"); | ||
let xmlObject = | ||
`<url><loc>${this.baseUrl}${pagePath}</loc>` + | ||
alternates + | ||
`<lastmod>${date}</lastmod></url>`; | ||
for (var i = 0, len = paths.length; i < len; i++) { | ||
let pagePath = paths[i]; | ||
let alternates = ""; | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
xmlObject, | ||
{ flag: "as" } | ||
); | ||
for (let langSite in this.alternatesUrls) { | ||
alternates += `<xhtml:link rel="alernate" hreflang="${langSite}" href="${this.alternatesUrls[langSite]}${pagePath}" />`; | ||
} | ||
let xmlObject = | ||
`<url><loc>${this.baseUrl}${pagePath}</loc>` + | ||
alternates + | ||
`<lastmod>${date}</lastmod></url>`; | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
xmlObject, | ||
{flag: "as"} | ||
); | ||
} | ||
} | ||
} | ||
} | ||
module.exports = SiteMapper; |
{ | ||
"name": "nextjs-sitemap-generator", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Generate sitemap.xml from nextjs pages", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
@@ -13,5 +12,6 @@ "keywords": [ | ||
"pages", | ||
"node" | ||
"node", | ||
"sitemap" | ||
], | ||
"author": "Adrián Alonso", | ||
"author": "Adrián Alonso Vergara", | ||
"license": "MIT", | ||
@@ -18,0 +18,0 @@ "dependencies": { |
![npmv1](https://img.shields.io/npm/v/nextjs-sitemap-generator.svg) | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors) | ||
You can make donations for the maintenance of the project. | ||
[Donate](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YFXG8SLXPEVXN&source=url) | ||
Simple sitemap.xml mapper for NextJs proyects. | ||
@@ -32,2 +35,6 @@ ## Usage | ||
nextConfigPath: __dirname + "\\next.config.js" | ||
ignoredExtensions: [ | ||
'png', | ||
'jpg' | ||
] | ||
}); | ||
@@ -41,2 +48,3 @@ | ||
- **ignoredPaths**: File or directory to not map (like admin routes).(OPTIONAL) | ||
- **ignoredExtensions**: Ignore files by extension.(OPTIONAL) | ||
- **pagesDirectory**: The directory where Nextjs pages live. You can use another directory while they are nextjs pages. **It must to be an absolute path**. | ||
@@ -43,0 +51,0 @@ - **targetDirectory**: The directory where sitemap.xml going to be written. |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
12144
6
134
1
80