1
0
Fork 0
mirror of https://github.com/kou029w/_.git synced 2025-01-30 13:58:08 +00:00
This commit is contained in:
Nebel 2024-04-22 00:20:58 +09:00
parent acabdaec8c
commit 08f797b3f2
Signed by: nebel
GPG key ID: 79807D08C6EF6460
11 changed files with 2760 additions and 1874 deletions

View file

@ -1,2 +1,2 @@
.vercel
dist
node_modules

View file

@ -1,19 +1,17 @@
## 何をしたかったか
Fastify と fastify-autoload を使って Vercel にデプロイできるかどうかチェック。
Fastify と @fastify/autoload を使って Vercel にデプロイできるかどうかチェック。
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fkou029w%2F_%2Ftree%2Fmaster%2Fvercel-fastify)
## 結果
cjs かつ includeFiles に含めればとりあえず OK。
全部含めるなら `**` でよい。
とりあえず OK
```json
{
"functions": { "api/index.js": { "includeFiles": "**" } }
}
```
## ネタ
TypeScript の場合は、`vercel-build` にビルドコマンドを指定する & ビルド後に生成されるファイルを指定する。(vercel.json を参照)
そういった設定をせず Vercel に任せると `vercel dev` では問題ないが、デプロイすると参照に失敗して 500 エラー。
- `api/_routes` に配置することで `api/**/*` includeFiles 不要
- top-level await NG
- await する処理は必ず handler 内に完結させる必要ある
- `api/[...].ts``api/foo/bar` にもルーティングしてほしいのだが、うまく機能しないようだ
- vercel.json 参照

View file

@ -1,10 +1,18 @@
import { create } from "../dist/server";
import autoload from "@fastify/autoload";
import type { VercelRequest, VercelResponse } from "@vercel/node";
import fastify from "fastify";
import path from "node:path";
const server = create({ isDev: process.env.NODE_ENV === "development" });
const app = fastify();
async function index(req, res) {
await server.ready();
server.server.emit("request", req, res);
app.register(autoload, {
dir: path.resolve(__dirname, "_routes"),
options: {
prefix: "/api",
},
});
export default async function handler(req: VercelRequest, res: VercelResponse) {
await app.ready();
app.server.emit("request", req, res);
}
export default index;

View file

@ -3,18 +3,15 @@
"version": "0.0.0",
"private": true,
"scripts": {
"build": ":",
"vercel-build": "tsup src --clean",
"preview": "vercel dev",
"deploy": "vercel deploy"
},
"dependencies": {
"fastify": "^3.28.0",
"fastify-autoload": "^3.12.0",
"pino-pretty": "^7.6.1"
"@fastify/autoload": "^5.8.0",
"fastify": "^3.29.5"
},
"devDependencies": {
"tsup": "^5.12.6",
"vercel": "^24.1.0"
"@vercel/node": "^3.0.27",
"vercel": "^24.2.5"
}
}

2726
vercel-fastify/pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,8 +0,0 @@
import { create, start } from "./server";
async function main() {
const server = create({ isDev: true });
await start(server, "3000");
}
main();

View file

@ -1,27 +0,0 @@
import fastify, { FastifyInstance } from "fastify";
import autoload from "fastify-autoload";
import path from "node:path";
type Options = {
isDev?: boolean;
quiet?: boolean;
};
type Server = FastifyInstance;
export function create(options: Options): Server {
const app = fastify({
logger: !options.quiet && { prettyPrint: options.isDev },
});
app.register(autoload, {
dir: path.resolve(__dirname, "routes"),
routeParams: true,
});
return app;
}
export async function start(server: Server, port: string): Promise<string> {
await server.ready();
const address: string = await server.listen(port, "::");
return address;
}

View file

@ -1,4 +1,8 @@
{
"functions": { "api/index.ts": { "includeFiles": "dist/**" } },
"rewrites": [{ "source": "/api/(.*)", "destination": "/api/index.ts" }]
"rewrites": [
{
"source": "/(.*)",
"destination": "/api/"
}
]
}

File diff suppressed because it is too large Load diff