Skip to content

Commit 77b3d87

Browse files
committed
fix: rss link
1 parent 11c4d1a commit 77b3d87

File tree

3 files changed

+62
-41
lines changed

3 files changed

+62
-41
lines changed

layouts/default.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
<Nuxt />
77
</div>
88
<footer>
9-
<a href="http://localhost:3000/blog/feed/articles/rss.xml" style="display:flex;"><img src="rss-icon.png" alt="rss" width="20" height="20" /> <p>RSS</p></a>
9+
<a href="/blog/feed.xml" style="display: flex"
10+
><img src="rss-icon.png" alt="rss" width="20" height="20" />
11+
<p>RSS</p></a
12+
>
1013
</footer>
1114
</div>
1215
</template>

layouts/post.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
<Nuxt />
55
</div>
66
<footer>
7-
<a href="http://localhost:3000/blog/feed/articles/rss.xml" style="display:flex;"><img src="rss-icon.png" alt="rss" width="20" height="20" /> <p>RSS</p></a>
7+
<a href="/blog/feed.xml" style="display: flex"
8+
><img src="rss-icon.png" alt="rss" width="20" height="20" />
9+
<p>RSS</p></a
10+
>
811
</footer>
912
</div>
1013
</template>
@@ -40,7 +43,6 @@
4043
height: 20px;
4144
}
4245
43-
4446
.post-wrapper footer p {
4547
margin-top: 0;
4648
font-size: 18px;

nuxt.config.js

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,43 @@
11
import path from 'path';
2+
import fs from 'fs';
3+
4+
let posts = [];
5+
6+
const constructFeedItem = async (post, dir, hostname) => {
7+
// note the path used here, we are using a dummy page with an empty layout in order to not send that data along with our other content
8+
const filePath = path.join(__dirname, `dist/${post.slug}/index.html`);
9+
const content = await fs.promises.readFile(filePath, 'utf8');
10+
const url = `${hostname}/${dir}/${post.slug}`;
11+
return {
12+
title: post.title,
13+
id: url,
14+
link: url,
15+
description: post.description,
16+
content,
17+
};
18+
};
19+
20+
const create = async (feed, args) => {
21+
const [filePath, ext] = args;
22+
const hostname =
23+
process.NODE_ENV === 'production'
24+
? 'https://oskarlindgren.se/blog'
25+
: 'http://localhost:3000/blog';
26+
feed.options = {
27+
title: 'My Blog',
28+
description: /Blog Stuff!/,
29+
link: `${hostname}/feed.${ext}`,
30+
};
31+
const { $content } = require('@nuxt/content');
32+
if (posts === null || posts.length === 0) {
33+
posts = await $content('articles').fetch();
34+
}
35+
for (const post of posts) {
36+
const feedItem = await constructFeedItem(post, filePath, hostname);
37+
feed.addItem(feedItem);
38+
}
39+
return feed;
40+
};
241

342
export default {
443
// Target: https://go.nuxtjs.dev/config-target
@@ -25,6 +64,12 @@ export default {
2564
rel: 'stylesheet',
2665
href: 'style.css',
2766
},
67+
{
68+
rel: 'alternate',
69+
type: 'application/rss+xml',
70+
title: 'Oskar Lindgren Tech Blog FullStack Dev',
71+
href: 'https://www.oskarlindgren.se/blog/feed.xml',
72+
},
2873
],
2974
},
3075

@@ -48,44 +93,15 @@ export default {
4893
'@nuxtjs/feed',
4994
],
5095

51-
feed() {
52-
const baseUrlArticles = 'https://oskarlindgren.se/blog';
53-
const baseLinkFeedArticles = '/feed/articles';
54-
const feedFormats = {
55-
rss: { type: 'rss2', file: 'rss.xml' },
56-
json: { type: 'json1', file: 'feed.json' },
57-
};
58-
const { $content } = require('@nuxt/content');
59-
60-
const createFeedArticles = async function (feed) {
61-
feed.options = {
62-
title: 'My Blog',
63-
description: 'I write about technology',
64-
link: baseUrlArticles,
65-
};
66-
const articles = await $content('articles').fetch();
67-
68-
articles.forEach((article) => {
69-
const url = `${baseUrlArticles}/${article.slug}`;
70-
71-
feed.addItem({
72-
title: article.title,
73-
id: url,
74-
link: url,
75-
date: article.published,
76-
description: article.summary,
77-
content: article.summary,
78-
author: article.authors,
79-
});
80-
});
81-
};
82-
83-
return Object.values(feedFormats).map(({ file, type }) => ({
84-
path: `${baseLinkFeedArticles}/${file}`,
85-
type,
86-
create: createFeedArticles,
87-
}));
88-
},
96+
feed: [
97+
{
98+
path: '/feed.xml',
99+
create,
100+
cacheTime: 1000 * 60 * 15,
101+
type: 'rss2',
102+
data: ['blog', 'xml'],
103+
},
104+
],
89105

90106
render: {
91107
injectScripts: false,

0 commit comments

Comments
 (0)