Skip to content

Commit b0d5574

Browse files
Merge pull request #87 from jhildenbiddle/vercel
Add Vercel preview
2 parents 9cb881f + 19775df commit b0d5574

File tree

5 files changed

+77
-34
lines changed

5 files changed

+77
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ node_modules
1111
._*
1212
.cache
1313
.DS_Store
14+
.vercel

middleware.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { rewriteRules } from './preview.config.cjs';
2+
3+
// Exports
4+
// =============================================================================
5+
export const config = {
6+
matcher: ['/preview/(index.html)?'],
7+
};
8+
9+
// Serve virtual /preview/index.html
10+
// Note: See vercel.json for preview routing configuration
11+
// 1. Fetch index.html from /docs/ directory
12+
// 2. Replace CDN URLs with local paths (see rewriteRules)
13+
// 3. Return preview HTML
14+
export default async function middleware(request) {
15+
const { origin } = new URL(request.url);
16+
const indexURL = `${origin}/docs/index.html`;
17+
const indexHTML = await fetch(indexURL).then((res) => res.text());
18+
const previewHTML = rewriteRules.reduce((html, rule) => html.replace(rule.match, rule.replace), indexHTML);
19+
20+
return new Response(previewHTML, {
21+
status: 200,
22+
headers: { 'content-type': 'text/html' },
23+
});
24+
}

preview.config.cjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
rewriteRules: [
3+
// Replace CDN URLs with local paths
4+
{
5+
match : /https?.*\/CHANGELOG.md/g,
6+
replace: '/CHANGELOG.md'
7+
},
8+
{
9+
// CDN versioned default
10+
// Ex1: //cdn.com/package-name
11+
// Ex2: http://cdn.com/[email protected]
12+
// Ex3: https://cdn.com/package-name@latest
13+
match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g,
14+
replace: '/dist/js/docsify-themeable.min.js'
15+
},
16+
{
17+
// CDN paths to local paths
18+
// Ex1: //cdn.com/package-name/path/file.js => /path/file.js
19+
// Ex2: http://cdn.com/[email protected]/dist/file.js => /dist/file.js
20+
// Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js
21+
match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g,
22+
replace: '/dist/'
23+
}
24+
],
25+
};

server.cjs

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Dependencies
2-
// =============================================================================
31
const browserSync = require('browser-sync').create();
42
const compression = require('compression');
3+
const { rewriteRules } = require('./preview.config.cjs');
4+
5+
const previewDir = '/preview';
56

67
browserSync.init({
78
files: [
@@ -18,42 +19,25 @@ browserSync.init({
1819
cors: true,
1920
reloadDebounce: 1000,
2021
reloadOnRestart: true,
22+
rewriteRules,
2123
server: {
22-
baseDir: [
23-
'./docs/'
24-
],
25-
// directory: true,
24+
baseDir: '.',
2625
middleware: [
27-
compression()
26+
compression(),
27+
// Redirect root to preview
28+
function(req, res, next) {
29+
if (req.url === '/') {
30+
res.writeHead(301, { Location: previewDir });
31+
res.end();
32+
}
33+
34+
return next();
35+
}
2836
],
2937
routes: {
30-
'/CHANGELOG.md': './CHANGELOG.md'
38+
[previewDir]: './docs/',
39+
[`${previewDir}/CHANGELOG.md`]: './CHANGELOG.md',
3140
}
3241
},
33-
serveStatic: [
34-
'./dist/'
35-
],
36-
rewriteRules: [
37-
// Replace CDN URLs with local paths
38-
{
39-
match : /https?.*\/CHANGELOG.md/g,
40-
replace: '/CHANGELOG.md'
41-
},
42-
{
43-
// CDN versioned default
44-
// Ex1: //cdn.com/package-name
45-
// Ex2: http://cdn.com/[email protected]
46-
// Ex3: https://cdn.com/package-name@latest
47-
match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*(?=["'])/g,
48-
replace: '/js/docsify-themeable.min.js'
49-
},
50-
{
51-
// CDN paths to local paths
52-
// Ex1: //cdn.com/package-name/path/file.js => /path/file.js
53-
// Ex2: http://cdn.com/[email protected]/dist/file.js => /dist/file.js
54-
// Ex3: https://cdn.com/package-name@latest/dist/file.js => /dist/file.js
55-
match : /(?:https?:)*\/\/.*cdn.*docsify-themeable[@\d.latest]*\/(?:dist\/)/g,
56-
replace: '/'
57-
}
58-
]
42+
startPath: previewDir,
5943
});

vercel.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"redirects": [
3+
{ "source": "/", "destination": "/preview/" }
4+
],
5+
"rewrites": [
6+
{ "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" },
7+
{ "source": "/preview/:path*", "destination": "/docs/:path*" }
8+
]
9+
}

0 commit comments

Comments
 (0)