From 3e70e099db7c9aa319afd022ec6679446326888b Mon Sep 17 00:00:00 2001 From: John Hildenbiddle Date: Tue, 19 Dec 2023 12:21:53 -0600 Subject: [PATCH] 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, });