diff --git a/CHANGELOG.md b/CHANGELOG.md index a3c2296..cbc4bd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,11 +6,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.0.2] - 2021-03-01 + +### Added +- Allow the filename to be changed + +### Fixed +- The public path was not resolved properly + - Defaults to using the next dir when from the `next.config.js` file + - Will us the a local public folder when running directly when not defined + ## [0.0.1] - 2021-02-28 ### Added - Initial File - Docs and such -[Unreleased]: https://github.com/reecem/prismic-sitemap/compare/v0.0.1...HEAD +[Unreleased]: https://github.com/reecem/prismic-sitemap/compare/v0.0.2...HEAD +[0.0.2]: https://github.com/reecem/prismic-sitemap/tag/v0.0.2 [0.0.1]: https://github.com/reecem/prismic-sitemap/tag/v0.0.1 diff --git a/README.md b/README.md index 06969a7..cfa6494 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,7 @@ The sitemap object is made up of the following: linkResolver = Function, apiEndpoint = String, hostname = String, + fileName = String, optionsMapPerDocumentType = Object, /** @see https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options */ documentTypes = Array, sitemapConfig = Object /** @see https://github.com/ekalinin/sitemap.js#options-you-can-pass */ @@ -148,6 +149,7 @@ The sitemap object is made up of the following: |linkResolver|function|`doc => {return `${doc.uid}`;}`| This is the Prismic.io link resolver, this could be custom, or used from the prismic-configuration files.| |apiEndpoint|string|`'https://some-repository-on-prismic.cdn.prismic.io/api/v2'`| This is the URL of your Prismic repository, the API version of it.| |hostname|string|`'http://example.com/'`| The hostname of your Vercel/Next.js application| +|fileName|string|`'sitemap.xml'`| The name of the sitemap, it is always placed inside public| |optionsMapPerDocumentType|object|`{ page: { changefreq: "monthly", priority: 1 }, }`| The options for the documents that are indexed, this can also have other options, found at [https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options](https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options)| |documentTypes|array|`['homepage', 'page', 'pricing', 'legal']`|| |sitemapConfig|object|| see [https://github.com/ekalinin/sitemap.js#options-you-can-pass](https://github.com/ekalinin/sitemap.js#options-you-can-pass)| diff --git a/generator.js b/generator.js index 213ca7a..f92f7d5 100644 --- a/generator.js +++ b/generator.js @@ -21,6 +21,7 @@ const generator = async (sitemap) => { hostname = '', optionsMapPerDocumentType = {}, documentTypes = [], + fileName = 'sitemap.xml', publicPath = 'public', sitemapConfig } = sitemap; @@ -57,13 +58,23 @@ const generator = async (sitemap) => { const sitemapData = await streamToPromise(sitemapStream); - if (!fs.existsSync(path.join(__dirname, publicPath))) { - fs.mkdirSync(path.join(__dirname, publicPath), { recursive: true }); + let basePath = resolvePublicPath(publicPath) + + if (!fs.existsSync(path.join(basePath))) { + fs.mkdirSync(path.join(basePath), { recursive: true }); } - fs.writeFileSync(path.join(__dirname, `${publicPath}/sitemap.xml`), sitemapData, "utf-8"); + fs.writeFileSync(path.join(basePath, fileName), sitemapData, "utf-8"); return sitemapData; } +function resolvePublicPath(dir) { + if (dir === 'public') { + dir = path.join(__dirname, dir); + } + + return dir; +} + module.exports = generator; diff --git a/index.js b/index.js index dac6507..584a114 100644 --- a/index.js +++ b/index.js @@ -13,6 +13,10 @@ module.exports = (nextConfig) => ({ if (isServer) { console.log('[Sitemap Generator]: Generating Sitemap'); + if (typeof sitemap.publicPath === 'undefined') { + sitemap.publicPath = path.join(options.dir, 'public'); + } + generator(sitemap); return config; diff --git a/package.json b/package.json index c3570cd..1532f1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@reecem/prismic-sitemap", - "version": "0.0.1", + "version": "0.0.2", "description": "Sitemap Generator for Prismic and Next.js", "keywords": [ "javascript", @@ -22,7 +22,7 @@ }, "scripts": { "sitemap": "node ./index.js", - "test": "jest" + "test": "TEST_REPOSITORY=https://hbar.cdn.prismic.io/api/v2 jest" }, "author": "@ReeceM", "license": "MIT"