Skip to content

Commit 5800442

Browse files
committed
Merge branch 'main' of github.com:ReeceM/prismic-sitemap into main
2 parents e5a78ce + ad6678c commit 5800442

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

tests/buffer-example.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/generator.test.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const sitemapConfig = {
2828
post: { changefreq: "weekly", priority: 0.8 },
2929
latest_notice: (doc) => {
3030
return {
31-
lastmod: doc.last_publication_date ? doc.last_publication_date : (new Date()).toJSON(),
31+
lastmod: doc.last_publication_date ? doc.last_publication_date : "2020-07-16",
3232
changefreq: "weekly",
3333
priority: 0.8
3434
}
@@ -55,7 +55,7 @@ describe('tests for generator.js', () => {
5555
.toHaveBeenCalledTimes(1);
5656
});
5757

58-
it('file write should have been called with ...', () => {
58+
it('file write should have been called with sitemapData Buffer', () => {
5959

6060
expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith(
6161
path.join(__dirname, "../public/sitemap.xml"),
@@ -73,6 +73,27 @@ describe('tests for generator.js', () => {
7373
// })
7474
});
7575

76+
77+
describe('Pagination Tests', () => {
78+
it('file write should have been called with sitemapData Buffer', async () => {
79+
80+
const paginationConfig = {
81+
pagination: {
82+
pageSize: 5,
83+
}, ...sitemapConfig
84+
};
85+
86+
jest.restoreAllMocks();
87+
await generator(paginationConfig);
88+
89+
expect(jest.spyOn(fs, 'writeFileSync')).toHaveBeenCalledWith(
90+
path.join(__dirname, "../public/sitemap.xml"),
91+
require('./buffer-example'),
92+
"utf-8"
93+
);
94+
});
95+
});
96+
7697
describe('generator errors', () => {
7798
it('will fail if linkResolver is not a function', async () => {
7899
expect(async () => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/notice/sldkfmlksdmfs</loc><lastmod>2020-07-16</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/page/more-pages-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-a-lot-i-dont-got</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page21</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/customer-reviews-page-example</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot11</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-world-to-the-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/how-many-more-must-there-be</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/blog/a-demo-blog-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/blog/demo-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/hello-to-me</loc><lastmod>2021-06-29</lastmod></url><url><loc>https://example.com/sup</loc><lastmod>2000-02-02</lastmod></url></urlset>"`;
3+
exports[`Using Sitemap Generator visiting page loads sitemap.xml 1`] = `"<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?><urlset xmlns=\\"http://www.sitemaps.org/schemas/sitemap/0.9\\" xmlns:news=\\"http://www.google.com/schemas/sitemap-news/0.9\\" xmlns:xhtml=\\"http://www.w3.org/1999/xhtml\\" xmlns:image=\\"http://www.google.com/schemas/sitemap-image/1.1\\" xmlns:video=\\"http://www.google.com/schemas/sitemap-video/1.1\\"><url><loc>https://example.com/notice/sldkfmlksdmfs</loc><lastmod>2020-07-16</lastmod><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/page/more-pages-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-a-lot-i-dont-got</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page21</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-page-of-cabbages1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/a-demo-page2</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/customer-reviews-page-example</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/20-is-a-lot11</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/what-is-the-time1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-world-to-the-cabbages</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/how-many-more-must-there-be</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/hello-worldi-need-to-make-a-demo-app</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/page/another-page-here1</loc><changefreq>monthly</changefreq><priority>1.0</priority></url><url><loc>https://example.com/blog/a-demo-blog-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/blog/demo-post</loc><changefreq>weekly</changefreq><priority>0.8</priority></url><url><loc>https://example.com/hello-to-me</loc><lastmod>2021-06-29</lastmod></url><url><loc>https://example.com/sup</loc><lastmod>2000-02-02</lastmod></url></urlset>"`;

utils/paginator.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const paginator = {
77
/**
88
*
99
* @param {Prismic} api prismic client
10-
* @param {Object} options {pageSize, auto}
10+
* @param {Object} options {pageSize}
1111
*/
1212
init(api, options = {}) {
1313
this.api = api;
@@ -16,19 +16,27 @@ const paginator = {
1616
return this;
1717
},
1818

19+
/**
20+
* This paginates automatically over the document list from Prismic
21+
* @todo Add Lang support for scoping to a selection of languages.
22+
*
23+
* @param {String} type The Document type
24+
* @param {Number} next The Next Page type
25+
* @returns Promise
26+
*/
1927
async paginate(type, next = null) {
2028
const { results, total_pages, page } = await this.api.query(
2129
Prismic.Predicates.at("document.type", type),
22-
{
23-
lang: "*",
24-
pageSize: this.pageSize,
25-
page: next
26-
}
30+
{
31+
lang: "*",
32+
pageSize: this.pageSize,
33+
page: next
34+
}
2735
);
2836

2937
this.results.push(...results);
3038

31-
if (total_pages > this.nextPage) {
39+
if (total_pages !== page) {
3240
return this.paginate(type, page + 1)
3341
}
3442

0 commit comments

Comments
 (0)