Skip to content

Commit

Permalink
Merge pull request #10 from ReeceM/feature-minor/options-callback-for…
Browse files Browse the repository at this point in the history
…-sitemap-entry

adds options callback for sitemap entries [target: new v0.3.0]
  • Loading branch information
ReeceM authored Jun 30, 2021
2 parents 831637b + 6729580 commit d1ea2bf
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 31 deletions.
118 changes: 117 additions & 1 deletion .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,119 @@
# Security Policy

If you discover any security related issues, please email reecestevenmay@gmail.com instead of using the issue tracker.
**PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY, [SEE BELOW](#reporting-a-vulnerability).**

## Supported Versions

Use this section to tell people about which versions of your project are
currently being supported with security updates.

| Version | Supported |
| ------- | ------------------ |
| 0.2.x ||
| alpha | ❌ these are for testing |
| < 0.1 ||

## Reporting a Vulnerability

If you discover a security vulnerability, please send an email to Reece at zsh.rce@gmail.com.
All security vulnerabilities will be promptly addressed.

As the vulnerability is addressed and checked out I will attempt to keep you updated as things are sorted.

Please provide a proof of concept for what you did to find the vulnerability as well as any possible real world exploits that could lead to actual leakage of data or exploit

## Communication

Once you have made initial contact via email, now public issues, if they are opened it will be if it is deemed alright.

Please allow up to 24 Hours for a response.

## Credits

If you have found a vulnerability and it is found to be valid and is fixed, you will be credited with the discovery of the issue.

Obviously, this being open-source, there wouldn't be any payment for discovering vulnerabilities.

### Public PGP Key

<details>
<summary>🔑 Show Key</summary>
```
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: OpenPGP v2.0.8
Comment: https://sela.io/pgp/
xsFNBF5sBTMBEACf9OoEsOBGVGJ1q3wKnJXYfsLZMyYzDXkrWZ70Jvxfvls1sbu5
BJ7efrBzekgZ1aIoOjjZ2qXMaGHv0FCJLRqL3Wiv7kiMlc59q0Zr7ZMMU5FGASNl
f8iw1craew5bRfEqD6zcXR0MQbjoCLfpIVa8YJhe5k/NZhexybPNlevkYD0CTRqv
5alnFu2gEkx+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-----END PGP PUBLIC KEY BLOCK-----
```
</details>
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x]
node-version: [12.x, 14.x, 15.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.vscode

# dependencies
/node_modules
/.pnp
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

This is for version 0.3.0

### Added
- Option to make use of callback in `optionsMapPerDocumentType` see Discussion [Lastmod support #8](https://github.com/ReeceM/prismic-sitemap/discussions/8)
- Added option to have a static path list of urls for normal routes that aren't dynamic

### Changed
- Made the URL result from the linkResolver take priority on results that go into the sitemap
- The `sitemapConfig` option is using the `lastmodDateOnly = true` setting to ensure all lastmod dates are `YYYY-MM-DD` only. But can be disabled to keep UTC time.

## [0.2.0] - 2021-06-14

### Changed
Expand Down
81 changes: 72 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
[![npm](https://img.shields.io/npm/v/@reecem/prismic-sitemap)](https://www.npmjs.com/package/@reecem/prismic-sitemap)
[![npm](https://img.shields.io/npm/dt/@reecem/prismic-sitemap)](https://www.npmjs.com/package/@reecem/prismic-sitemap)

A sitemap generator for Next.js websites based on the pages in your [Prismic.io](https://prismic.io) backed Next.js application.
An easy to configure sitemap generator for Next.js websites based on the pages in your [Prismic.io](https://prismic.io) CMS.

## About

This package uses the [Sitemap.js](https://github.com/ekalinin/sitemap.js) package to generate the sitemaps as it handles all the needed stuff for sitemaps. This also gives the end user flexibility in generating the sitemap
This package uses the [Sitemap.js](https://github.com/ekalinin/sitemap.js) package to generate the sitemaps as it handles all the needed stuff for sitemaps. This also gives the end user flexibility in generating the sitemap.

This package is also inspired by the live steam from Prismic.io where they built a sitemap generator.
It aims to simplify the configuration required to create the sitemap, and also include it in the build process of your Next.js site by extending the `next.config.js` file.

> This package is also inspired by the live steam from Prismic.io where they built a sitemap generator. But wanting to make it better like.
## Installation

Expand Down Expand Up @@ -62,8 +64,16 @@ module.exports = withPrismicSitemap({
apiEndpoint: API_ENDPOINT,
hostname: SITE_URL,
optionsMapPerDocumentType: {
post: { changefreq: "weekly", priority: 0.8 },
page: { changefreq: "monthly", priority: 1 }
// setting the update date of the article.
post: (document) => {
return {
// get the last time the document was published in Prismic
lastmod: document.last_publication_date,
changefreq: "weekly",
priority: 0.8
}
}
page: { changefreq: "monthly", priority: 1 }
},
documentTypes: ['page', 'post']
}
Expand Down Expand Up @@ -169,6 +179,22 @@ module.exports = withPrismicSitemap({
})
```

### `<lastmod>` support

The package makes use of lastmod support for the sitemap, this would need to be set by the user if you would want the date to come through at the current version of `0.3.0`.

Because the Sitemap file is written using a separate package, it supports parsing the date straight to the `YYYY-MM-DD` format, if you would like it to have the time as well, you will need to override the setting in the config:

```javascript
sitemap: {

sitemapConfig: {
lastmodDateOnly: false,
},

}
```

## API

The sitemap object is made up of the following:
Expand All @@ -182,25 +208,62 @@ The sitemap object is made up of the following:
accessToken = String|null,
hostname = String,
fileName = String,
optionsMapPerDocumentType = Object, /** @see https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options */
optionsMapPerDocumentType = Object|Object<Function>,
defaultEntryOption = Object,
staticPaths = Array<Object>,
documentTypes = Array,
sitemapConfig = Object /** @see https://github.com/ekalinin/sitemap.js#options-you-can-pass */
sitemapConfig = Object
}
}
```

|Option|Type|eg|Description|
|------|----|--|-----------|
|linkResolver|function|`doc => {return `${doc.uid}`;}`| This is the Prismic.io link resolver, this could be custom, or used from the prismic-configuration files.|
|linkResolver|function|```doc => {return `/path/${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.|
|accessToken|string(optional)|`'random_api_string_that_you_get'`| This is the Access token used to access private Prismic Repositories|
|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)|
|optionsMapPerDocumentType|object|`{ page: { changefreq: "monthly", priority: 1 }, post: (doc) => {lastmod: doc.last_publication_date}}`| 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']`||
|defaultEntryOption|object (optional)| `{ changefreq: "monthly", priority: 1, }`| This is the default to add when nothing exists for the type or callback for entries|
|staticPaths|array|`[{ url: '/static/path', changefreq: "yearly", priority: 1, lastmod: '2000-01-01'}]`| Use this if you would like to define a custom path for the Sitemap that doesn't come from the CMS |
|sitemapConfig|object|| see [https://github.com/ekalinin/sitemap.js#options-you-can-pass](https://github.com/ekalinin/sitemap.js#options-you-can-pass)|


### `optionsMapPerDocumentType`

The `optionsMapPerDocumentType` setting for the sitemap value allows you to configure the object result for the sitemap entry for the specific document

This accepts a object with keys to the document type from Prismic, the value can be a object, or a callback.

**Using the Callback**

This option allows you to determine extra data about the document and return a object to be written to the Sitemap, there isn't a need to return the URL value, and this will be purged from the result anyway in favour of the `linkResolver` result. This is for consistency reasons.

The primary reason to add this is because of using the `<lastmod>` XML attribute in the Sitemap to improve indexing by Google or other search engines.

To make use of this, you can do the following logic:

```javascript

module.exports = withPrismicSitemap({
sitemap: {
// ... other cofing
optionsMapPerDocumentType: {
post: (document) => {
return {
lastmod: document.last_publication_date ? document.last_publication_date : (new Date()).toJSON(),
changefreq: 'monthly'
};
}
},
}
})
```

---

## Testing

```bash
Expand Down
Loading

0 comments on commit d1ea2bf

Please sign in to comment.