Docs · Deploy
A Vite front end and an API in one app
Have your API server serve your Vite build. The page and /api/* then share one address, so you deploy one app and need no CORS. These are two recipes, one for Hono and one for FastAPI. Each is a React app that calls /api/hello and has a client-side route, /about.
What the server answers
Both recipes answer in the same order:
/api/*goes to your API. A path it doesn’t have is a 404./assets/*is a file fromdist/assets, cached for a year. A file that isn’t there is a 404, not your page: a missing script should fail as a missing script.- Anything else is a file from
distif there is one, anddist/index.htmlif not. That is what makes a deep link like/aboutload.
homeport sets no Cache-Control on what your app serves, so the server sets it. Vite puts a hash of each file’s content in its name, which is why a year is safe.
The front end
It’s the same for both recipes. index.html is Vite’s usual one, with a <div id="root"> and a script tag for /src/main.jsx.
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [react()],
server: { proxy: { '/api': 'http://localhost:3000' } },
})import { useEffect, useState } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter, Link, Route, Routes } from 'react-router'
function Home() {
const [message, setMessage] = useState('Loading...')
useEffect(() => {
fetch('/api/hello')
.then((res) => res.json())
.then((data) => setMessage(data.message))
}, [])
return <p>{message}</p>
}
createRoot(document.getElementById('root')).render(
<BrowserRouter>
<nav>
<Link to="/">Home</Link> <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<p>About this app</p>} />
</Routes>
</BrowserRouter>,
)The proxy line is for your own machine: run the API on port 3000 and vite beside it, and /api calls reach the API. It plays no part in a deploy.
Keep dist and node_modules in your .gitignore. homeport builds dist itself.
Hono
.gitignore
index.html
package.json
package-lock.json
server.js
src/main.jsx
vite.config.jsnpm install hono @hono/node-server react react-dom react-router
npm install -D vite @vitejs/plugin-react{
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"start": "node server.js"
}
}This shows only the keys you add. npm writes your dependencies beside them.
import { serve } from '@hono/node-server'
import { serveStatic } from '@hono/node-server/serve-static'
import { Hono } from 'hono'
const app = new Hono()
app.get('/api/hello', (c) => c.json({ message: 'Hello from Hono' }))
app.all('/api/*', (c) => c.json({ error: 'not found' }, 404))
app.use(
'/assets/*',
serveStatic({
root: './dist',
onFound: (_path, c) => c.header('Cache-Control', 'public, max-age=31536000, immutable'),
}),
)
app.get('/assets/*', (c) => c.notFound())
app.use(serveStatic({ root: './dist' }))
app.get('*', serveStatic({ path: './dist/index.html' }))
serve({ fetch: app.fetch, hostname: '0.0.0.0', port: Number(process.env.PORT ?? 3000) })homeport deployWhat homeport does, with no settings and no homeport.yaml:
- It reads
honoin your dependencies, so this is a server and not a static site, even though it uses Vite. The build log says==> Hono. - It installs from
package-lock.jsonand runs yourbuildscript. - It drops your dev dependencies and ships the app with
dist. - It starts the app the way your
startscript does, on Node.js.
On Bun
Three changes run the same app on Bun:
- Set
starttobun server.js. - Remove
@hono/node-serverfrompackage.jsonand deletepackage-lock.json. Runbun installand commitbun.lock. - Change the imports and the last line of
server.js:
import { Hono } from 'hono'
import { serveStatic } from 'hono/bun'
// ...the same routes...
Bun.serve({ fetch: app.fetch, hostname: '0.0.0.0', port: Number(process.env.PORT ?? 3000) })FastAPI
.gitignore
bun.lock
index.html
main.py
package.json
requirements.txt
src/main.jsx
vite.config.jspackage.json sits beside main.py, in the app’s folder.
bun add react react-dom react-router
bun add -d vite @vitejs/plugin-react{
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
}
}fastapi==0.162.0
uvicorn==0.54.0from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
dist = (Path(__file__).parent / "dist").resolve()
class Assets(StaticFiles):
async def get_response(self, path, scope):
response = await super().get_response(path, scope)
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
return response
app = FastAPI()
@app.get("/api/hello")
def hello():
return {"message": "Hello from FastAPI"}
app.mount("/assets", Assets(directory=dist / "assets", check_dir=False), name="assets")
@app.get("/{path:path}")
def spa(path: str):
if path == "api" or path.startswith("api/"):
raise HTTPException(status_code=404)
file = (dist / path).resolve()
if file.is_relative_to(dist) and file.is_file():
return FileResponse(file)
return FileResponse(dist / "index.html")Define the API routes before the last route: FastAPI takes the first match, and the last one matches every path. The is_relative_to check keeps a crafted path from reading files outside dist, such as main.py. On your machine, run the API with uvicorn main:app --port 3000.
homeport deployWhat homeport does, with no settings and no homeport.yaml:
- It reads
fastapiinrequirements.txt, so this is a Python app, even with apackage.jsonbeside it. The build log says==> FastAPI. - It installs your requirements. If they have no uvicorn, it adds a pinned one and says so in the log.
- It runs
bun installandbun run build, becausepackage.jsonhas abuildscript. You need no start script. - It ships
distwith your code, withoutnode_modules. - It finds
app = FastAPI()inmain.pyand starts-m uvicorn --host 0.0.0.0 --port $PORT main:app.
Keep old files after a deploy
A tab opened before a deploy still asks for the old hashed files. homeport can’t tell where your server keeps them, so name the folder in a homeport.yaml, the same for both recipes:
assets:
- from: dist/assets
at: /assetsEach deploy then says it kept dist/assets, and the release before it stays reachable for a day. More in old files after a deploy.
Check it
Both recipes answer like this at their live address:
| Request | Answer |
|---|---|
/ | 200, index.html |
/api/hello | 200, JSON |
/api/nope | 404, JSON |
/about | 200, index.html, and React Router draws the page |
/assets/index-D1y-3jJZ.js | 200, with Cache-Control: public, max-age=31536000, immutable |
/assets/x.js | 404 |