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 --- .vscode/settings.json | 27 +++ README.md | 4 +- astro.config.mjs | 7 +- package-lock.json | 347 +++++++++++++++++++++++---- package.json | 21 +- public/images/noise.svg | 7 - public/netlify-logo.svg | 12 +- src/components/ContextAlert.astro | 2 +- src/components/Footer.astro | 2 +- src/components/Markdown.astro | 4 +- src/layouts/Layout.astro | 4 +- src/pages/api/blob.ts | 20 ++ src/pages/api/blobs.ts | 27 +++ src/pages/api/revalidate.ts | 19 ++ src/pages/blobs.astro | 8 - src/pages/blobs/_components/NewShape.tsx | 60 +++++ src/pages/blobs/_components/ShapeEditor.tsx | 18 ++ src/pages/blobs/_components/ShapePreview.tsx | 19 ++ src/pages/blobs/_components/StoredShapes.tsx | 78 ++++++ src/pages/blobs/index.astro | 18 ++ src/pages/edge/index.astro | 2 +- src/pages/image-cdn.astro | 39 +-- src/pages/index.astro | 16 +- src/pages/revalidation.astro | 61 +++++ src/styles/globals.css | 8 +- src/types.ts | 13 + src/utils.js | 4 - src/utils.ts | 55 +++++ tailwind.config.mjs | 22 +- tsconfig.json | 8 +- 30 files changed, 812 insertions(+), 120 deletions(-) create mode 100644 .vscode/settings.json delete mode 100644 public/images/noise.svg create mode 100644 src/pages/api/blob.ts create mode 100644 src/pages/api/blobs.ts create mode 100644 src/pages/api/revalidate.ts delete mode 100644 src/pages/blobs.astro create mode 100644 src/pages/blobs/_components/NewShape.tsx create mode 100644 src/pages/blobs/_components/ShapeEditor.tsx create mode 100644 src/pages/blobs/_components/ShapePreview.tsx create mode 100644 src/pages/blobs/_components/StoredShapes.tsx create mode 100644 src/pages/blobs/index.astro create mode 100644 src/pages/revalidation.astro create mode 100644 src/types.ts delete mode 100644 src/utils.js create mode 100644 src/utils.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..ca4e8a8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,27 @@ +{ + "editor.tabSize": 4, + "editor.formatOnSave": true, + "editor.codeActionsOnSave": { + "source.fixAll.eslint": "explicit" + }, + "[javascript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.formatOnSave": true + }, + "[yaml]": { + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.tabSize": 2 + }, + "[toml]": { + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.tabSize": 2 + }, + "[markdown]": { + "editor.defaultFormatter": "esbenp.prettier-vscode", + "editor.tabSize": 2 + }, + "typescript.tsdk": "node_modules/typescript/lib", + "files.associations": { + "*.css": "tailwindcss", + } +} diff --git a/README.md b/README.md index 4bb1df6..c87ce8b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Astro Starter +# Astro on Netlify Platform Starter ## Commands @@ -11,4 +11,4 @@ All commands are run from the root of the project, from a terminal: | `npm run build` | Build your production site to `./dist/` | | `npm run preview` | Preview your build locally, before deploying | | `npm run astro ...` | Run CLI commands like `astro add`, `astro check` | -| `npm run astro -- --help` | Get help using the Astro CLI | \ No newline at end of file +| `npm run astro -- --help` | Get help using the Astro CLI | diff --git a/astro.config.mjs b/astro.config.mjs index 0798920..adb7e74 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,11 +1,16 @@ import { defineConfig } from 'astro/config'; +import netlify from '@astrojs/netlify'; +import react from '@astrojs/react'; import tailwind from '@astrojs/tailwind'; // https://astro.build/config export default defineConfig({ integrations: [ + react(), tailwind({ applyBaseStyles: false }) - ] + ], + output: 'hybrid', + adapter: netlify() }); diff --git a/package-lock.json b/package-lock.json index 00d3e2d..c78a494 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,14 +8,27 @@ "name": "astro-netlify-platform-starter", "version": "0.1.0", "dependencies": { + "@astrojs/netlify": "^5.2.0", + "@astrojs/react": "^3.3.0", "@astrojs/tailwind": "^5.1.0", - "astro": "^4.5.14", + "@fontsource/inter": "^5.0.18", + "@netlify/blobs": "^7.3.0", + "@netlify/functions": "^2.6.0", + "@types/react": "^18.2.79", + "@types/react-dom": "^18.2.25", + "astro": "^4.6.2", + "blobshape": "^1.0.0", "marked": "^12.0.1", - "marked-shiki": "^1.0.1", - "tailwindcss": "^3.4.3" + "marked-shiki": "^1.1.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "tailwindcss": "^3.4.3", + "unique-names-generator": "^4.7.1" }, "devDependencies": { - "daisyui": "^4.9.0" + "@types/blobshape": "^1.0.3", + "@types/node": "^20.12.7", + "daisyui": "^4.10.2" } }, "node_modules/@alloc/quick-lru": { @@ -52,11 +65,11 @@ "integrity": "sha512-6B13lz5n6BrbTqCTwhXjJXuR1sqiX/H6rTxzlXx+lN1NnV4jgnq/KJldCQaUWJzPL5SiWahQyinxAbxQtwgPHA==" }, "node_modules/@astrojs/markdown-remark": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/@astrojs/markdown-remark/-/markdown-remark-5.0.0.tgz", - "integrity": "sha512-QBXbxXZamVRoqCNN2gjDXa7qYPUkJZq7KYFfg3DX7rze3QL6xiz4N+Wg202dNPRaIkQa16BV6D8+EHibQFubRg==", + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@astrojs/markdown-remark/-/markdown-remark-5.1.0.tgz", + "integrity": "sha512-S6Z3K2hOB7MfjeDoHsotnP/q2UsnEDB8NlNAaCjMDsGBZfTUbWxyLW3CaphEWw08f6KLZi2ibK9yC3BaMhh2NQ==", "dependencies": { - "@astrojs/prism": "^3.0.0", + "@astrojs/prism": "^3.1.0", "github-slugger": "^2.0.0", "hast-util-from-html": "^2.0.0", "hast-util-to-text": "^4.0.0", @@ -76,15 +89,46 @@ "vfile": "^6.0.1" } }, + "node_modules/@astrojs/netlify": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/@astrojs/netlify/-/netlify-5.2.0.tgz", + "integrity": "sha512-NFg3AdUToV7kbY5w2yHZY5UskqfsmIo9i0zIzQ2GgUcTwjyZCekZ89DLtKCJ1jFVle+QXJGsJAS61EYyptBDBQ==", + "dependencies": { + "@astrojs/underscore-redirects": "^0.3.3", + "@netlify/functions": "^2.0.1", + "esbuild": "^0.19.5" + }, + "peerDependencies": { + "astro": "^4.2.0" + } + }, "node_modules/@astrojs/prism": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/@astrojs/prism/-/prism-3.0.0.tgz", - "integrity": "sha512-g61lZupWq1bYbcBnYZqdjndShr/J3l/oFobBKPA3+qMat146zce3nz2kdO4giGbhYDt4gYdhmoBz0vZJ4sIurQ==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@astrojs/prism/-/prism-3.1.0.tgz", + "integrity": "sha512-Z9IYjuXSArkAUx3N6xj6+Bnvx8OdUSHA8YoOgyepp3+zJmtVYJIl/I18GozdJVW1p5u/CNpl3Km7/gwTJK85cw==", "dependencies": { "prismjs": "^1.29.0" }, "engines": { - "node": ">=18.14.1" + "node": "^18.17.1 || ^20.3.0 || >=21.0.0" + } + }, + "node_modules/@astrojs/react": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/@astrojs/react/-/react-3.3.0.tgz", + "integrity": "sha512-IKHizDgYPTl9TTKcEB+PwmqHqYgCwD3jqd9hGBRITYiMmeiHVMRr2zNNXSmOOv0uWL3/EwsISsrUnjwuxIVZUA==", + "dependencies": { + "@vitejs/plugin-react": "^4.2.0", + "ultrahtml": "^1.3.0" + }, + "engines": { + "node": "^18.17.1 || ^20.3.0 || >=21.0.0" + }, + "peerDependencies": { + "@types/react": "^17.0.50 || ^18.0.21", + "@types/react-dom": "^17.0.17 || ^18.0.6", + "react": "^17.0.2 || ^18.0.0", + "react-dom": "^17.0.2 || ^18.0.0" } }, "node_modules/@astrojs/tailwind": { @@ -102,35 +146,26 @@ } }, "node_modules/@astrojs/telemetry": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/@astrojs/telemetry/-/telemetry-3.0.4.tgz", - "integrity": "sha512-A+0c7k/Xy293xx6odsYZuXiaHO0PL+bnDoXOc47sGDF5ffIKdKQGRPFl2NMlCF4L0NqN4Ynbgnaip+pPF0s7pQ==", + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@astrojs/telemetry/-/telemetry-3.1.0.tgz", + "integrity": "sha512-/ca/+D8MIKEC8/A9cSaPUqQNZm+Es/ZinRv0ZAzvu2ios7POQSsVD+VOj7/hypWNsNM3T7RpfgNq7H2TU1KEHA==", "dependencies": { - "ci-info": "^3.8.0", + "ci-info": "^4.0.0", "debug": "^4.3.4", "dlv": "^1.1.3", - "dset": "^3.1.2", + "dset": "^3.1.3", "is-docker": "^3.0.0", "is-wsl": "^3.0.0", "which-pm-runs": "^1.1.0" }, "engines": { - "node": ">=18.14.1" + "node": "^18.17.1 || ^20.3.0 || >=21.0.0" } }, - "node_modules/@astrojs/telemetry/node_modules/ci-info": { - "version": "3.9.0", - "resolved": "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz", - "integrity": "sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/sibiraj-s" - } - ], - "engines": { - "node": ">=8" - } + "node_modules/@astrojs/underscore-redirects": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/@astrojs/underscore-redirects/-/underscore-redirects-0.3.3.tgz", + "integrity": "sha512-qDAKhFO4M1KzP7mxoJfiehf8oyf3EB158MxAa6z10NeD2pR3o4K3LlOQI8CfJgXE+BDBQcnaLvVCg/Mz/Gkg4Q==" }, "node_modules/@babel/code-frame": { "version": "7.24.2", @@ -421,6 +456,34 @@ "@babel/core": "^7.0.0-0" } }, + "node_modules/@babel/plugin-transform-react-jsx-self": { + "version": "7.24.1", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-jsx-self/-/plugin-transform-react-jsx-self-7.24.1.tgz", + "integrity": "sha512-kDJgnPujTmAZ/9q2CN4m2/lRsUUPDvsG3+tSHWUJIzMGTt5U/b/fwWd3RO3n+5mjLrsBrVa5eKFRVSQbi3dF1w==", + "dependencies": { + "@babel/helper-plugin-utils": "^7.24.0" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, + "node_modules/@babel/plugin-transform-react-jsx-source": { + "version": "7.24.1", + "resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-jsx-source/-/plugin-transform-react-jsx-source-7.24.1.tgz", + "integrity": "sha512-1v202n7aUq4uXAieRTKcwPzNyphlCuqHHDcdSNc+vdhoTEZcFMh+L5yZuCmGaIO7bs1nJUNfHB89TZyoL48xNA==", + "dependencies": { + "@babel/helper-plugin-utils": "^7.24.0" + }, + "engines": { + "node": ">=6.9.0" + }, + "peerDependencies": { + "@babel/core": "^7.0.0-0" + } + }, "node_modules/@babel/template": { "version": "7.24.0", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.24.0.tgz", @@ -812,6 +875,11 @@ "node": ">=12" } }, + "node_modules/@fontsource/inter": { + "version": "5.0.18", + "resolved": "https://registry.npmjs.org/@fontsource/inter/-/inter-5.0.18.tgz", + "integrity": "sha512-YCsoYPTcs713sI7tLtxaPrIhXAXvEetGg5Ry02ivA8qUOb3fQHojbK/X9HLD5OOKvFUNR2Ynkwb1kR1hVKQHpw==" + }, "node_modules/@isaacs/cliui": { "version": "8.0.2", "resolved": "https://registry.npmjs.org/@isaacs/cliui/-/cliui-8.0.2.tgz", @@ -892,6 +960,45 @@ "@jridgewell/sourcemap-codec": "^1.4.14" } }, + "node_modules/@netlify/blobs": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/@netlify/blobs/-/blobs-7.3.0.tgz", + "integrity": "sha512-wN/kNTZo4xjlUM/C0WILOkJbe8p4AFquSGkZEIoIcgnsx5ikp2GyqGiq1WMLee7QdbnqeIV2g2hn/PjT324E5w==", + "engines": { + "node": "^14.16.0 || >=16.0.0" + } + }, + "node_modules/@netlify/functions": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/@netlify/functions/-/functions-2.6.0.tgz", + "integrity": "sha512-vU20tij0fb4nRGACqb+5SQvKd50JYyTyEhQetCMHdakcJFzjLDivvRR16u1G2Oy4A7xNAtGJF1uz8reeOtTVcQ==", + "dependencies": { + "@netlify/serverless-functions-api": "1.14.0" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@netlify/node-cookies": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/@netlify/node-cookies/-/node-cookies-0.1.0.tgz", + "integrity": "sha512-OAs1xG+FfLX0LoRASpqzVntVV/RpYkgpI0VrUnw2u0Q1qiZUzcPffxRK8HF3gc4GjuhG5ahOEMJ9bswBiZPq0g==", + "engines": { + "node": "^14.16.0 || >=16.0.0" + } + }, + "node_modules/@netlify/serverless-functions-api": { + "version": "1.14.0", + "resolved": "https://registry.npmjs.org/@netlify/serverless-functions-api/-/serverless-functions-api-1.14.0.tgz", + "integrity": "sha512-HUNETLNvNiC2J+SB/YuRwJA9+agPrc0azSoWVk8H85GC+YE114hcS5JW+dstpKwVerp2xILE3vNWN7IMXP5Q5Q==", + "dependencies": { + "@netlify/node-cookies": "^0.1.0", + "urlpattern-polyfill": "8.0.2" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -1155,6 +1262,17 @@ "@babel/types": "^7.20.7" } }, + "node_modules/@types/blobshape": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@types/blobshape/-/blobshape-1.0.3.tgz", + "integrity": "sha512-jbJZ9AIebK77LoeN8PjPyAs88GJCDKO+ZpWCJlLlV2FXkeYScQsvkZmlGHGvSRk/WFmUOdbGJ75umLATGX1lsw==", + "dev": true + }, + "node_modules/@types/cookie": { + "version": "0.5.4", + "resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.5.4.tgz", + "integrity": "sha512-7z/eR6O859gyWIAjuvBWFzNURmf2oPBmJlfVWkwehU5nzIyjwBsTh7WMmEEV4JFnHuQ3ex4oyTvfKzcyJVDBNA==" + }, "node_modules/@types/debug": { "version": "4.1.12", "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.12.tgz", @@ -1202,6 +1320,37 @@ "resolved": "https://registry.npmjs.org/@types/unist/-/unist-2.0.10.tgz", "integrity": "sha512-IfYcSBWE3hLpBg8+X2SEa8LVkJdJEkT2Ese2aaLs3ptGdVtABxndrMaxuFlQ1qdFf9Q5rDvDpxI3WwgvKFAsQA==" }, + "node_modules/@types/node": { + "version": "20.12.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", + "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", + "devOptional": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/@types/prop-types": { + "version": "15.7.12", + "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.12.tgz", + "integrity": "sha512-5zvhXYtRNRluoE/jAp4GVsSduVUzNWKkOZrCDBWYtE7biZywwdC2AcEzg+cSMLFRfVgeAFqpfNabiPjxFddV1Q==" + }, + "node_modules/@types/react": { + "version": "18.2.79", + "resolved": "https://registry.npmjs.org/@types/react/-/react-18.2.79.tgz", + "integrity": "sha512-RwGAGXPl9kSXwdNTafkOEuFrTBD5SA2B3iEB96xi8+xu5ddUa/cpvyVCSNn+asgLCTHkb5ZxN8gbuibYJi4s1w==", + "dependencies": { + "@types/prop-types": "*", + "csstype": "^3.0.2" + } + }, + "node_modules/@types/react-dom": { + "version": "18.2.25", + "resolved": "https://registry.npmjs.org/@types/react-dom/-/react-dom-18.2.25.tgz", + "integrity": "sha512-o/V48vf4MQh7juIKZU2QGDfli6p1+OOi5oXx36Hffpc9adsHeXjVp8rHuPkjd8VT8sOJ2Zp05HR7CdpGTIUFUA==", + "dependencies": { + "@types/react": "*" + } + }, "node_modules/@types/unist": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/@types/unist/-/unist-3.0.2.tgz", @@ -1212,6 +1361,24 @@ "resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz", "integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==" }, + "node_modules/@vitejs/plugin-react": { + "version": "4.2.1", + "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.2.1.tgz", + "integrity": "sha512-oojO9IDc4nCUUi8qIR11KoQm0XFFLIwsRBwHRR4d/88IWghn1y6ckz/bJ8GHDCsYEJee8mDzqtJxh15/cisJNQ==", + "dependencies": { + "@babel/core": "^7.23.5", + "@babel/plugin-transform-react-jsx-self": "^7.23.3", + "@babel/plugin-transform-react-jsx-source": "^7.23.3", + "@types/babel__core": "^7.20.5", + "react-refresh": "^0.14.0" + }, + "engines": { + "node": "^14.18.0 || >=16.0.0" + }, + "peerDependencies": { + "vite": "^4.2.0 || ^5.0.0" + } + }, "node_modules/acorn": { "version": "8.11.3", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.11.3.tgz", @@ -1335,14 +1502,14 @@ } }, "node_modules/astro": { - "version": "4.5.14", - "resolved": "https://registry.npmjs.org/astro/-/astro-4.5.14.tgz", - "integrity": "sha512-OXQ9Wbcn5e2eEPrNKJjGXeCWnKamNGyvSwORL0B8BB1fp3OO/EYo/RfeQGTR9kxGNxEpokoPZL9JNgFHoVK+wA==", + "version": "4.6.2", + "resolved": "https://registry.npmjs.org/astro/-/astro-4.6.2.tgz", + "integrity": "sha512-Kl+Wd7MJMQFnI3+V0JxF4HPbs8M67eltqQgtztReOwDLSl0VnOd39rM61W/3LEh10FZ0F13xrDgtdgfXzuLVbg==", "dependencies": { "@astrojs/compiler": "^2.7.1", "@astrojs/internal-helpers": "0.4.0", - "@astrojs/markdown-remark": "5.0.0", - "@astrojs/telemetry": "3.0.4", + "@astrojs/markdown-remark": "5.1.0", + "@astrojs/telemetry": "3.1.0", "@babel/core": "^7.24.3", "@babel/generator": "^7.23.3", "@babel/parser": "^7.23.3", @@ -1350,6 +1517,7 @@ "@babel/traverse": "^7.23.3", "@babel/types": "^7.23.3", "@types/babel__core": "^7.20.4", + "@types/cookie": "^0.5.4", "acorn": "^8.11.2", "aria-query": "^5.3.0", "axobject-query": "^4.0.0", @@ -1406,8 +1574,9 @@ "astro": "astro.js" }, "engines": { - "node": ">=18.14.1", - "npm": ">=6.14.0" + "node": "^18.17.1 || ^20.3.0 || >=21.0.0", + "npm": ">=9.6.5", + "pnpm": ">=7.1.0" }, "optionalDependencies": { "sharp": "^0.32.6" @@ -1555,6 +1724,11 @@ "readable-stream": "^3.4.0" } }, + "node_modules/blobshape": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/blobshape/-/blobshape-1.0.0.tgz", + "integrity": "sha512-GUD6tUIvj7k8zLSMtfdM1RyusWDjFqYL9g6J6QIgwxbVjL3b6mbRz5peerDc+6kbZX/14BSr3ed9sizrJKdkQQ==" + }, "node_modules/boxen": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/boxen/-/boxen-7.1.1.tgz", @@ -1978,6 +2152,11 @@ "node": ">=4" } }, + "node_modules/csstype": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz", + "integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==" + }, "node_modules/culori": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/culori/-/culori-3.3.0.tgz", @@ -1988,9 +2167,9 @@ } }, "node_modules/daisyui": { - "version": "4.9.0", - "resolved": "https://registry.npmjs.org/daisyui/-/daisyui-4.9.0.tgz", - "integrity": "sha512-9JsDx4E+30kPxThE+6yEwQokqg1957uwTx/skP2RE98fG6Ten6U+S9YXeQg1a3CI958aF5aOb0oEA+KZFfrZUA==", + "version": "4.10.2", + "resolved": "https://registry.npmjs.org/daisyui/-/daisyui-4.10.2.tgz", + "integrity": "sha512-eCWS1W/JPyxW9IvlgW5m0R6rp9ZhRsBTW37rvEUthckkjsV04u8XipV519OoccSA46ixhSJa3q7XLI1WUFtRCA==", "dev": true, "dependencies": { "css-selector-tokenizer": "^0.8", @@ -2691,9 +2870,9 @@ } }, "node_modules/hast-util-to-text": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/hast-util-to-text/-/hast-util-to-text-4.0.0.tgz", - "integrity": "sha512-EWiE1FSArNBPUo1cKWtzqgnuRQwEeQbQtnFJRYV1hb1BWDgrAlBU0ExptvZMM/KSA82cDpm2sFGf3Dmc5Mza3w==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/hast-util-to-text/-/hast-util-to-text-4.0.1.tgz", + "integrity": "sha512-RHL7Vo2n06ZocCFWqmbyhZ1pCYX/mSKdywt9YD5U6Hquu5syV+dImCXFKLFt02JoK5QxkQFS0PoVdFdPXuPffQ==", "dependencies": { "@types/hast": "^3.0.0", "@types/unist": "^3.0.0", @@ -3164,6 +3343,17 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "dependencies": { + "js-tokens": "^3.0.0 || ^4.0.0" + }, + "bin": { + "loose-envify": "cli.js" + } + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -3204,9 +3394,9 @@ } }, "node_modules/marked-shiki": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/marked-shiki/-/marked-shiki-1.0.1.tgz", - "integrity": "sha512-fVz5Gq4ZBEptQH4kZTpNfILLxnBONxrXdpjko7rfXm68psltGaZL5VcR1xNNbF/ZN7W4u7ba5fxM+bzzEZoPlQ==", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/marked-shiki/-/marked-shiki-1.1.0.tgz", + "integrity": "sha512-PNiC0X4rYnzF63T6CDscwTxQB8l5Aur9XR7MRNBi/Y9nSxmPxasWT+V5OVam8Pj+NycWjzG6idO7fbc2bDD16w==", "peerDependencies": { "marked": ">=7.0.0", "shiki": ">=1.0.0" @@ -4851,6 +5041,37 @@ "rc": "cli.js" } }, + "node_modules/react": { + "version": "18.2.0", + "resolved": "https://registry.npmjs.org/react/-/react-18.2.0.tgz", + "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==", + "dependencies": { + "loose-envify": "^1.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/react-dom": { + "version": "18.2.0", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.2.0.tgz", + "integrity": "sha512-6IMTriUmvsjHUjNtEDudZfuDQUoWXVxKHhlEGSk81n4YFS+r/Kl99wXiwlVXtPBtJenozv2P+hxDsw9eA7Xo6g==", + "dependencies": { + "loose-envify": "^1.1.0", + "scheduler": "^0.23.0" + }, + "peerDependencies": { + "react": "^18.2.0" + } + }, + "node_modules/react-refresh": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/react-refresh/-/react-refresh-0.14.0.tgz", + "integrity": "sha512-wViHqhAd8OHeLS/IRMJjTSDHF3U9eWi62F/MledQGPdJGDhodXJ9PBLNGr6WWL7qlH12Mt3TyTpbS+hGXMjCzQ==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/read-cache": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", @@ -5514,6 +5735,14 @@ } ] }, + "node_modules/scheduler": { + "version": "0.23.0", + "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.0.tgz", + "integrity": "sha512-CtuThmgHNg7zIZWAXi3AsyIzA3n4xx7aNyjwC2VJldO2LMVDhFK+63xGqq6CsJH4rTAt6/M+N4GhZiDYPx9eUw==", + "dependencies": { + "loose-envify": "^1.1.0" + } + }, "node_modules/section-matter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/section-matter/-/section-matter-1.0.0.tgz", @@ -6096,6 +6325,17 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/ultrahtml": { + "version": "1.5.3", + "resolved": "https://registry.npmjs.org/ultrahtml/-/ultrahtml-1.5.3.tgz", + "integrity": "sha512-GykOvZwgDWZlTQMtp5jrD4BVL+gNn2NVlVafjcFUJ7taY20tqYdwdoWBFy6GBJsNTZe1GkGPkSl5knQAjtgceg==" + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "devOptional": true + }, "node_modules/unherit": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/unherit/-/unherit-3.0.1.tgz", @@ -6123,6 +6363,14 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/unique-names-generator": { + "version": "4.7.1", + "resolved": "https://registry.npmjs.org/unique-names-generator/-/unique-names-generator-4.7.1.tgz", + "integrity": "sha512-lMx9dX+KRmG8sq6gulYYpKWZc9RlGsgBR6aoO8Qsm3qvkSJ+3rAymr+TnV8EDMrIrwuFJ4kruzMWM/OpYzPoow==", + "engines": { + "node": ">=8" + } + }, "node_modules/unist-util-find-after": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/unist-util-find-after/-/unist-util-find-after-5.0.0.tgz", @@ -6276,6 +6524,11 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/urlpattern-polyfill": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/urlpattern-polyfill/-/urlpattern-polyfill-8.0.2.tgz", + "integrity": "sha512-Qp95D4TPJl1kC9SKigDcqgyM2VDVO4RiJc2d4qe5GrYm+zbIQCWWKAFaJNQ4BhdFeDGwBmAxqJBwWSJDb9T3BQ==" + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/package.json b/package.json index a089f6e..eb7b56a 100644 --- a/package.json +++ b/package.json @@ -10,13 +10,26 @@ "astro": "astro" }, "dependencies": { + "@astrojs/netlify": "^5.2.0", + "@astrojs/react": "^3.3.0", "@astrojs/tailwind": "^5.1.0", - "astro": "^4.5.14", + "@fontsource/inter": "^5.0.18", + "@netlify/blobs": "^7.3.0", + "@netlify/functions": "^2.6.0", + "@types/react": "^18.2.79", + "@types/react-dom": "^18.2.25", + "astro": "^4.6.2", + "blobshape": "^1.0.0", "marked": "^12.0.1", - "marked-shiki": "^1.0.1", - "tailwindcss": "^3.4.3" + "marked-shiki": "^1.1.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "tailwindcss": "^3.4.3", + "unique-names-generator": "^4.7.1" }, "devDependencies": { - "daisyui": "^4.9.0" + "@types/blobshape": "^1.0.3", + "@types/node": "^20.12.7", + "daisyui": "^4.10.2" } } diff --git a/public/images/noise.svg b/public/images/noise.svg deleted file mode 100644 index 17bddaf..0000000 --- a/public/images/noise.svg +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/public/netlify-logo.svg b/public/netlify-logo.svg index 9aa5a20..0183451 100644 --- a/public/netlify-logo.svg +++ b/public/netlify-logo.svg @@ -1,11 +1,11 @@ - - - - - - + + + + + + diff --git a/src/components/ContextAlert.astro b/src/components/ContextAlert.astro index ed54565..61273c3 100644 --- a/src/components/ContextAlert.astro +++ b/src/components/ContextAlert.astro @@ -5,7 +5,7 @@ import { getNetlifyContext } from '../utils'; const noNetlifyContextAlert = ` For full functionality, either run this site locally via \`netlify dev\` -([see docs](https://docs.netlify.com/cli/local-development/")) or deploy it to Netlify. +([see docs](https://docs.netlify.com/cli/local-development/)) or deploy it to Netlify. `; const { addedChecksFunction, class: className } = Astro.props; diff --git a/src/components/Footer.astro b/src/components/Footer.astro index 9eb5917..fbf1e8f 100644 --- a/src/components/Footer.astro +++ b/src/components/Footer.astro @@ -4,6 +4,6 @@

