diff --git a/.gitignore b/.gitignore index 833e719..e3b0f8e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ node_modules yarn.lock public dist +out .gen .next diff --git a/index.ts b/index.ts index 0ad458c..e2a11bf 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,7 @@ import fs from 'fs' import path from 'path' import http from 'http' +import serveHandler from 'serve-handler' import serve404 from './libs/serve-404' @@ -13,7 +14,20 @@ const badgeHandlers = fs.readdirSync(path.join(__dirname, 'endpoints')) .filter(name => !name.startsWith('_')) .map(name => name.replace(/\.ts$/, '')) +const isStatic = (url) => { + if (url === '/') return true + if (url.startsWith('/static/')) return true + if (url.startsWith('/_next/')) return true + return false +} + const server = http.createServer(async (req, res) => { + // handle statics + if (isStatic(req.url)) { + return serveHandler(req, res, { public: path.join(__dirname, 'out') }) + } + + // handle endpoints const handlerName = badgeHandlers.find(h => req.url!.startsWith(`/${h}`)) try { diff --git a/next.config.js b/next.config.js index 0fbd0e5..41db1a7 100644 --- a/next.config.js +++ b/next.config.js @@ -1,3 +1,8 @@ module.exports = { - target: 'serverless' + target: 'serverless', + exportPathMap: async function (defaultPathMap) { + return { + '/': { page: '/' } + } + } } diff --git a/now.json b/now.json index 88e3e5c..66e0bfd 100644 --- a/now.json +++ b/now.json @@ -5,7 +5,9 @@ ], "builds": [ { "src": "endpoints/*.ts", "use": "@now/node" }, - { "src": "next.config.js", "use": "@now/next" } + { "src": "package.json", "use": "@now/static-build", "config": { + "distDir": "out" + } } ], "routes": [ { "src": "/(?\\w+)/.*", "dest": "/endpoints/$name.ts" } diff --git a/package.json b/package.json index 8018f49..195d4f7 100644 --- a/package.json +++ b/package.json @@ -5,18 +5,17 @@ "license": "ISC", "private": true, "scripts": { - "dev2": "nodemon -e ts,js -w endpoints -x 'ts-node index.ts'", "lint": "standard", - "dev": "micro-dev service.js -s -i .next", + "dev": "nodemon -e ts,js -w endpoints -w libs -x 'ts-node index.ts'", "dev:web": "next", - "build": "tsc", + "tools": "ts-node tools/gen-examples.ts", + "now-build": "npm run tools && next build && next export", "pretest": "npm run lint", "test": "tap test/*.js --reporter spec -j12", "start": "node service.js", "now-prune": "now rm badgen.net --safe -y -S amio || true", "predeploy": "now -S amio inspect badgen.net", - "deploy": "now -S amio && now -S amio alias", - "canary": "now -S amio && now -S amio alias badgen-canary" + "deploy": "now -S amio && now -S amio alias" }, "standard": { "parser": "babel-eslint"