Skip to content

Commit

Permalink
Add shared local and Vercel preview config
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Dec 19, 2023
1 parent d79ab15 commit 3e70e09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 58 deletions.
26 changes: 2 additions & 24 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -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
// =============================================================================
Expand All @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions preview.config.cjs
Original file line number Diff line number Diff line change
@@ -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/'
}
],
};
52 changes: 18 additions & 34 deletions server.cjs
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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,
});

0 comments on commit 3e70e09

Please sign in to comment.