Fix sitemap.xml dynamic generation
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -34,6 +34,4 @@ yarn-error.log*
|
|||||||
*.tsbuildinfo
|
*.tsbuildinfo
|
||||||
next-env.d.ts
|
next-env.d.ts
|
||||||
|
|
||||||
.env
|
.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": {
|
"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"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
User-agent: *
|
User-agent: *
|
||||||
Disallow:
|
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 { 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 {
|
||||||
|
|||||||
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