From d79ab150a19916bd62ba31292426886fe8c564be Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Tue, 19 Dec 2023 10:46:31 -0600 Subject: [PATCH 1/2] Add Vercal preview configuration --- .gitignore | 1 + middleware.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ vercel.json | 9 +++++++++ 3 files changed, 56 insertions(+) create mode 100644 middleware.js create mode 100644 vercel.json diff --git a/.gitignore b/.gitignore index 8a59dac..990c2d1 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ node_modules ._* .cache .DS_Store +.vercel diff --git a/middleware.js b/middleware.js new file mode 100644 index 0000000..4035764 --- /dev/null +++ b/middleware.js @@ -0,0 +1,46 @@ +const rewriteRules = [ + // Replace CDN URLs with local paths + { + match : /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md' + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g, + replace: '/dist/js/docsify-themeable.min.js' + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g, + replace: '/dist/' + } +]; + +// Exports +// ============================================================================= +export const config = { + matcher: ['/preview/(index.html)?'], +}; + +// Serve virtual /preview/index.html +// Note: See vercel.json for preview routing configuration +// 1. Fetch index.html from /docs/ directory +// 2. Replace CDN URLs with local paths (see rewriteRules above) +// 3. Return preview HTML +export default async function middleware(request) { + const { origin } = new URL(request.url); + const indexURL = `${origin}/docs/index.html`; + const indexHTML = await fetch(indexURL).then((res) => res.text()); + const previewHTML = rewriteRules.reduce((html, rule) => html.replace(rule.match, rule.replace), indexHTML); + + return new Response(previewHTML, { + status: 200, + headers: { 'content-type': 'text/html' }, + }); +} diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000..d8b5b82 --- /dev/null +++ b/vercel.json @@ -0,0 +1,9 @@ +{ + "redirects": [ + { "source": "/", "destination": "/preview/" } + ], + "rewrites": [ + { "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" }, + { "source": "/preview/:path*", "destination": "/docs/:path*" } + ] +} \ No newline at end of file From 3e70e099db7c9aa319afd022ec6679446326888b Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Tue, 19 Dec 2023 12:21:53 -0600 Subject: [PATCH 2/2] Add shared local and Vercel preview config --- middleware.js | 26 ++--------------------- preview.config.cjs | 25 ++++++++++++++++++++++ server.cjs | 52 ++++++++++++++++------------------------------ 3 files changed, 45 insertions(+), 58 deletions(-) create mode 100644 preview.config.cjs diff --git a/middleware.js b/middleware.js index 4035764..3646102 100644 --- a/middleware.js +++ b/middleware.js @@ -1,26 +1,4 @@ -const rewriteRules = [ - // Replace CDN URLs with local paths - { - match : /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md' - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g, - replace: '/dist/js/docsify-themeable.min.js' - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g, - replace: '/dist/' - } -]; +import { rewriteRules } from './preview.config.cjs'; // Exports // ============================================================================= @@ -31,7 +9,7 @@ export const config = { // Serve virtual /preview/index.html // Note: See vercel.json for preview routing configuration // 1. Fetch index.html from /docs/ directory -// 2. Replace CDN URLs with local paths (see rewriteRules above) +// 2. Replace CDN URLs with local paths (see rewriteRules) // 3. Return preview HTML export default async function middleware(request) { const { origin } = new URL(request.url); diff --git a/preview.config.cjs b/preview.config.cjs new file mode 100644 index 0000000..10fdd87 --- /dev/null +++ b/preview.config.cjs @@ -0,0 +1,25 @@ +module.exports = { + rewriteRules: [ + // Replace CDN URLs with local paths + { + match : /https?.*\/CHANGELOG.md/g, + replace: '/CHANGELOG.md' + }, + { + // CDN versioned default + // Ex1: //cdn.com/package-name + // Ex2: http://cdn.com/package-name@1.0.0 + // Ex3: https://cdn.com/package-name@latest + match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g, + replace: '/dist/js/docsify-themeable.min.js' + }, + { + // CDN paths to local paths + // Ex1: //cdn.com/package-name/path/file.js => /path/file.js + // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js + // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js + match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g, + replace: '/dist/' + } + ], +}; diff --git a/server.cjs b/server.cjs index a3dd517..480a6dc 100644 --- a/server.cjs +++ b/server.cjs @@ -1,7 +1,8 @@ -// Dependencies -// ============================================================================= const browserSync = require('browser-sync').create(); const compression = require('compression'); +const { rewriteRules } = require('./preview.config.cjs'); + +const previewDir = '/preview'; browserSync.init({ files: [ @@ -18,42 +19,25 @@ browserSync.init({ cors: true, reloadDebounce: 1000, reloadOnRestart: true, + rewriteRules, server: { - baseDir: [ - './docs/' - ], - // directory: true, + baseDir: '.', middleware: [ - compression() + compression(), + // Redirect root to preview + function(req, res, next) { + if (req.url === '/') { + res.writeHead(301, { Location: previewDir }); + res.end(); + } + + return next(); + } ], routes: { - '/CHANGELOG.md': './CHANGELOG.md' + [previewDir]: './docs/', + [`${previewDir}/CHANGELOG.md`]: './CHANGELOG.md', } }, - serveStatic: [ - './dist/' - ], - rewriteRules: [ - // Replace CDN URLs with local paths - { - match : /https?.*\/CHANGELOG.md/g, - replace: '/CHANGELOG.md' - }, - { - // CDN versioned default - // Ex1: //cdn.com/package-name - // Ex2: http://cdn.com/package-name@1.0.0 - // Ex3: https://cdn.com/package-name@latest - match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g, - replace: '/js/docsify-themeable.min.js' - }, - { - // CDN paths to local paths - // Ex1: //cdn.com/package-name/path/file.js => /path/file.js - // Ex2: http://cdn.com/package-name@1.0.0/dist/file.js => /dist/file.js - // Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js - match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g, - replace: '/' - } - ] + startPath: previewDir, });