Skip to content

Commit

Permalink
Added Sitemap Generator"
Browse files Browse the repository at this point in the history
  • Loading branch information
rishaandesai committed Sep 26, 2022
1 parent 5ac4201 commit 9ba89a8
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"postbuild": "NODE_OPTIONS='--experimental-json-modules' node ./scripts/generate-sitemap.mjs"
},
"dependencies": {
"next": "12.1.0",
"prop-types": "^15.8.1",
"react": "17.0.2",
"react-dom": "17.0.2"
"react-dom": "17.0.2",
"react-feather": "^2.0.10",
"react-parallax": "^3.5.1",
"styled-components": "^5.3.5",
"prettier": "^2.5.1",
"globby": "13.1.2"
},
"devDependencies": {
"eslint": "8.9.0",
Expand Down
2 changes: 2 additions & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> </urlset>
47 changes: 47 additions & 0 deletions scripts/generate-sitemap.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { writeFileSync } from 'fs'

import { globby } from 'globby'
import prettier from 'prettier'

async function generate() {
const prettierConfig = await prettier.resolveConfig('./.prettierrc.js')
const pages = await globby([
'pages/*.tsx',
'data/**/*.mdx',
'!data/*.mdx',
'!pages/_*.tsx',
'!pages/api',
'!pages/404.tsx',
])

const sitemap = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages
.map(page => {
const path = page
.replace('pages', '')
.replace('data', '')
.replace('.tsx', '')
.replace('.mdx', '')
const route = path === '/index' ? '' : path
return `
<url>
<loc>${`https://cretu.dev${route}`}</loc>
</url>
`
})
.join('')}
</urlset>
`

const formatted = prettier.format(sitemap, {
...prettierConfig,
parser: 'html',
})

// eslint-disable-next-line no-sync
writeFileSync('public/sitemap.xml', formatted)
}

generate()

0 comments on commit 9ba89a8

Please sign in to comment.