feat: Limit RSS Feed to return a maximum of 20 items
This commit is contained in:
@@ -1,9 +1,23 @@
|
|||||||
import { Feed } from "feed";
|
import { Feed } from "feed";
|
||||||
import { releaseNoteIsAlpha, releaseNotes } from "@/lib/release-notes";
|
import { releaseNotes } from "@/lib/release-notes";
|
||||||
import type { ReleaseNote } from "@/lib/release-notes";
|
import type { ReleaseNote } from "@/lib/release-notes";
|
||||||
|
|
||||||
|
// Force feed.xml to be cached as static and remain constant for the lifetime of the current site build.
|
||||||
|
// The supplied releaseNotes array is constant per build, so this will always be the latest release notes.
|
||||||
|
export const dynamic = "force-static";
|
||||||
|
|
||||||
|
/** The default number of entries to include in the RSS feed. */
|
||||||
|
const RSS_ENTRY_LIMIT = 20;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the GET request for the `feed.xml` endpoint.
|
||||||
|
* @returns The RSS feed for the Zen Browser release notes.
|
||||||
|
*/
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
releaseNotes[0].date
|
// Just in case the release notes array is empty for whatever reason.
|
||||||
|
const latestDate = releaseNotes.length > 0
|
||||||
|
? formatRssDate(releaseNotes[0].date)
|
||||||
|
: new Date();
|
||||||
|
|
||||||
const feed = new Feed({
|
const feed = new Feed({
|
||||||
id: "https://www.zen-browser.app/release-notes",
|
id: "https://www.zen-browser.app/release-notes",
|
||||||
@@ -13,10 +27,10 @@ export async function GET() {
|
|||||||
language: "en",
|
language: "en",
|
||||||
favicon: "https://www.zen-browser.app/favicon.ico",
|
favicon: "https://www.zen-browser.app/favicon.ico",
|
||||||
copyright: `Zen Browser © ${new Date().getFullYear()} - Made with ❤️ by the Zen team.`,
|
copyright: `Zen Browser © ${new Date().getFullYear()} - Made with ❤️ by the Zen team.`,
|
||||||
updated: formatRssDate(releaseNotes[0].date),
|
updated: latestDate,
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const releaseNote of releaseNotes) {
|
for (const releaseNote of releaseNotes.slice(0, RSS_ENTRY_LIMIT)) {
|
||||||
feed.addItem({
|
feed.addItem({
|
||||||
title: `Release notes for version ${releaseNote.version}`,
|
title: `Release notes for version ${releaseNote.version}`,
|
||||||
id: `https://www.zen-browser.app/release-notes/${releaseNote.version}`,
|
id: `https://www.zen-browser.app/release-notes/${releaseNote.version}`,
|
||||||
@@ -34,23 +48,38 @@ export async function GET() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatRssDate(date: string) {
|
/**
|
||||||
// NOTE: This is assuming the format day/month/year. If release notes change to ISO format, this will need to be updated.
|
* Formats a date string in the format day/month/year.
|
||||||
const splitDate = date.split("/");
|
*
|
||||||
const year = Number(splitDate[2]);
|
* Note: If release notes change to ISO format, this will need to be updated.
|
||||||
const month = Number(splitDate[1]) - 1;
|
* @param dateStr The date string to format.
|
||||||
|
* @returns The passed in date string as a Date object.
|
||||||
|
*/
|
||||||
|
function formatRssDate(dateStr: string) {
|
||||||
|
const splitDate = dateStr.split("/");
|
||||||
|
if (splitDate.length !== 3) {
|
||||||
|
throw new Error("Invalid date format");
|
||||||
|
}
|
||||||
|
|
||||||
const day = Number(splitDate[0]);
|
const day = Number(splitDate[0]);
|
||||||
|
const month = Number(splitDate[1]) - 1;
|
||||||
|
const year = Number(splitDate[2]);
|
||||||
return new Date(year, month, day);
|
return new Date(year, month, day);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats the release note entry for use as the content of the RSS feed.
|
||||||
|
* @param releaseNote The release note to format.
|
||||||
|
* @returns The formatted release note as a HTML string.
|
||||||
|
*/
|
||||||
function formatReleaseNote(releaseNote: ReleaseNote) {
|
function formatReleaseNote(releaseNote: ReleaseNote) {
|
||||||
let content = "<p>If you encounter any issues, please report them on <a href=\"https://github.com/zen-browser/desktop/issues/\">the issues page</a>. Thanks everyone for your feedback! ❤️</p>";
|
let content = "<p>If you encounter any issues, please report them on <a href=\"https://github.com/zen-browser/desktop/issues/\">the issues page</a>. Thanks everyone for your feedback! ❤️</p>";
|
||||||
|
|
||||||
if(releaseNote.extra) {
|
if (releaseNote.extra) {
|
||||||
content += `<p>${releaseNote.extra.replace(/(\n)/g, "<br />")}</p>`
|
content += `<p>${releaseNote.extra.replace(/(\n)/g, "<br />")}</p>`
|
||||||
}
|
}
|
||||||
|
|
||||||
if(releaseNote.breakingChanges) {
|
if (releaseNote.breakingChanges) {
|
||||||
content += `<h2>⚠️ Breaking changes</h2>`
|
content += `<h2>⚠️ Breaking changes</h2>`
|
||||||
content += `<ul>`
|
content += `<ul>`
|
||||||
for (const breakingChange of releaseNote.breakingChanges) {
|
for (const breakingChange of releaseNote.breakingChanges) {
|
||||||
@@ -59,7 +88,7 @@ function formatReleaseNote(releaseNote: ReleaseNote) {
|
|||||||
content += `</ul>`
|
content += `</ul>`
|
||||||
}
|
}
|
||||||
|
|
||||||
if(releaseNote.features) {
|
if (releaseNote.features) {
|
||||||
content += `<h2>⭐ Features</h2>`
|
content += `<h2>⭐ Features</h2>`
|
||||||
content += `<ul>`
|
content += `<ul>`
|
||||||
for (const feature of releaseNote.features) {
|
for (const feature of releaseNote.features) {
|
||||||
@@ -68,7 +97,7 @@ function formatReleaseNote(releaseNote: ReleaseNote) {
|
|||||||
content += `</ul>`
|
content += `</ul>`
|
||||||
}
|
}
|
||||||
|
|
||||||
if(releaseNote.fixes) {
|
if (releaseNote.fixes) {
|
||||||
content += `<h2>✓ Fixes</h2>`
|
content += `<h2>✓ Fixes</h2>`
|
||||||
content += `<ul>`
|
content += `<ul>`
|
||||||
for (const fix of releaseNote.fixes) {
|
for (const fix of releaseNote.fixes) {
|
||||||
@@ -78,4 +107,4 @@ function formatReleaseNote(releaseNote: ReleaseNote) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user