- Astro.js on Netlify + Astro on Netlify

diff --git a/src/components/Markdown.astro b/src/components/Markdown.astro index d9881a8..e8996c5 100644 --- a/src/components/Markdown.astro +++ b/src/components/Markdown.astro @@ -16,7 +16,7 @@ const { content, class: className } = Astro.props; // with the loaded themes and languages. const highlighter = await getHighlighter({ langs: ['jsx', 'js'], - themes: ['poimandres'] + themes: ['min-dark'] }); const html = await new Marked() .use( @@ -24,7 +24,7 @@ const html = await new Marked() highlight(code, lang, props) { return highlighter.codeToHtml(code, { lang, - theme: 'poimandres' + theme: 'min-dark' }); } }) diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro index 1d5bea1..5b37dd5 100644 --- a/src/layouts/Layout.astro +++ b/src/layouts/Layout.astro @@ -1,5 +1,7 @@ --- import '../../src/styles/globals.css'; +import '@fontsource/inter/400.css'; +import '@fontsource/inter/800.css'; import Header from '../components/Header.astro'; import Footer from '../components/Footer.astro'; @@ -19,7 +21,7 @@ const { title } = Astro.props; - +
diff --git a/src/pages/api/blob.ts b/src/pages/api/blob.ts new file mode 100644 index 0000000..4ff37c4 --- /dev/null +++ b/src/pages/api/blob.ts @@ -0,0 +1,20 @@ +import type { APIRoute } from 'astro'; +import { getStore } from '@netlify/blobs'; + +export const prerender = false; + +export const GET: APIRoute = async (context) => { + const urlParams = new URL(context.url); + const key = urlParams.searchParams.get('key'); + if (!key) { + return new Response('Bad Request', { status: 400 }); + } + + const blobStore = getStore('shapes'); + const blob = await blobStore.get(key, { type: 'json' }); + return new Response( + JSON.stringify({ + blob + }) + ); +}; diff --git a/src/pages/api/blobs.ts b/src/pages/api/blobs.ts new file mode 100644 index 0000000..d11aa58 --- /dev/null +++ b/src/pages/api/blobs.ts @@ -0,0 +1,27 @@ +import type { APIRoute } from 'astro'; +import { getStore } from '@netlify/blobs'; + +export const prerender = false; + +export const POST: APIRoute = async ({ request }) => { + const parameters = await request.json(); + const blobStore = getStore('shapes'); + const key = parameters.name; + await blobStore.setJSON(key, parameters); + return new Response( + JSON.stringify({ + message: `Stored shape "${key}"` + }) + ); +}; + +export const GET: APIRoute = async ({ request }) => { + const blobStore = getStore({ name: 'shapes', consistency: 'strong' }); + const data = await blobStore.list(); + const keys = data.blobs.map(({ key }) => key); + return new Response( + JSON.stringify({ + keys + }) + ); +}; diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts new file mode 100644 index 0000000..0d953a3 --- /dev/null +++ b/src/pages/api/revalidate.ts @@ -0,0 +1,19 @@ +import type { APIRoute } from 'astro'; +import { purgeCache } from '@netlify/functions'; + +export const prerender = false; + +export const POST: APIRoute = async ({ request }) => { + const { tags } = await request.json(); + + if (!Array.isArray(tags)) { + return new Response(`Bad Request: expected tags attribute with array of strings in the body, got ${typeof tags}`, { status: 400 }); + } + + await purgeCache({ tags }); + return new Response( + JSON.stringify({ + invalidated: tags + }) + ); +}; diff --git a/src/pages/blobs.astro b/src/pages/blobs.astro deleted file mode 100644 index 2884c4a..0000000 --- a/src/pages/blobs.astro +++ /dev/null @@ -1,8 +0,0 @@ ---- -import Layout from "../layouts/Layout.astro"; ---- - - -

