Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rss): fix an issue where trailing slash is not removed even if trailingSlash is set to false #11050

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-otters-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/rss": patch
---

Fixes an issue where trailing slash is not removed even if the `trailingSlash` option is set to `false`.
8 changes: 4 additions & 4 deletions packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
root.rss.channel = {
title: rssOptions.title,
description: rssOptions.description,
link: createCanonicalURL(site, rssOptions.trailingSlash, undefined).href,
link: createCanonicalURL(site, rssOptions.trailingSlash, undefined),
};
if (typeof rssOptions.customData === 'string')
Object.assign(
Expand All @@ -220,7 +220,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
// If the item's link is already a valid URL, don't mess with it.
const itemLink = isValidURL(result.link)
? result.link
: createCanonicalURL(result.link, rssOptions.trailingSlash, site).href;
: createCanonicalURL(result.link, rssOptions.trailingSlash, site);
item.link = itemLink;
item.guid = { '#text': itemLink, '@_isPermaLink': 'true' };
}
Expand All @@ -246,7 +246,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
if (typeof result.commentsUrl === 'string') {
item.comments = isValidURL(result.commentsUrl)
? result.commentsUrl
: createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site).href;
: createCanonicalURL(result.commentsUrl, rssOptions.trailingSlash, site);
}
if (result.source) {
item.source = parser.parse(
Expand All @@ -256,7 +256,7 @@ async function generateRSS(rssOptions: ValidatedRSSOptions): Promise<string> {
if (result.enclosure) {
const enclosureURL = isValidURL(result.enclosure.url)
? result.enclosure.url
: createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site).href;
: createCanonicalURL(result.enclosure.url, rssOptions.trailingSlash, site);
item.enclosure = parser.parse(
`<enclosure url="${enclosureURL}" length="${result.enclosure.length}" type="${result.enclosure.type}"/>`
).enclosure;
Expand Down
15 changes: 9 additions & 6 deletions packages/astro-rss/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ export function createCanonicalURL(
url: string,
trailingSlash?: RSSOptions['trailingSlash'],
base?: string
): URL {
): string {
let pathname = url.replace(/\/index.html$/, ''); // index.html is not canonical
if (trailingSlash === false) {
// remove the trailing slash
pathname = pathname.replace(/\/*$/, '');
} else if (!getUrlExtension(url)) {
if (!getUrlExtension(url)) {
// add trailing slash if there’s no extension or `trailingSlash` is true
pathname = pathname.replace(/\/*$/, '/');
}

pathname = pathname.replace(/\/+/g, '/'); // remove duplicate slashes (URL() won’t)
return new URL(pathname, base);

const canonicalUrl = new URL(pathname, base).href;
if (trailingSlash === false) {
// remove the trailing slash
return canonicalUrl.replace(/\/*$/, '');
}
return canonicalUrl;
}

/** Check if a URL is already valid */
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('getRssString', () => {
trailingSlash: false,
});

assert.ok(str.includes('https://example.com/<'));
assert.ok(str.includes('https://example.com<'));
assert.ok(str.includes('https://example.com/php<'));
});

Expand Down
Loading