Skip to content

Commit

Permalink
Add Vercal configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Dec 21, 2023
1 parent bce2119 commit c098ffa
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@

# exceptions
!.gitkeep
.vercel
32 changes: 32 additions & 0 deletions middleware.js
@@ -0,0 +1,32 @@
import serverConfigs from './server.config.js';

const { rewriteRules } = serverConfigs.dev;

// 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',
'x-robots-tag': 'noindex',
},
});
}
78 changes: 49 additions & 29 deletions server.config.js
Expand Up @@ -3,52 +3,65 @@ import * as url from 'node:url';

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const defaults = {

// Production (CDN URLs, watch disabled)
const prod = {
hostname: '127.0.0.1',
notify: false,
open: false,
port: 8080,
server: {
baseDir: './docs',
},
snippet: false,
ui: false,
};

// Development (local URLs, watch enabled)
const dev = {
...prod,
files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'],
port: 3000,
rewriteRules: [
// Replace remote URLs with local paths
// Replace CDN URLs with local paths
{
// Changelog
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[@\d.latest]*(?=["'])/g,
replace: '/lib/docsify.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[@\d.latest]*\/(?:lib\/)/g,
replace: '/lib/',
},
],
server: {
baseDir: 'docs',
index: 'preview.html',
...prod.server,
routes: {
'/changelog.md': path.resolve(__dirname, 'CHANGELOG.md'),
'/lib': path.resolve(__dirname, 'lib'),
'/node_modules': path.resolve(__dirname, 'node_modules'), // Required for automated Vue tests
},
},
snippet: false,
ui: false,
snippet: true,
};

export default {
// Development (preview, local URLs, watch enabled)
dev: {
...defaults,
files: ['CHANGELOG.md', 'docs/**/*', 'lib/**/*'],
port: 3000,
open: true,
snippet: true,
},
// Production (index, CDN URLs, watch disabled)
prod: {
...defaults,
port: 8080,
server: {
...defaults.server,
index: 'index.html',
},
},
// Test (preview, local URLs, watch disabled)
test: {
...defaults,
// Test (local URLs, watch disabled)
const test = {
...dev,
port: 4000,
server: {
...dev.server,
middleware: [
// Blank page required for test environment
{
Expand All @@ -60,6 +73,13 @@ export default {
},
},
],
port: 4000,
},
snippet: false,
watch: false,
};

export default {
dev,
prod,
test,
};
2 changes: 1 addition & 1 deletion server.js
Expand Up @@ -8,6 +8,6 @@ const configName =
const settings = serverConfigs[configName];

// prettier-ignore
console.log(`\nStarting ${configName} server (${settings.server.index}, watch: ${Boolean(settings.files)})\n`);
console.log(`\nStarting ${configName} server (watch: ${Boolean(settings.files)})\n`);

bsServer.init(settings);
12 changes: 8 additions & 4 deletions vercel.json
@@ -1,9 +1,13 @@
{
"redirects": [
"headers": [
{
"source": "/",
"destination": "./docs/preview.html",
"permanent": true
"source": "/(.*)",
"headers": [{ "key": "x-robots-tag", "value": "noindex" }]
}
],
"redirects": [{ "source": "/", "destination": "/preview/" }],
"rewrites": [
{ "source": "/preview/CHANGELOG.md", "destination": "/CHANGELOG.md" },
{ "source": "/preview/:path*", "destination": "/docs/:path*" }
]
}

0 comments on commit c098ffa

Please sign in to comment.