Blobs x Blobs

-

Get started with Astro.js and Netlify in seconds.

-
diff --git a/src/pages/blobs/_components/NewShape.tsx b/src/pages/blobs/_components/NewShape.tsx new file mode 100644 index 0000000..05f9e6f --- /dev/null +++ b/src/pages/blobs/_components/NewShape.tsx @@ -0,0 +1,60 @@ +import { useState, useEffect } from 'react'; +import type { Dispatch, SetStateAction } from 'react'; +import ShapePreview from './ShapePreview.tsx'; +import { generateBlob } from '../../../utils'; +import type { BlobProps } from '../../../types.ts'; + +interface Props { + setLastMutationTime?: Dispatch>; +} + +export default function NewShape(props: Props) { + const { setLastMutationTime } = props; + const [blobData, setBlobData] = useState(); + const [wasUploaded, setWasUploaded] = useState(false); + + const randomizeBlob = () => { + setBlobData(generateBlob()); + setWasUploaded(false); + }; + + const uploadBlob = async () => { + const response = await fetch('/api/blobs', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(blobData.parameters) + }); + const data = await response.json(); + if (data.message) { + console.log(data.message); + } + setWasUploaded(true); + setLastMutationTime(Date.now()); + }; + + useEffect(() => { + if (!blobData) { + randomizeBlob(); + } + }, [blobData]); + + return ( + <> +

New Random Shape

+
+
+ {blobData && {blobData.parameters?.name}} +
+
{blobData && }
+
+
+ + +
+ + ); +} diff --git a/src/pages/blobs/_components/ShapeEditor.tsx b/src/pages/blobs/_components/ShapeEditor.tsx new file mode 100644 index 0000000..9a339cc --- /dev/null +++ b/src/pages/blobs/_components/ShapeEditor.tsx @@ -0,0 +1,18 @@ +import { useState } from 'react'; +import NewShape from './NewShape.tsx'; +import StoredShapes from './StoredShapes.tsx'; + +export default function ShapeEditor() { + const [lastMutationTime, setLastMutationTime] = useState(null); + + return ( +
+
+ +
+
+ +
+
+ ); +} diff --git a/src/pages/blobs/_components/ShapePreview.tsx b/src/pages/blobs/_components/ShapePreview.tsx new file mode 100644 index 0000000..a00d2bc --- /dev/null +++ b/src/pages/blobs/_components/ShapePreview.tsx @@ -0,0 +1,19 @@ +import { randomInt } from '../../../utils.ts'; +import type { BlobProps } from '../../../types.ts'; + +export default function ShapePreview(props: BlobProps) { + const { svgPath, parameters } = props; + const gradientId = `gradient-${randomInt(10_000_000, 100_000_000)}`; + + return ( + + + + + + + + + + ); +} diff --git a/src/pages/blobs/_components/StoredShapes.tsx b/src/pages/blobs/_components/StoredShapes.tsx new file mode 100644 index 0000000..e870ef5 --- /dev/null +++ b/src/pages/blobs/_components/StoredShapes.tsx @@ -0,0 +1,78 @@ +import { useState, useEffect } from 'react'; +import ShapePreview from './ShapePreview.tsx'; +import { generateBlob } from '../../../utils'; +import type { BlobProps } from '../../../types.ts'; + +interface Props { + lastMutationTime: number; +} + +export default function StoredShapes(props: Props) { + const { lastMutationTime } = props; + const [keys, setKeys] = useState([]); + const [selectedKey, setSelectedKey] = useState(null); + const [previewData, setPreviewData] = useState(null); + + const getBlobKeyList = async () => { + console.log('Fetching keys...'); + const response = await fetch('/api/blobs', { + method: 'GET', + headers: { 'Content-Type': 'application/json' } + }); + const data = await response.json(); + if (data.keys) { + setKeys(data.keys); + } + }; + + const getBlobByKey = async (key: string) => { + setSelectedKey(key); + const params = new URLSearchParams({ key }); + const response = await fetch(`/api/blob/?${params}`, { + method: 'GET' + }); + const data = await response.json(); + if (data.blob) { + setPreviewData(generateBlob(data.blob)); + } + }; + + useEffect(() => { + getBlobKeyList(); + }, [lastMutationTime]); + + return ( + <> +

Objects in Blob Store

+
+
+ {keys?.length ? ( +
+ {keys.map((keyName) => ( + + ))} +
+ ) : ( + Please upload some shapes! + )} +
+ {previewData && ( +
+ +
+ )} +
+ + ); +} diff --git a/src/pages/blobs/index.astro b/src/pages/blobs/index.astro new file mode 100644 index 0000000..5630d6b --- /dev/null +++ b/src/pages/blobs/index.astro @@ -0,0 +1,18 @@ +--- +import Layout from '../../layouts/Layout.astro'; +import Markdown from '../../components/Markdown.astro'; +import ShapeEditor from './_components/ShapeEditor.tsx'; + +const explainer = ` +[Netlify Blobs](https://docs.netlify.com/blobs/overview/) provides an object store for any kind of data, be it JSON, binary, +or [really](https://mk.gg/projects/chalkstream) anything else ([really!](https://mk.gg/projects/turbofan)). In this example, the blob store is used to **hold the data of user-generated random blobby shapes**. + +Below is an Astro island with a React component for editing, uploading and viewing blobs to the blob store 🙂. +`; +--- + + +

