Sitemap generator now works for main pages and posts
This commit is contained in:
parent
25e70c36d4
commit
2730ceeb62
8 changed files with 111 additions and 11 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -16,3 +16,5 @@ yarn-error.log*
|
|||
.idea/
|
||||
#Feeds
|
||||
public/feeds/
|
||||
public/sitemap*
|
||||
|
||||
|
|
|
|||
14
data/external/cms.js
vendored
14
data/external/cms.js
vendored
|
|
@ -52,6 +52,20 @@ export const getAllPosts = async (filter, page, limit) => {
|
|||
return await res.json()
|
||||
}
|
||||
|
||||
export const getSinglePost = async (postId) => {
|
||||
const { serverRuntimeConfig} = getConfig()
|
||||
const url = serverRuntimeConfig.base_path + 'articles?filters[Slug][$eq]=' + postId + '&populate=*'
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: new Headers({
|
||||
'Authorization': serverRuntimeConfig.strapi_token,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
})
|
||||
})
|
||||
const artdata = await res.json()
|
||||
return artdata.data[0].attributes
|
||||
}
|
||||
|
||||
export const getAllPodcastSeries = async () => {
|
||||
const { serverRuntimeConfig } = getConfig()
|
||||
const qs = require('qs')
|
||||
|
|
|
|||
|
|
@ -0,0 +1,71 @@
|
|||
import { getAllPosts } from "../external/cms"
|
||||
import fs from "fs"
|
||||
import prettier from "prettier"
|
||||
import config from './config'
|
||||
|
||||
|
||||
export const generateSitemap = async () => {
|
||||
const getDate = new Date().toISOString()
|
||||
|
||||
const staticPaths = fs.readdirSync("pages", {withFileTypes: true})
|
||||
.filter((staticPage) => {
|
||||
if(staticPage.isFile()) {
|
||||
return ![
|
||||
"api",
|
||||
"_app.js",
|
||||
"_document.js",
|
||||
"404.js",
|
||||
"sitemap.xml.js",
|
||||
"index.js"
|
||||
].includes(staticPage.name);
|
||||
}
|
||||
})
|
||||
.map((staticPagePath) => {
|
||||
console.log(staticPagePath)
|
||||
return `${config.siteURL}/${staticPagePath.name}`;
|
||||
}
|
||||
);
|
||||
|
||||
const postData = await getAllPosts()
|
||||
const postList = []
|
||||
postData.data.forEach(post => postList.push({slug: post.attributes.Slug, updatedAt: post.attributes.updatedAt}))
|
||||
|
||||
console.log(postList)
|
||||
|
||||
const formatted = sitemap => prettier.format(sitemap, { parser: "html" });
|
||||
|
||||
const pageListMap = staticPaths.map(page => {
|
||||
return `
|
||||
<url>
|
||||
<loc>${page}</loc>
|
||||
<lastmod>${getDate}</lastmod>
|
||||
<priority>1</priority>
|
||||
</url>`
|
||||
}).join("")
|
||||
|
||||
const postListSiteMap = postList.map(post => {
|
||||
return `
|
||||
<url>
|
||||
<loc>${`${config.siteURL}/news/${post.slug}`}</loc>
|
||||
<lastmod>${post.updatedAt}</lastmod>
|
||||
<priority>0.5</priority>
|
||||
</url>`
|
||||
})
|
||||
.join("")
|
||||
|
||||
const generatedSitemap = `
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset
|
||||
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
|
||||
>${pageListMap}
|
||||
${postListSiteMap}
|
||||
</urlset>
|
||||
`
|
||||
|
||||
console.log(generatedSitemap)
|
||||
const formattedSitemap = [formatted(generatedSitemap)];
|
||||
|
||||
fs.writeFileSync("public/sitemap-posts.xml", generatedSitemap, "utf8")
|
||||
}
|
||||
|
|
@ -15,6 +15,7 @@
|
|||
"@zeit/next-less": "^1.0.1",
|
||||
"bootstrap": "^4.6.0",
|
||||
"feed": "^4.2.2",
|
||||
"fs": "^0.0.1-security",
|
||||
"less": "^3.13.1",
|
||||
"mdbreact": "^5.1.0",
|
||||
"next": "^10.2.3",
|
||||
|
|
@ -22,6 +23,7 @@
|
|||
"next-fonts": "^1.5.1",
|
||||
"next-images": "^1.8.1",
|
||||
"postcss": "^8.3.6",
|
||||
"prettier": "^2.7.1",
|
||||
"qs": "^6.10.3",
|
||||
"react": "^16.14.0",
|
||||
"react-bootstrap": "^1.6.1",
|
||||
|
|
|
|||
|
|
@ -29,6 +29,13 @@ class CustomDocument extends Document {
|
|||
`,
|
||||
}}
|
||||
/>
|
||||
<link rel="icon" href="/images/favicon-32.png" sizes="32x32" />
|
||||
<link rel="icon" href="/images/favicon-57.png" sizes="57x57" />
|
||||
<link rel="icon" href="/images/favicon-76.png" sizes="76x76" />
|
||||
<link rel="icon" href="/images/favicon-96.png" sizes="96x96" />
|
||||
<link rel="icon" href="/images/favicon-128.png" sizes="128x128" />
|
||||
<link rel="icon" href="/images/favicon-192.png" sizes="192x192" />
|
||||
<link rel="icon" href="/images/favicon-228.png" sizes="228x228" />
|
||||
</Fragment>
|
||||
<link href="https://fonts.googleapis.com/css?family=Cabin|Jaldi" rel="stylesheet" />
|
||||
</Head>
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@ import Head from 'next/head'
|
|||
import Image from 'next/image';
|
||||
import { FEEDS, getFeed } from "../lib/rss"
|
||||
import { generatePodcastFeeds, generateRssFeed } from "../data/internal/feed-generator"
|
||||
import { generateSitemap } from "../data/internal/sitemap-generator"
|
||||
|
||||
export async function getStaticProps(context) {
|
||||
generateRssFeed()
|
||||
generateRssFeed('tech-and-disability')
|
||||
generatePodcastFeeds()
|
||||
generateSitemap()
|
||||
const { serverRuntimeConfig, publicRuntimeConfig, strapiConfig } = getConfig()
|
||||
console.log(serverRuntimeConfig.strapi_token)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import "../../components/main.js"
|
|||
import Layout from "../../components/main.js"
|
||||
import FeatureImage from "../../components/featureimage.js"
|
||||
import StorySideBar from '../../components/storysidebar.js'
|
||||
import { getAllPosts } from '../../data/external/cms'
|
||||
import { getAllPosts, getSinglePost } from '../../data/external/cms'
|
||||
import * as gtag from "../../lib/gtag"
|
||||
import Image from 'next/image';
|
||||
import Head from 'next/head'
|
||||
|
|
@ -44,16 +44,8 @@ export async function getStaticPaths() {
|
|||
export async function getStaticProps({params}) {
|
||||
const { serverRuntimeConfig, publicRuntimeConfig } = getConfig()
|
||||
const slug = params.slug
|
||||
const url = serverRuntimeConfig.base_path + 'articles?filters[Slug][$eq]=' + slug + '&populate=*'
|
||||
|
||||
const res = await fetch(url, {
|
||||
headers: new Headers({
|
||||
'Authorization': serverRuntimeConfig.strapi_token,
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
})
|
||||
})
|
||||
const artdata = await res.json()
|
||||
const article_obj = artdata.data[0].attributes
|
||||
const article_obj = await getSinglePost(slug)
|
||||
|
||||
const qs = require('qs')
|
||||
const query = qs.stringify({
|
||||
|
|
|
|||
10
yarn.lock
10
yarn.lock
|
|
@ -1312,6 +1312,11 @@ fs-constants@^1.0.0:
|
|||
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
|
||||
integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==
|
||||
|
||||
fs@^0.0.1-security:
|
||||
version "0.0.1-security"
|
||||
resolved "https://registry.yarnpkg.com/fs/-/fs-0.0.1-security.tgz#8a7bd37186b6dddf3813f23858b57ecaaf5e41d4"
|
||||
integrity sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==
|
||||
|
||||
fsevents@~2.3.1:
|
||||
version "2.3.2"
|
||||
resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.3.2.tgz#8a526f78b8fdf4623b709e0b975c52c24c02fd1a"
|
||||
|
|
@ -2655,6 +2660,11 @@ prebuild-install@^7.1.0:
|
|||
tar-fs "^2.0.0"
|
||||
tunnel-agent "^0.6.0"
|
||||
|
||||
prettier@^2.7.1:
|
||||
version "2.7.1"
|
||||
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
|
||||
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==
|
||||
|
||||
process-nextick-args@~2.0.0:
|
||||
version "2.0.1"
|
||||
resolved "https://registry.yarnpkg.com/process-nextick-args/-/process-nextick-args-2.0.1.tgz#7820d9b16120cc55ca9ae7792680ae7dba6d7fe2"
|
||||
|
|
|
|||
Loading…
Reference in a new issue