1
1
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 : / B l o g S t u f f ! / ,
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
+ } ;
2
41
3
42
export default {
4
43
// Target: https://go.nuxtjs.dev/config-target
@@ -25,6 +64,12 @@ export default {
25
64
rel : 'stylesheet' ,
26
65
href : 'style.css' ,
27
66
} ,
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
+ } ,
28
73
] ,
29
74
} ,
30
75
@@ -48,44 +93,15 @@ export default {
48
93
'@nuxtjs/feed' ,
49
94
] ,
50
95
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
+ ] ,
89
105
90
106
render : {
91
107
injectScripts : false ,
0 commit comments