Blobs x Blobs

+ + +
diff --git a/src/pages/edge/index.astro b/src/pages/edge/index.astro index f49f3a9..8bf4a85 100644 --- a/src/pages/edge/index.astro +++ b/src/pages/edge/index.astro @@ -9,7 +9,7 @@ This page is using a [Netlify Edge Function](https://docs.netlify.com/edge-funct For it to be invoked, please either run this site locally with \`netlify dev\` or deploy it to Netlify. -Edge Functions are framework-agnostic, but are also used behind the scenes to run Next.js Middleware on Netlify. +Edge Functions are framework-agnostic, but are also used behind the scenes to run Astro Middleware on Netlify. There are advatanges to using Edge Functions directly, such as the ability to access & transform the response body. [See more examples](https://edge-functions-examples.netlify.app) diff --git a/src/pages/image-cdn.astro b/src/pages/image-cdn.astro index 8847536..dbb8822 100644 --- a/src/pages/image-cdn.astro +++ b/src/pages/image-cdn.astro @@ -10,18 +10,25 @@ const sampleImage = '/images/corgi.jpg'; const ctx = getNetlifyContext(); const forceWebP = ctx === 'dev'; -const sampleImageNetlify = `/.netlify/images?url=${sampleImage}${forceWebP ? '&fm=webp' : ''}`; +const sampleImageSrcSet = [640, 1280, 2048] + .map((size) => { + return `/.netlify/images?url=${sampleImage}&w=${size}${forceWebP ? '&fm=webp' : ''} ${size}w`; + }) + .join(', '); +const sizes = '(max-width: 1024px) 100vw, 1024px'; const astroImageSnippet = ` -## Using built-in Astro <Image /> component +## Using the built-in Astro <Image /> component -When running on Netlify, \`next/image\` is automatically set-up to use Netlify Image CDN for optimized images. +Astro's \`Image\` component will automatically use Netlify Image CDN to serve optimized images. ~~~jsx -import Image from 'next/image'; - -// In your component -Corgi +--- +import { Image } from 'astro:assets'; +import corgi from '../assets/corgi.jpg'; +--- +// Later in your markup... +Corgi ~~~ `; @@ -29,7 +36,7 @@ const originalVsCdnSnippet = ` ## Original vs. optimized image: can you tell the difference? In the code below, a regular \`\` tag is used in both cases for a framework-agnostic example. -Other than using \`next/image\` or rolling your own \`\` tags, you can also use the excellent [unpic-img](https://unpic.pics/). +Note that aside from Astro's \`Image\` or rolling your own \`\` tags, you can also use the excellent [unpic-img](https://unpic.pics/) package. ~~~jsx // <== On the left, the original image @@ -37,8 +44,8 @@ Other than using \`next/image\` or rolling your own \`\` tags, you can also // ==> On the right, explicitly using Netlify Image CDN endpoint for a responsive image Corgi ~~~ @@ -53,11 +60,11 @@ detection, so format is set to WebP.
- { - return ctx === "dev" ? devModeWarning : null; - } - } /> + { + return ctx === 'dev' ? devModeWarning : null; + }} + />

