Skip to content

Commit b9a926a

Browse files
authored
docs(ud): API route prefix (#1838)
Document route prefix
1 parent 8b10a96 commit b9a926a

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

docs/docs/deploy/universal-deploy.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,79 @@ yarn cedar setup deploy vercel --ud
6969

7070
This installs `vite-plugin-vercel`, adds the Vercel Vite plugin, and writes a `vercel.json` configured for Universal Deploy.
7171

72+
## API route prefix
73+
74+
When deploying with Universal Deploy, API routes (your functions under
75+
`api/src/functions/`) need a URL prefix to avoid colliding with your web app's
76+
SPA routes. For example, a `hello` function should be accessible at
77+
`/.api/functions/hello`, not `/hello`.
78+
79+
Two related but distinct concepts control this:
80+
81+
### `apiUrl` (`cedar.toml`)
82+
83+
```toml
84+
[web]
85+
apiUrl = "/.api/functions"
86+
```
87+
88+
`apiUrl` is a **web-side configuration** that tells the Cedar web server which
89+
URL paths should be proxied to the API server. In the browser,
90+
`globalThis.RWJS_API_URL` is set to this value so your web code knows where to
91+
send API requests.
92+
93+
During local testing with `yarn cedar serve --ud` or `yarn cedar serve web`,
94+
the web server (port 8910) intercepts requests matching `apiUrl`, **strips the
95+
prefix**, and forwards them to the API server (port 8911). So a browser request
96+
to `http://localhost:8910/.api/functions/hello` reaches the API server as
97+
`/hello`.
98+
99+
### `--apiRootPath` (CLI flag)
100+
101+
```
102+
yarn cedar build --ud --apiRootPath=/.api/functions
103+
```
104+
105+
`--apiRootPath` is a **build-time configuration** for the
106+
`cedarUniversalDeployPlugin`. It determines the route prefix baked into the
107+
Universal Deploy server entry (`api/dist/ud/index.js`).
108+
109+
When the flag is not passed, `apiRootPath` defaults to `/`, meaning routes are
110+
registered at `/hello`, `/graphql`, etc. This is correct for local development
111+
where the web dev server strips the prefix before forwarding.
112+
113+
When deploying to a serverless provider (Netlify, Vercel), the provider routes
114+
requests at the prefixed path directly to your functions. There is no dev server
115+
to strip the prefix, so you must set `--apiRootPath=/.api/functions` so routes
116+
are registered at `/.api/functions/hello`, matching how the provider forwards
117+
requests.
118+
119+
Both `yarn cedar setup deploy netlify --ud` and
120+
`yarn cedar setup deploy vercel --ud` configure this automatically in their
121+
build commands.
122+
123+
### `CEDAR_API_ROOT_PATH` (environment variable)
124+
125+
```shell
126+
CEDAR_API_ROOT_PATH=/.api/functions yarn cedar build --ud
127+
```
128+
129+
The `CEDAR_API_ROOT_PATH` environment variable can be used instead of the
130+
`--apiRootPath` CLI flag. It takes precedence over any value set in
131+
`cedarUniversalDeployPlugin` options, but the `--apiRootPath` CLI flag takes
132+
precedence over the environment variable when both are set. This is useful for
133+
CI/CD environments where you want to configure the prefix via environment
134+
injection without modifying source files or build commands.
135+
136+
### Summary
137+
138+
| Concept | Where set | Precedence | Purpose | Typical value |
139+
| --------------------------------------------- | ----------------------------------- | ------------------------ | ---------------------------------------------------------------------------- | ----------------------------------------------- |
140+
| `apiUrl` | `cedar.toml` `[web]` section || Tells the browser and web dev server where the API lives | `/.api/functions` |
141+
| `--apiRootPath` | CLI flag on `yarn cedar build --ud` | Highest | Controls the route prefix baked into the UD server entry | `/.api/functions` (deploy), not set (local dev) |
142+
| `CEDAR_API_ROOT_PATH` | Environment variable | Overrides plugin options | Overrides `cedarUniversalDeployPlugin` options without modifying vite config | `/.api/functions` |
143+
| `cedarUniversalDeployPlugin({ apiRootPath })` | Vite config | Lowest | Configure the route prefix in the plugin options ||
144+
72145
## How it works
73146

74147
The `--ud` build step (`yarn cedar build --ud`) produces a server entry at `api/dist/ud/index.js` that exports:

0 commit comments

Comments
 (0)