Skip to content

Commit ffd842a

Browse files
committed
add changelogs hash to body tag
1 parent bcac422 commit ffd842a

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

apps/changelog/prisma/seed/seed.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ async function seed() {
4343
updatedAt: new Date('01/01/2020'),
4444
title: 'Changelog 3',
4545
slug: 'changelog-3',
46-
content: 'Changelog 3 content with [markdown content](https://de.wikipedia.org/wiki/Markdown)',
47-
summary: 'Changelog 3 summary with [markdown content](https://de.wikipedia.org/wiki/Markdown)',
46+
content:
47+
'Changelog 3 content with [markdown content](https://de.wikipedia.org/wiki/Markdown)',
48+
summary:
49+
'Changelog 3 summary with [markdown content](https://de.wikipedia.org/wiki/Markdown)',
4850
published: true,
4951
deleted: false,
5052
},

apps/changelog/src/app/changelog/feed.xml/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function GET() {
1414
ttl: 60,
1515
});
1616

17-
const allChangelogs = await getChangelogs();
17+
const {changelogs: allChangelogs} = await getChangelogs();
1818

1919
if (allChangelogs) {
2020
allChangelogs

apps/changelog/src/app/changelog/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {Element} from 'hast';
1313
export const dynamic = 'force-dynamic';
1414

1515
export default async function Page() {
16-
const changelogs = await getChangelogs();
16+
const {changelogs} = await getChangelogs();
1717

1818
const changelogsWithPublishedAt = changelogs.filter(changelog => {
1919
return changelog.publishedAt !== null;

apps/changelog/src/app/layout.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {getChangelogs} from '@/server/utils';
12
import './globals.css';
23

34
import {Theme} from '@radix-ui/themes';
@@ -30,10 +31,11 @@ export const metadata: Metadata = {
3031
},
3132
};
3233

33-
export default function RootLayout({children}: {children: React.ReactNode}) {
34+
export default async function RootLayout({children}: {children: React.ReactNode}) {
35+
const {hash: chngelogsHash} = await getChangelogs();
3436
return (
3537
<html lang="en">
36-
<body className={`${rubik.variable}`}>
38+
<body className={`${rubik.variable}`} data-content-hash={chngelogsHash}>
3739
<Theme accentColor="iris" grayColor="sand" radius="large" scaling="95%">
3840
{children}
3941
</Theme>

apps/changelog/src/server/utils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {prismaClient} from '@/server/prisma-client';
2+
import {Changelog} from '@prisma/client';
23
import {unstable_cache} from 'next/cache';
34

45
export const getChangelogs = unstable_cache(
56
async () => {
6-
return await prismaClient.changelog.findMany({
7+
const changelogs = await prismaClient.changelog.findMany({
78
include: {
89
categories: true,
910
},
@@ -14,6 +15,27 @@ export const getChangelogs = unstable_cache(
1415
publishedAt: 'desc',
1516
},
1617
});
18+
const hashHex = (buffer: ArrayBuffer) => {
19+
const hexCodes = Array.from(new Uint8Array(buffer)).map(value =>
20+
value.toString(16).padStart(2, '0')
21+
);
22+
return hexCodes.join('');
23+
};
24+
const hashChangelogs = (changelogs: Changelog[]) =>
25+
crypto.subtle
26+
.digest(
27+
'SHA-256',
28+
new TextEncoder().encode(
29+
changelogs
30+
.map(changelogs => changelogs.id + ':' + changelogs.updatedAt)
31+
.join(',')
32+
)
33+
)
34+
.then(hashHex);
35+
return {
36+
changelogs,
37+
hash: await hashChangelogs(changelogs),
38+
};
1739
},
1840
['changelogs'],
1941
{tags: ['changelogs']}

0 commit comments

Comments
 (0)