Skip to content

Commit 8cdf714

Browse files
committed
add deterministic rand, satori not cached
1 parent f6034c3 commit 8cdf714

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// src/utils/array.ts
2+
3+
export const hashString = (str: string): number => {
4+
let hash = 0;
5+
6+
for (let i = 0; i < str.length; i++) {
7+
hash = (hash * 31 + str.charCodeAt(i)) >>> 0;
8+
}
9+
10+
return hash;
11+
};
12+
13+
/** Deterministic. Enables caching in build. */
14+
export const getRandomElementFromArrayBySeed = <T>(arr: T[], seed: string, salt = ''): T => {
15+
const hash = hashString(seed + salt);
16+
17+
return arr[hash % arr.length];
18+
};
19+
20+
21+
import { default as twColors } from 'tailwindcss/colors';
22+
23+
import { getRandomElementFromArrayBySeed as rndFn } from '@/utils/array';
24+
25+
import type { DefaultColors } from 'tailwindcss/types/generated/colors';
26+
27+
type ColorKeys = keyof DefaultColors;
28+
type ShadeKeys = keyof DefaultColors[ColorKeys];
29+
30+
const colors = ['gray', 'indigo', 'yellow', 'blue', 'cyan', 'lime', 'sky', 'white'] as ColorKeys[];
31+
const shades = [50, 100, 200] as ShadeKeys[];
32+
const directions = ['to right', 'to bottom', '45deg'];
33+
34+
// to support white
35+
// Todo: for cached build should depend on title arg
36+
const getRandomColor = (seed: string, salt: string) => {
37+
const rndColor = rndFn(colors, seed, `color-${salt}`);
38+
return rndColor === 'white' ? rndColor : twColors[rndColor][rndFn(shades, `shade-${salt}`)];
39+
};
40+
41+
export const getRandomGradient = (seed: string) =>
42+
`background: linear-gradient(${rndFn(directions, seed, 'direction')}, ${getRandomColor(seed, 'start')}, ${getRandomColor(seed, 'end')})`;
43+
44+
export const grayGradient = `background: linear-gradient(to right, ${twColors.gray[100]}, ${twColors.gray[300]})`;
45+

src/content/post/2026/02-26-vercel-static-github-actions/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tags:
99
- devops
1010
- serverless
1111
- astro
12-
category: tutorial
12+
category: tutorials
1313
toc: true
1414
draft: false
1515
---

src/utils/gradients.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { default as twColors } from 'tailwindcss/colors';
22

3-
import { getRandomElementFromArray as rnd } from '@/utils/array';
3+
import { getRandomElementFromArray as rndFn } from '@/utils/array';
44

55
import type { DefaultColors } from 'tailwindcss/types/generated/colors';
66

@@ -12,13 +12,13 @@ const shades = [50, 100, 200] as ShadeKeys[];
1212
const directions = ['to right', 'to bottom', '45deg'];
1313

1414
// to support white
15-
// Todo: for cached build should depend on title arg
15+
// Todo: for cached build should depend on title arg, deterministic random. Won't work. Satori renders each time, no caching, outside of Astro build caching.
1616
const getRandomColor = () => {
17-
const rndColor = rnd(colors);
18-
return rndColor === 'white' ? rndColor : twColors[rndColor][rnd(shades)];
17+
const rndColor = rndFn(colors);
18+
return rndColor === 'white' ? rndColor : twColors[rndColor][rndFn(shades)];
1919
};
2020

2121
export const getRandomGradient = () =>
22-
`background: linear-gradient(${rnd(directions)}, ${getRandomColor()}, ${getRandomColor()})`;
22+
`background: linear-gradient(${rndFn(directions)}, ${getRandomColor()}, ${getRandomColor()})`;
2323

2424
export const grayGradient = `background: linear-gradient(to right, ${twColors.gray[100]}, ${twColors.gray[300]})`;

0 commit comments

Comments
 (0)