Skip to content

Commit

Permalink
fix: work from the local options.dir from next
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeceM committed Feb 28, 2021
1 parent 90f955b commit ec4bffd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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)|
Expand Down
17 changes: 14 additions & 3 deletions generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const generator = async (sitemap) => {
hostname = '',
optionsMapPerDocumentType = {},
documentTypes = [],
fileName = 'sitemap.xml',
publicPath = 'public',
sitemapConfig
} = sitemap;
Expand Down Expand Up @@ -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;
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
Expand Down

0 comments on commit ec4bffd

Please sign in to comment.