Skip to content

Commit

Permalink
Merge pull request #128 from jarylc/add_xml_sitemap
Browse files Browse the repository at this point in the history
#58: Add support for sitemap.xml endpoint
  • Loading branch information
wenbinf committed Apr 14, 2024
2 parents 31023e0 + 7ff5c2b commit 438b68a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
37 changes: 37 additions & 0 deletions edge-src/common/PageUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,43 @@ export class JsonResponseBuilder extends ResponseBuilder {
}
}

export class SitemapResponseBuilder extends ResponseBuilder {
get _contentType() {
return 'text/xml';
}

_getResponse(props) {
const res = super._getResponse(props);
let xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">'
xml += `<url><loc>${this.jsonData.home_page_url}</loc><image:image><image:loc>${this.jsonData.icon}</image:loc></image:image></url>`;
this.jsonData.items.map((item) => {
xml += `<url><loc>${item._microfeed.web_url}</loc><lastmod>${item.date_published}</lastmod>`
if (item.attachments) {
item.attachments.forEach((attachment) => {
if (attachment.mime_type.startsWith('image/')) {
xml += `<image:image><image:loc>${attachment.url}</image:loc></image:image>`
} else if (attachment.mime_type.startsWith('video/')) {
xml += `<video:video><video:title>${item.title}</video:title><video:publication_date>${item.date_published}</video:publication_date><video:content_loc>${attachment.url}</video:content_loc></video:video>`
}
})
}
xml += `</url>`
})
xml += '</urlset>';
return new Response(xml, res);
}

get _fetchItems() {
const queryKwargs = this.fetchItemsObj.queryKwargs || {};
return getFetchItemsParams(
this.request,
{
// status: STATUSES.PUBLISHED,
...queryKwargs,
}, -1);
}
}

class CodeInjector {
constructor(settings, theme, sharedTheme) {
this.settings = settings;
Expand Down
5 changes: 4 additions & 1 deletion edge-src/models/FeedDb.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,10 @@ export default class FeedDb {
orderBy,
queryKwargs,
};
if (fetchItemsParams.limit > MAX_ITEMS_PER_PAGE) {

if (fetchItemsParams.limit < 0) {
fetchItemsParams.limit = undefined;
} else if (fetchItemsParams.limit > MAX_ITEMS_PER_PAGE) {
fetchItemsParams.limit = MAX_ITEMS_PER_PAGE;
}
things = [{
Expand Down
15 changes: 15 additions & 0 deletions functions/sitemap.xml.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {SitemapResponseBuilder} from '../edge-src/common/PageUtils';
import {STATUSES} from "../common-src/Constants";

export async function onRequestGet({env, request}) {
const sitemapResponseBuilder = new SitemapResponseBuilder(env, request, {
queryKwargs: {
status: STATUSES.PUBLISHED,
},
});
return sitemapResponseBuilder.getResponse({
getComponent: (_, jsonData) => {
return jsonData
},
});
}

0 comments on commit 438b68a

Please sign in to comment.