From 7e40aa188979311e706154dfcf52e47975f99cde Mon Sep 17 00:00:00 2001 From: Elad Rosenheim Date: Tue, 23 Apr 2024 17:55:24 +0300 Subject: Revalidation & Blobs pages, tweaks (#8) * added blobs.astro page * updated blobs * updated index.astro and npm dependencies * updated api blobs * Revalidation example WIP * Revalidation by tag example * Update revalidation.astro * Image CDN fixes * Text fixes * updated colors (#9) * updated colors * updated README.md * updated Layout.astro bg color * updated template colors * Inline Inter font * Inline noise.png * Remove remaining "Astro.js" --------- Co-authored-by: TomasBankauskas --- src/utils.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/utils.ts (limited to 'src/utils.ts') 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 { + // 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; +} -- cgit v1.2.3