Image CDN

@@ -77,7 +84,7 @@ detection, so format is set to WebP.
- Corgi + Corgi
diff --git a/src/pages/index.astro b/src/pages/index.astro index 8955471..6e73e14 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,13 +1,21 @@ --- import Layout from '../layouts/Layout.astro'; -import Card from '../components/Card.astro'; +import ContextAlert from '../components/ContextAlert.astro'; +import Markdown from '../components/Markdown.astro'; + +const explainer = ` +An Astro website can go way beyond static pages - on the right platform. + +Netlify supports not only [Streaming SSR](https://docs.astro.build/en/guides/server-side-rendering/) and fast [Edge Middleware](https://docs.astro.build/en/guides/middleware/), but also [on-demand revalidation](https://www.netlify.com/blog/cache-tags-and-purge-api-on-netlify/) and [stale-while-revalidate](https://www.netlify.com/blog/swr-and-fine-grained-cache-control/). +Any page or data can be rebuilt only when needed, without site visitors ever getting a performance hit. +`; --- -

Netlify Platform Starter - Astro.js

-

Get started with Astro.js and Netlify in seconds.

+ +

Netlify Platform Starter for Astro

+

Read the Docs

-
diff --git a/src/pages/revalidation.astro b/src/pages/revalidation.astro new file mode 100644 index 0000000..f7d4f6e --- /dev/null +++ b/src/pages/revalidation.astro @@ -0,0 +1,61 @@ +--- +import Layout from '../layouts/Layout.astro'; +import ContextAlert from '../components/ContextAlert.astro'; +import Markdown from '../components/Markdown.astro'; +import { cacheHeaders, randomInt } from '../utils'; + +export const prerender = false; + +const tags = ['/revalidation', 'cats-related', 'all-pets-related']; +const headers = cacheHeaders(365, tags); + +Object.entries(headers).map(([k, v]) => { + Astro.response.headers.set(k, v); +}); + +const explainer = ` +This is a server-rendered page, last created at \`${new Date().toUTCString()}\` with a few extra response headers: + +~~~js +${JSON.stringify(headers, null, 2)} +~~~ + +Meaning: **for browsers,** this is a non-cacheable page. At the **CDN level,** it *is* cacheable for up to a year (and Netlify also automatically busts the cache on new deployments, by default). + +But there's something more: the \`Cache-Tag\` header, in tandem with the [purge API](https://docs.netlify.com/platform/caching/#on-demand-invalidation), allows a developer to invalidate at will any +pieces of content (pages, JSON responses, or whatnot) based on the tags associated with them. Tags are completely up to you to decide on, and allow for very powerful patterns. + +In real-world applications, you may want to use [stale-while-revalidate](https://docs.netlify.com/platform/caching/#stale-while-revalidate-directive) as well, so that end-users don't experience +a performance hit as pages are being rebuilt. + +Use the button below to invlidate a random tag out of this page's tags, then refresh the page. +`; + +const exampleTag = tags[randomInt(0, tags.length - 1)]; +--- + + +

