-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shared local and Vercel preview config
- Loading branch information
1 parent
d79ab15
commit 3e70e09
Showing
3 changed files
with
45 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected] | ||
// 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/[email protected]/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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/[email protected] | ||
// 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/[email protected]/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/' | ||
} | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [ | ||
|
@@ -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/[email protected] | ||
// 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/[email protected]/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, | ||
}); |