diff options
| author | Elad Rosenheim <eladroz@users.noreply.github.com> | 2024-04-23 17:55:24 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 17:55:24 +0300 |
| commit | 7e40aa188979311e706154dfcf52e47975f99cde (patch) | |
| tree | 1368cc922524b5b1eb7eef4684fccab0fbc3628d /src/utils.ts | |
| parent | 5396de306695226c939384598410761297739fef (diff) | |
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 <tbdizainas@gmail.com>
Diffstat (limited to 'src/utils.ts')
| -rw-r--r-- | src/utils.ts | 55 |
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; +} |
