Fix sitemap.xml dynamic generation

This commit is contained in:
VityaSchel
2023-10-12 18:45:24 +04:00
parent 813fa218e9
commit 9ae56a82a8
9 changed files with 31 additions and 28 deletions

2
.gitignore vendored
View File

@@ -35,5 +35,3 @@ yarn-error.log*
next-env.d.ts next-env.d.ts
.env .env
public/sitemap.xml

1
.npmrc
View File

@@ -1 +0,0 @@
enable-pre-post-scripts=true

View File

@@ -1,12 +0,0 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const groups = require('./src/shared/data/groups.json')
/** @type {import('next-sitemap').IConfig} */
module.exports = {
siteUrl: 'https://kspsuti.ru',
generateRobotsTxt: false,
generateIndexSitemap: false,
changefreq: 'weekly',
exclude: ['/'],
additionalPaths: async () => Object.keys(groups).map(groupName => `/${groupName}`)
}

View File

@@ -5,7 +5,6 @@
"scripts": { "scripts": {
"dev": "next dev", "dev": "next dev",
"build": "next build", "build": "next build",
"postbuild": "next-sitemap",
"start": "next start", "start": "next start",
"lint": "next lint" "lint": "next lint"
}, },

View File

@@ -1,2 +1,4 @@
User-agent: * User-agent: *
Disallow: Disallow:
Sitemap: https://kspsuti.ru/sitemap.xml

View File

@@ -5,7 +5,7 @@ import { getSchedule } from '@/app/agregator/schedule'
import { NextSerialized, nextDeserializer, nextSerialized } from '@/app/utils/date-serializer' import { NextSerialized, nextDeserializer, nextSerialized } from '@/app/utils/date-serializer'
import { NavBar } from '@/widgets/navbar' import { NavBar } from '@/widgets/navbar'
import { LastUpdateAt } from '@/entities/last-update-at' import { LastUpdateAt } from '@/entities/last-update-at'
import groups from '@/shared/data/groups.json' import { groups } from '@/shared/data/groups'
import crypto from 'crypto' import crypto from 'crypto'
import React from 'react' import React from 'react'
import { getDayOfWeek } from '@/shared/utils' import { getDayOfWeek } from '@/shared/utils'
@@ -61,7 +61,6 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
if (group && Object.hasOwn(groups, group) && group in groups) { if (group && Object.hasOwn(groups, group) && group in groups) {
let schedule let schedule
let parsedAt let parsedAt
const groupName = group as keyof typeof groups
const cachedSchedule = cachedSchedules.get(group) const cachedSchedule = cachedSchedules.get(group)
if (cachedSchedule?.lastFetched && Date.now() - cachedSchedule.lastFetched.getTime() < maxCacheDurationInMS) { if (cachedSchedule?.lastFetched && Date.now() - cachedSchedule.lastFetched.getTime() < maxCacheDurationInMS) {
@@ -69,10 +68,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
parsedAt = cachedSchedule.lastFetched parsedAt = cachedSchedule.lastFetched
} else { } else {
try { try {
const group = groups[groupName] as [number, string] schedule = await getSchedule(...groups[group])
schedule = await getSchedule(...group)
parsedAt = new Date() parsedAt = new Date()
cachedSchedules.set(groupName, { lastFetched: new Date(), results: schedule }) cachedSchedules.set(group, { lastFetched: new Date(), results: schedule })
} catch(e) { } catch(e) {
if (cachedSchedule?.lastFetched) { if (cachedSchedule?.lastFetched) {
schedule = cachedSchedule.results schedule = cachedSchedule.results
@@ -105,7 +103,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
props: nextSerialized({ props: nextSerialized({
schedule: schedule, schedule: schedule,
parsedAt: parsedAt, parsedAt: parsedAt,
group: groups[groupName][1] group: groups[group][1]
}) })
} }
} else { } else {

View File

@@ -0,0 +1,19 @@
import { ISitemapField, getServerSideSitemapLegacy } from 'next-sitemap'
import { GetServerSideProps } from 'next'
import { groups } from '@/shared/data/groups'
export const getServerSideProps: GetServerSideProps = async (ctx) => {
const siteURL = 'https://kspsuti.ru'
const fields = Object.keys(groups).map<ISitemapField>(group => (
{
loc: `${siteURL}/${group}`,
changefreq: 'weekly',
priority: 0.8
}
))
return getServerSideSitemapLegacy(ctx, fields)
}
export default function Sitemap() { }

View File

@@ -1,4 +0,0 @@
{
"ps7": [146, "ПС-7"],
"pks35k": [78, "ПКС-35к"]
}

View File

@@ -0,0 +1,4 @@
export const groups: { [group: string]: [number, string] } = {
ps7: [146, 'ПС-7'],
pks35k: [78, 'ПКС-35к']
}