Skip to content

Commit 9ba89a8

Browse files
committed
Added Sitemap Generator"
1 parent 5ac4201 commit 9ba89a8

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
"dev": "next dev",
55
"build": "next build",
66
"start": "next start",
7-
"lint": "next lint"
7+
"lint": "next lint",
8+
"postbuild": "NODE_OPTIONS='--experimental-json-modules' node ./scripts/generate-sitemap.mjs"
89
},
910
"dependencies": {
1011
"next": "12.1.0",
12+
"prop-types": "^15.8.1",
1113
"react": "17.0.2",
12-
"react-dom": "17.0.2"
14+
"react-dom": "17.0.2",
15+
"react-feather": "^2.0.10",
16+
"react-parallax": "^3.5.1",
17+
"styled-components": "^5.3.5",
18+
"prettier": "^2.5.1",
19+
"globby": "13.1.2"
1320
},
1421
"devDependencies": {
1522
"eslint": "8.9.0",

public/sitemap.xml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </urlset>

scripts/generate-sitemap.mjs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { writeFileSync } from 'fs'
2+
3+
import { globby } from 'globby'
4+
import prettier from 'prettier'
5+
6+
async function generate() {
7+
const prettierConfig = await prettier.resolveConfig('./.prettierrc.js')
8+
const pages = await globby([
9+
'pages/*.tsx',
10+
'data/**/*.mdx',
11+
'!data/*.mdx',
12+
'!pages/_*.tsx',
13+
'!pages/api',
14+
'!pages/404.tsx',
15+
])
16+
17+
const sitemap = `
18+
<?xml version="1.0" encoding="UTF-8"?>
19+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
20+
${pages
21+
.map(page => {
22+
const path = page
23+
.replace('pages', '')
24+
.replace('data', '')
25+
.replace('.tsx', '')
26+
.replace('.mdx', '')
27+
const route = path === '/index' ? '' : path
28+
return `
29+
<url>
30+
<loc>${`https://cretu.dev${route}`}</loc>
31+
</url>
32+
`
33+
})
34+
.join('')}
35+
</urlset>
36+
`
37+
38+
const formatted = prettier.format(sitemap, {
39+
...prettierConfig,
40+
parser: 'html',
41+
})
42+
43+
// eslint-disable-next-line no-sync
44+
writeFileSync('public/sitemap.xml', formatted)
45+
}
46+
47+
generate()

0 commit comments

Comments
 (0)