Guides
Adding an app
Give a context a React app, served by the same backend at the context's own path.
A context’s app is a web app that the backend serves next to the context’s API. newt add app gives a context a React app, bundled by Bun and served at /<context>, with no separate server or build step.
Add one
Section titled “Add one”newt add app tasks created src/contexts/tasks/app/index.html created src/contexts/tasks/app/main.tsx created src/contexts/tasks/app/app.tsx created src/contexts/tasks/app/base.ts created src/contexts/tasks/app/tsconfig.json updated package.json updated tsconfig.json updated src/contexts/tasks/tasks.context.ts updated src/backend.test.ts
Next steps: bun install bun run dev, and open /tasksRun bun install for React, then bun run dev, and open http://localhost:3000/tasks.
The context must exist first. Each context has at most one app, at /<context>.
What it writes
Section titled “What it writes”| File | What it is for |
|---|---|
app/index.html |
the page; it loads main.tsx |
app/main.tsx |
mounts the React app on #root |
app/app.tsx |
your app |
app/base.ts |
where the app is served, /tasks, and its API, /tasks/api |
app/tsconfig.json |
TypeScript for the browser, with JSX, the DOM and the context’s schemas |
It also changes four files:
package.json: addsreact,react-domand their types.tsconfig.json: leavessrc/contexts/*/appout and references the app’s owntsconfig.json, sobun run typecheckchecks both.src/contexts/tasks/tasks.context.ts: hands its app to run, next to its router.src/backend.test.ts: checks that the app’s page is served.
export function App() { return <h1>Tasks</h1>}How it is served
Section titled “How it is served”The context hands its app to run, next to its router:
import { default as app } from './app/index.html'
static async open( platform: Platform, place: DataPlace, ): Promise<ContextToRun> { // … const context = Tasks_Context.initialize(base, router) return { context, router, host, app } }app is an HTML import or the folder of a built app. Importing an .html file gives Bun an HTML bundle: Bun bundles the page with its scripts and styles and serves the result.
/tasksand every path under it, such as/tasks/board/12, answer with the page, so your app can have its own client-side routes./tasks/api/…stays the context’s API, and/tasks/health-checkits health: a path there that no route takes is a 404, never the page.- Two contexts’ apps never meet: each one is under its own context.
While you develop
Section titled “While you develop”bun run dev restarts the backend when you change server code. The generated backend does not turn on Bun’s development mode, so the page is bundled once: after you change the app, restart bun run dev to see it.
To get hot reloading in the browser, turn on development in src/backend.ts:
return runContexts(contexts, { platformWithoutContexts: platform, serve: { port, development: Bun.env.NODE_ENV !== 'production', },})Bun then rebuilds the page on every save and reloads it in the browser.
Call the API
Section titled “Call the API”The page and the API share the origin, so call the API by its path. base.ts holds it, and the app imports the types of its context’s schemas, so it never copies a DTO by hand:
import { useEffect, useState } from 'react'import type { TaskDTO } from '../modules/task/task.schema'import { API } from './base'
export function App() { const [tasks, setTasks] = useState<TaskDTO[]>([])
useEffect(() => { fetch(`${API}/tasks`) .then(response => response.json()) .then(page => setTasks(page.items)) }, [])
return ( <ul> {tasks.map(task => <li key={task.id}>{task.name}</li>)} </ul> )}- The browser sends the backend’s cookies with these requests, so a session cookie signs them in. See authentication.
- Until
src/auth.tssigns someone in, the API answers 401 to the page too. - A
POST,PUT,PATCHorDELETEfrom the page passes the cross-origin check, because it comes from the backend’s own origin.
- The app imports its own files and, with
import type, its context’s schemas: never the server’s code. Theapp-importsrule checks it. - Give a client-side router
BASEas its base, such as React Router’sbasename.
An app built elsewhere
Section titled “An app built elsewhere”If you build your app with another tool, the context can serve the built folder instead:
newt add app tasks --built web/dist--builttakes the folder that holds the builtindex.html.- Build it for
/<context>/, such asvite build --base=/tasks/, or its assets answer 404. - Until that folder has an
index.html,/tasksanswers 503. - Files that exist are served as they are. Any other path under
/tasksgetsindex.html. - The context hands it to run as
app: join(import.meta.dir, '../../../web/dist'), from its own folder. - Its HTML pages get a Content-Security-Policy and
X-Frame-Options: DENY. Change the policy with the server’scspoption.
The folder cannot be one that holds the backend, since everything in it would be served, .env included.
Options
Section titled “Options”newt add app <context> [--built <folder>] [--backend <folder>]| Option | What it does |
|---|---|
--built |
serves a folder built elsewhere, instead of writing a React app |
--backend |
which backend, in a monorepo with several |
See the newt add app reference.