diff --git a/next.config.js b/next.config.js index 2a23b1d..60707f6 100644 --- a/next.config.js +++ b/next.config.js @@ -5,7 +5,8 @@ const nextConfig = { permanent: false, destination: '/ps7', source: '/' - }] + }], + generateEtags: false } module.exports = nextConfig diff --git a/src/pages/[group].tsx b/src/pages/[group].tsx index f2bd5b3..2d5fd6e 100644 --- a/src/pages/[group].tsx +++ b/src/pages/[group].tsx @@ -6,6 +6,7 @@ import { NextSerialized, nextDeserializer, nextSerialized } from '@/app/utils/da import { NavBar } from '@/widgets/navbar' import { LastUpdateAt } from '@/entities/last-update-at' import { groups } from '@/shared/data/groups' +import crypto from 'crypto' type PageProps = NextSerialized<{ schedule: Day[] @@ -51,6 +52,26 @@ export async function getServerSideProps(context: GetServerSidePropsContext<{ gr } } + const getSha256Hash = (input: string) => { + const hash = crypto.createHash('sha256') + hash.update(input) + return hash.digest('hex') + } + + const etag = getSha256Hash(JSON.stringify(nextSerialized(schedule))) + + const ifNoneMatch = context.req.headers['if-none-match'] + if (ifNoneMatch === etag) { + context.res.writeHead(304, { ETag: etag }) + context.res.end() + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore Content has not changed + return { props: {} } + } + + console.log('etag', etag) + + context.res.setHeader('ETag', etag) return { props: nextSerialized({ schedule: schedule,