From 90cc880a01ceab5b1a1920471c4db9262be179ac Mon Sep 17 00:00:00 2001 From: giuem Date: Wed, 11 May 2022 23:57:01 +0800 Subject: [PATCH] feat: vercel json generator --- apps/www.giuem.com/vercel.json | 201 ------------------ .../gatsby-plugin-vercel-json/gatsby-node.ts | 72 ++++++- 2 files changed, 63 insertions(+), 210 deletions(-) delete mode 100644 apps/www.giuem.com/vercel.json diff --git a/apps/www.giuem.com/vercel.json b/apps/www.giuem.com/vercel.json deleted file mode 100644 index e4c8d23f..00000000 --- a/apps/www.giuem.com/vercel.json +++ /dev/null @@ -1,201 +0,0 @@ -{ - "$schema": "https://openapi.vercel.sh/vercel.json", - "trailingSlash": false, - "cleanUrls": true, - "headers": [ - { - "source": "/(.*)", - "headers": [ - { - "key": "X-Content-Type-Options", - "value": "nosniff" - }, - { - "key": "X-Frame-Options", - "value": "DENY" - }, - { - "key": "X-XSS-Protection", - "value": "1; mode=block" - } - ] - } - ], - "redirects": [ - { - "source": "/recent-updates-of-my-blog-2021", - "destination": "/blog/recent-updates-of-my-blog-2021", - "permanent": true - }, - { - "source": "/xiaomi-ax9000", - "destination": "/blog/xiaomi-ax9000", - "permanent": true - }, - { - "source": "/dark-mode", - "destination": "/blog/dark-mode", - "permanent": true - }, - { - "source": "/how-fast-is-my-blog", - "destination": "/blog/how-fast-is-my-blog", - "permanent": true - }, - { - "source": "/a-brief-introduction-to-fluid-typography", - "destination": "/blog/a-brief-introduction-to-fluid-typography", - "permanent": true - }, - { - "source": "/j1900-nas", - "destination": "/blog/j1900-nas", - "permanent": true - }, - { - "source": "/hus728t8tale6l4-review", - "destination": "/blog/hus728t8tale6l4-review", - "permanent": true - }, - { - "source": "/tls-1-3-overview", - "destination": "/blog/tls-1-3-overview", - "permanent": true - }, - { - "source": "/a-new-beginning-to-blog", - "destination": "/blog/a-new-beginning-to-blog", - "permanent": true - }, - { - "source": "/extend-disk-at-66to", - "destination": "/blog/extend-disk-at-66to", - "permanent": true - }, - { - "source": "/computer-diy-record", - "destination": "/blog/computer-diy-record", - "permanent": true - }, - { - "source": "/my-first-participation-in-cumcm", - "destination": "/blog/my-first-participation-in-cumcm", - "permanent": true - }, - { - "source": "/setup-ssh-2fa-via-authy", - "destination": "/blog/setup-ssh-2fa-via-authy", - "permanent": true - }, - { - "source": "/caddy-tutorial-for-debian", - "destination": "/blog/caddy-tutorial-for-debian", - "permanent": true - }, - { - "source": "/google-analytics-loading-optimize", - "destination": "/blog/google-analytics-loading-optimize", - "permanent": true - }, - { - "source": "/deploy-via-travis-ci", - "destination": "/blog/deploy-via-travis-ci", - "permanent": true - }, - { - "source": "/set-up-my-own-git-server-with-gogs-and-docker", - "destination": "/blog/set-up-my-own-git-server-with-gogs-and-docker", - "permanent": true - }, - { - "source": "/setup-l4d2-server-for-linux", - "destination": "/blog/setup-l4d2-server-for-linux", - "permanent": true - }, - { - "source": "/a-qiandao-site-and-ssl-config", - "destination": "/blog/a-qiandao-site-and-ssl-config", - "permanent": true - }, - { - "source": "/webfontloader", - "destination": "/blog/webfontloader", - "permanent": true - }, - { - "source": "/result-of-gankao-came-out", - "destination": "/blog/result-of-gankao-came-out", - "permanent": true - }, - { - "source": "/baidu-pan-customize-share-password-by-javascript", - "destination": "/blog/baidu-pan-customize-share-password-by-javascript", - "permanent": true - }, - { - "source": "/use-basketjs", - "destination": "/blog/use-basketjs", - "permanent": true - }, - { - "source": "/how-to-prevent-wormhole", - "destination": "/blog/how-to-prevent-wormhole", - "permanent": true - }, - { - "source": "/taste-wormhole", - "destination": "/blog/taste-wormhole", - "permanent": true - }, - { - "source": "/fuck-messy-thai", - "destination": "/blog/fuck-messy-thai", - "permanent": true - }, - { - "source": "/css-arrow", - "destination": "/blog/css-arrow", - "permanent": true - }, - { - "source": "/cloudxns-dns", - "destination": "/blog/cloudxns-dns", - "permanent": true - }, - { - "source": "/php-bug-cve-2015-4024", - "destination": "/blog/php-bug-cve-2015-4024", - "permanent": true - }, - { - "source": "/php-hack-equality-sign", - "destination": "/blog/php-hack-equality-sign", - "permanent": true - }, - { - "source": "/raspberry-pi-download", - "destination": "/blog/raspberry-pi-download", - "permanent": true - }, - { - "source": "/php-check-if-a-string-is-a-valid-email", - "destination": "/blog/php-check-if-a-string-is-a-valid-email", - "permanent": true - }, - { - "source": "/bing-image-get-php", - "destination": "/blog/bing-image-get-php", - "permanent": true - }, - { - "source": "/js-get-ip", - "destination": "/blog/js-get-ip", - "permanent": true - }, - { - "source": "/html-head", - "destination": "/blog/html-head", - "permanent": true - } - ] -} diff --git a/packages/gatsby-plugin-vercel-json/gatsby-node.ts b/packages/gatsby-plugin-vercel-json/gatsby-node.ts index 5a041951..e519b310 100644 --- a/packages/gatsby-plugin-vercel-json/gatsby-node.ts +++ b/packages/gatsby-plugin-vercel-json/gatsby-node.ts @@ -1,7 +1,7 @@ import fs from 'fs'; import { resolve } from 'path'; -import type { GatsbyNode, Actions } from 'gatsby'; +import type { GatsbyNode, Actions, Page } from 'gatsby'; type VercelRedirect = { source: string; @@ -9,16 +9,58 @@ type VercelRedirect = { permanent?: boolean; }; +type VercelHeaders = { + source: string; + headers: { + key: string; + value: string; + }[]; +}; + type GatsbyRedirect = Parameters[0]; type GatsbyState = { redirects: GatsbyRedirect[]; - // pages: Map; - // program: { directory: string }; + pages: Map; + program: { directory: string }; +}; + +type PluginOptions = { + cleanUrls?: boolean; + headers: VercelHeaders[]; }; -export const onPostBuild: GatsbyNode['onPostBuild'] = async ({ store }) => { - const { redirects }: GatsbyState = store.getState(); +export const pluginOptionsSchema: GatsbyNode['pluginOptionsSchema'] = ({ + Joi, +}) => + Joi.object({ + cleanUrls: Joi.boolean().default(false), + headers: Joi.array().items( + Joi.object({ + source: Joi.string().required(), + headers: Joi.array() + .items( + Joi.object({ + key: Joi.string().required(), + value: Joi.string().required(), + }) + ) + .required(), + }) + ), + }); + +export const onPostBuild: GatsbyNode['onPostBuild'] = async ( + { store, reporter }, + pluginOptions +) => { + console.log(pluginOptions); + const { cleanUrls, headers } = pluginOptions as unknown as PluginOptions; + const { redirects, program }: GatsbyState = store.getState(); + + const publicDir = resolve(program.directory, 'public'); + const vercelJsonPath = resolve(publicDir, 'vercel.json'); + throw 1; const vercelRedirects: VercelRedirect[] = []; redirects.forEach((redirect) => { @@ -29,11 +71,23 @@ export const onPostBuild: GatsbyNode['onPostBuild'] = async ({ store }) => { }); }); + if (redirects.length > 1024) { + reporter.panicOnBuild( + `Vercel supports redirects up to 1024, but got ${redirects.length}` + ); + } + // eslint-disable-next-line @typescript-eslint/no-var-requires - const vercelJson = require('./vercel.json'); - vercelJson.redirects = vercelRedirects; - await fs.promises.writeFile( - resolve('vercel.json'), + const vercelJson = { + cleanUrls, + headers, + // @todo get from config + trailingSlash: false, + redirects: vercelRedirects, + }; + + return fs.promises.writeFile( + vercelJsonPath, JSON.stringify(vercelJson, null, 2), 'utf-8' );