Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vercel preview #87

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ node_modules
._*
.cache
.DS_Store
.vercel
24 changes: 24 additions & 0 deletions middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { rewriteRules } from './preview.config.cjs';

// 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)
// 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' },
});
}
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,
});
9 changes: 9 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"redirects": [
{ "source": "/", "destination": "/preview/" }
],
"rewrites": [
{ "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" },
{ "source": "/preview/:path*", "destination": "/docs/:path*" }
]
}
Loading