summaryrefslogtreecommitdiff
path: root/src/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.ts')
-rw-r--r--src/utils.ts55
1 files changed, 55 insertions, 0 deletions
diff --git a/src/utils.ts b/src/utils.ts
new file mode 100644
index 0000000..7a32b7d
--- /dev/null
+++ b/src/utils.ts
@@ -0,0 +1,55 @@
+import blobshape from 'blobshape';
+import { uniqueNamesGenerator, adjectives, animals } from 'unique-names-generator';
+
+// Note: this only works on the server side
+export function getNetlifyContext() {
+ return process.env.CONTEXT;
+}
+
+export function randomInt(min: number, max: number) {
+ return Math.floor(Math.random() * (max - min + 1) + min);
+}
+
+export function uniqueName() {
+ const config = {
+ dictionaries: [adjectives, animals],
+ separator: '-',
+ length: 2
+ };
+ return uniqueNamesGenerator(config) + '-' + randomInt(100, 999);
+}
+
+export function generateBlob(parameters?: any) {
+ const gradientColors = [
+ ['#2E3192', '#1BFFFF'],
+ ['#93A5CF', '#E4EfE9'],
+ ['#BFF098', '#6FD6FF'],
+ ['#A1C4FD', '#C2E9FB'],
+ ['#11998E', '#38EF7D'],
+ ['#D8B5FF', '#1EAE98']
+ ];
+
+ parameters = {
+ seed: null,
+ size: 512,
+ edges: randomInt(3, 20),
+ growth: randomInt(2, 9),
+ name: uniqueName(),
+ colors: gradientColors[randomInt(0, gradientColors.length - 1)],
+ ...parameters
+ };
+ const { path: svgPath, seedValue: seed } = blobshape(parameters);
+ return { parameters: { ...parameters, seed }, svgPath };
+}
+
+export function cacheHeaders(maxAgeDays = 365, cacheTags?: string[]): Record<string, string> {
+ // As far as the browser is concerned, it must revalidate on every request.
+ // However, Netlify CDN is told to keep the content cached for up to maxAgeDays (note: new deployment bust the cache by default).
+ // We're also setting cache tags to be able to later purge via API (see: https://www.netlify.com/blog/cache-tags-and-purge-api-on-netlify/)
+ const headers = {
+ 'Cache-Control': 'public, max-age=0, must-revalidate', // Tell browsers to always revalidate
+ 'Netlify-CDN-Cache-Control': `public, max-age=${maxAgeDays * 86_400}, must-revalidate` // Tells Netlify CDN the max allwed cache duration
+ };
+ if (cacheTags?.length > 0) headers['Cache-Tag'] = cacheTags.join(',');
+ return headers;
+}