|
1 | 1 | import { Hono } from "hono"; |
| 2 | +import { readFileSync, readdirSync, existsSync } from "fs"; |
| 3 | +import { resolve, dirname } from "path"; |
2 | 4 | import api from "./api/api"; |
| 5 | +import { serveStatic } from "@hono/node-server/serve-static"; |
3 | 6 |
|
4 | 7 | const app = new Hono(); |
5 | 8 |
|
6 | | -// API routes |
| 9 | + |
| 10 | +// Debug route to see filesystem structure on Vercel |
| 11 | +app.get("/_debug", (c) => { |
| 12 | + const listDir = (path: string, depth = 0): string[] => { |
| 13 | + const results: string[] = []; |
| 14 | + const indent = " ".repeat(depth); |
| 15 | + try { |
| 16 | + if (!existsSync(path)) { |
| 17 | + results.push(`${indent}[NOT FOUND: ${path}]`); |
| 18 | + return results; |
| 19 | + } |
| 20 | + const entries = readdirSync(path, { withFileTypes: true }); |
| 21 | + for (const entry of entries.slice(0, 50)) { // Limit to 50 entries |
| 22 | + if (entry.isDirectory()) { |
| 23 | + results.push(`${indent}${entry.name}/`); |
| 24 | + if (depth < 2) { // Limit depth |
| 25 | + results.push(...listDir(resolve(path, entry.name), depth + 1)); |
| 26 | + } |
| 27 | + } else { |
| 28 | + results.push(`${indent}${entry.name}`); |
| 29 | + } |
| 30 | + } |
| 31 | + } catch (e) { |
| 32 | + results.push(`${indent}[ERROR: ${e}]`); |
| 33 | + } |
| 34 | + return results; |
| 35 | + }; |
| 36 | + |
| 37 | + const cwd = process.cwd(); |
| 38 | + const metaDirname = import.meta.dirname; |
| 39 | + |
| 40 | + const info = { |
| 41 | + cwd, |
| 42 | + metaDirname, |
| 43 | + cwdContents: listDir(cwd), |
| 44 | + metaDirnameContents: listDir(metaDirname), |
| 45 | + publicFromCwd: listDir(resolve(cwd, "public")), |
| 46 | + parentDir: listDir(resolve(metaDirname, "..")), |
| 47 | + }; |
| 48 | + |
| 49 | + return c.json(info, 200, { "Content-Type": "application/json" }); |
| 50 | +}); |
| 51 | + |
| 52 | +// API routes first |
7 | 53 | app.route("/", api); |
8 | 54 |
|
9 | | -export default app; |
| 55 | +app.use("/*", serveStatic({ root: resolve(process.cwd(), "public") })); |
10 | 56 |
|
| 57 | + |
| 58 | +// SPA fallback - serve index.html for client-side routing |
| 59 | +// Static files are served by Vercel CDN from public/ |
| 60 | +app.get("*", (c) => { |
| 61 | + const path = c.req.path; |
| 62 | + |
| 63 | + // Skip if it looks like a static file request |
| 64 | + if (path.includes(".") && !path.endsWith(".html")) { |
| 65 | + return c.notFound(); |
| 66 | + } |
| 67 | + |
| 68 | + // Serve index.html for SPA routes |
| 69 | + try { |
| 70 | + const indexPath = resolve(process.cwd(), "public", "index.html"); |
| 71 | + const html = readFileSync(indexPath, "utf-8"); |
| 72 | + return c.html(html); |
| 73 | + } catch { |
| 74 | + return c.notFound(); |
| 75 | + } |
| 76 | +}); |
| 77 | + |
| 78 | +export default app; |
0 commit comments