Skip to content
homeport

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:

  1. /api/* goes to your API. A path it doesn’t have is a 404.
  2. /assets/* is a file from dist/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.
  3. Anything else is a file from dist if there is one, and dist/index.html if not. That is what makes a deep link like /about load.

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.

vite.config.js
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [react()],
  server: { proxy: { '/api': 'http://localhost:3000' } },
})
src/main.jsx
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

Files
.gitignore
index.html
package.json
package-lock.json
server.js
src/main.jsx
vite.config.js
Install
npm install hono @hono/node-server react react-dom react-router
npm install -D vite @vitejs/plugin-react
package.json
{
  "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.

server.js
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) })
Deploy
homeport deploy

What homeport does, with no settings and no homeport.yaml:

  • It reads hono in 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.json and runs your build script.
  • It drops your dev dependencies and ships the app with dist.
  • It starts the app the way your start script does, on Node.js.

On Bun

Three changes run the same app on Bun:

  1. Set start to bun server.js.
  2. Remove @hono/node-server from package.json and delete package-lock.json. Run bun install and commit bun.lock.
  3. Change the imports and the last line of server.js:
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

Files
.gitignore
bun.lock
index.html
main.py
package.json
requirements.txt
src/main.jsx
vite.config.js

package.json sits beside main.py, in the app’s folder.

Install
bun add react react-dom react-router
bun add -d vite @vitejs/plugin-react
package.json
{
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build"
  }
}
requirements.txt
fastapi==0.162.0
uvicorn==0.54.0
main.py
from 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.

Deploy
homeport deploy

What homeport does, with no settings and no homeport.yaml:

  • It reads fastapi in requirements.txt, so this is a Python app, even with a package.json beside 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 install and bun run build, because package.json has a build script. You need no start script.
  • It ships dist with your code, without node_modules.
  • It finds app = FastAPI() in main.py and 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:

homeport.yaml
assets:
  - from: dist/assets
    at: /assets

Each 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:

What the deployed app answers
RequestAnswer
/200, index.html
/api/hello200, JSON
/api/nope404, JSON
/about200, index.html, and React Router draws the page
/assets/index-D1y-3jJZ.js200, with Cache-Control: public, max-age=31536000, immutable
/assets/x.js404