Revalidating Server Content

+ + + +
+ + diff --git a/src/styles/globals.css b/src/styles/globals.css index dabf3e4..2573355 100644 --- a/src/styles/globals.css +++ b/src/styles/globals.css @@ -1,5 +1,3 @@ -@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;800&display=swap'); - @tailwind base; @tailwind components; @tailwind utilities; @@ -10,7 +8,7 @@ } h2 { - @apply text-2xl font-bold sm:text-3xl + @apply text-2xl font-bold sm:text-3xl; } a { @@ -22,10 +20,12 @@ } :not(pre) > code { - @apply px-1 py-0.5 font-mono rounded bg-neutral-900 text-yellow-200; + @apply px-1 py-0.5 font-mono rounded bg-[#1f1f1f] text-[#F67280]; font-size: 0.9em; } +} +@layer components { .markdown > h1, .markdown > h2 { @apply mt-12 mb-6; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..8c8daa8 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,13 @@ +export type BlobParameterProps = { + seed: number; + size: number; + edges: number; + growth: number; + name: string; + colors: string[]; +}; + +export type BlobProps = { + svgPath: string; + parameters: BlobParameterProps; +}; diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index d2cc47b..0000000 --- a/src/utils.js +++ /dev/null @@ -1,4 +0,0 @@ -// Note: this only works on the server side -export function getNetlifyContext() { - return process.env.CONTEXT; -} \ No newline at end of file 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; +} diff --git a/tailwind.config.mjs b/tailwind.config.mjs index 87a222b..7f520ee 100644 --- a/tailwind.config.mjs +++ b/tailwind.config.mjs @@ -1,13 +1,17 @@ /** @type {import('tailwindcss').Config} */ const colors = require('tailwindcss/colors'); const defaultTheme = require('tailwindcss/defaultTheme'); +const fs = require('fs'); + +const noiseBitmap = fs.readFileSync('./public/images/noise.png', { encoding: 'base64' }); +const noiseDataUri = 'data:image/png;base64,' + noiseBitmap; export default { - content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'], + content: ['./src/**/*.{astro,html,js,jsx,md,mdx,svelte,ts,tsx,vue}'], theme: { extend: { backgroundImage: { - 'grid-pattern': "linear-gradient(to bottom, theme('colors.neutral.950 / 0%'), theme('colors.neutral.950 / 100%')), url('/images/noise.png')" + 'grid-pattern': `linear-gradient(to bottom, theme('colors.neutral.950 / 0%'), theme('colors.neutral.950 / 100%')), url('${noiseDataUri}')` }, colors: { neutral: colors.neutral @@ -22,14 +26,14 @@ export default { { lofi: { ...require('daisyui/src/theming/themes')['lofi'], - primary: '#fbb13d', - 'primary-content': '#171717', - secondary: '#0f766e', - info: '#fbb13d', - 'info-content': '#171717', + primary: '#F67280', + 'primary-content': '#1f1f1f', + secondary: '#C06C84', + info: '#F67280', + 'info-content': '#1f1f1f' } } ] }, - plugins: [require('daisyui')], -} + plugins: [require('daisyui')] +}; diff --git a/tsconfig.json b/tsconfig.json index d78f81e..7fb90fa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,3 +1,7 @@ { - "extends": "astro/tsconfigs/base" -} + "extends": "astro/tsconfigs/base", + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "react" + } +} \ No newline at end of file -- cgit v1.2.3