Fix sitemap.xml dynamic generation
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -35,5 +35,3 @@ yarn-error.log*
|
||||
next-env.d.ts
|
||||
|
||||
.env
|
||||
|
||||
public/sitemap.xml
|
||||
@@ -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}`)
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"postbuild": "next-sitemap",
|
||||
"start": "next start",
|
||||
"lint": "next lint"
|
||||
},
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
User-agent: *
|
||||
Disallow:
|
||||
|
||||
Sitemap: https://kspsuti.ru/sitemap.xml
|
||||
@@ -5,7 +5,7 @@ import { getSchedule } from '@/app/agregator/schedule'
|
||||
import { NextSerialized, nextDeserializer, nextSerialized } from '@/app/utils/date-serializer'
|
||||
import { NavBar } from '@/widgets/navbar'
|
||||
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 React from 'react'
|
||||
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) {
|
||||
let schedule
|
||||
let parsedAt
|
||||
const groupName = group as keyof typeof groups
|
||||
|
||||
const cachedSchedule = cachedSchedules.get(group)
|
||||
if (cachedSchedule?.lastFetched && Date.now() - cachedSchedule.lastFetched.getTime() < maxCacheDurationInMS) {
|
||||
@@ -69,10 +68,9 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
|
||||
parsedAt = cachedSchedule.lastFetched
|
||||
} else {
|
||||
try {
|
||||
const group = groups[groupName] as [number, string]
|
||||
schedule = await getSchedule(...group)
|
||||
schedule = await getSchedule(...groups[group])
|
||||
parsedAt = new Date()
|
||||
cachedSchedules.set(groupName, { lastFetched: new Date(), results: schedule })
|
||||
cachedSchedules.set(group, { lastFetched: new Date(), results: schedule })
|
||||
} catch(e) {
|
||||
if (cachedSchedule?.lastFetched) {
|
||||
schedule = cachedSchedule.results
|
||||
@@ -105,7 +103,7 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr
|
||||
props: nextSerialized({
|
||||
schedule: schedule,
|
||||
parsedAt: parsedAt,
|
||||
group: groups[groupName][1]
|
||||
group: groups[group][1]
|
||||
})
|
||||
}
|
||||
} else {
|
||||
|
||||
19
src/pages/sitemap.xml/index.tsx
Normal file
19
src/pages/sitemap.xml/index.tsx
Normal 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() { }
|
||||
@@ -1,4 +0,0 @@
|
||||
{
|
||||
"ps7": [146, "ПС-7"],
|
||||
"pks35k": [78, "ПКС-35к"]
|
||||
}
|
||||
4
src/shared/data/groups.ts
Normal file
4
src/shared/data/groups.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export const groups: { [group: string]: [number, string] } = {
|
||||
ps7: [146, 'ПС-7'],
|
||||
pks35k: [78, 'ПКС-35к']
|
||||
}
|
||||
Reference in New Issue
Block a user