# livebranch > livebranch.dev runs a TypeScript project in the browser, straight from a GitHub branch, pull request or commit. The TypeScript compiler (tsrs, as WebAssembly) compiles the sources in the visitor's tab, so there is no server build, no account and nothing to install. A link holds everything. Use it to show someone a running branch or pull request of a TypeScript library, with a demo, and to compare it with another ref side by side. ## Open something Put `https://livebranch.dev/` in front of a GitHub URL of a repository, a branch, a pull request, a commit or a file: - `https://livebranch.dev/github.com/maplibre/maplibre-gl-js/pull/8628` - `https://livebranch.dev/github.com/owner/repo/blob/branch/examples/page.html` opens that page of that branch. The same as link parameters: `https://livebranch.dev/#gh=owner/repo@ref`. The address of a pull request opens it next to the repository's default branch, so that what it changes shows. It redirects to `#gh=owner/repo@pull/123&base=main`, and parameters in its own fragment win (`livebranch.dev/owner/repo/pull/123#base=v1.0.0&mirror=1`). The bar over the previews names each side, and starts, changes and stops a comparison. ## A demo in the description of a pull request A pull request can carry the demo that shows what it changes. Put it in the description as a code block whose first line says `livebranch` after the language: ````md ```ts livebranch import * as maplibregl from 'maplibre-gl'; const map = new maplibregl.Map({container: 'map', style: 'https://demotiles.maplibre.org/style.json'}); ``` ```` The address of the pull request, `https://livebranch.dev/owner/repo/pull/123`, then runs that demo on the default branch and on the pull request, side by side. No link with code in it is needed, and editing the description changes the demo. - GitHub shows the block as highlighted code and nothing of the word `livebranch`. So write next to it where it runs. - A further word names the file: `ts livebranch helper.ts`. Without one, a `ts` block is `demo.ts` and an `html` block is `index.html`. - A link that has files of its own (`code`, `gist`) or an `entry` does not read the description. - The page reads the description from GitHub's API. That is one request for each opened pull request, out of the 60 an hour that an anonymous visitor gets. Past them the project's stock demo runs and a warning says why. - The block may sit inside a `
` element, which keeps a long demo out of the way. It needs an empty line before and after it there. - The Share button of the page copies the open demo as such a block, folded away in `
` under a line that names the address. `window.__livebranch.block()` returns the same text. - `/api/link` and `/api/check` do not read descriptions. Send the demo to them as `files`. ## Link parameters They go in the URL fragment, joined by `&`. | Parameter | Meaning | | --- | --- | | `gh=owner/repo@ref` | The project. `ref` is a branch, a tag or a commit. `owner/repo@pull/123` is the head of a pull request, also one from a fork. | | `base=ref` | A second ref, shown next to the first and running the same demo. A ref of the same repository (`main`) or a full `owner/repo@ref`. | | `mirror=1` | In a comparison, input on one side goes to the other too, at the same place, and each side handles it with its own code. | | `gesture=…` | A gesture recorded with Record in the app. Play starts the pages afresh and gives each the same input at the same times. | | `play=1` | Play the gesture as soon as the pages are up, once. The address then drops it. | | `entry=path` | The page to open or the script to run. | | `code=...` | Files carried in the link. Make it with `POST /api/link`. | | `gist=id` | Files of a gist, laid over the project. `id@revision` pins it. | | `adapter=name` | Use a named adapter. `none` turns the lookup off. | | `view=result` | Show only the running page, without the editor. | | `embed=1` | For an iframe on another page. A compact header with a link that opens the link in livebranch, and the panel closed. | | `file=path` | The file that the code panel opens first. | | `run=click` | Load nothing until the reader presses Run, as on a page with several embeds. | A link that names a branch or a pull request shows its latest push. A link that names a full commit hash never changes. ## Writing a demo A demo is either a page (`index.html`) or a script (`demo.ts`). Both may contain TypeScript. A script runs in a page made from the project's template. - Import the project by its package name. `import {thing} from 'package-name'` resolves to the project's `src/index.ts` at the ref of the link. - Imports of other npm packages load from esm.sh, at the version in the project's lockfile. - Relative imports between the files of the link work, with or without `.ts`. - The demo is type checked against the project at that ref. The errors show in the Problems tab. For maplibre-gl-js, a script demo gets a page with the stylesheet and a full-size `
`: ```ts import * as maplibregl from 'maplibre-gl'; const map = new maplibregl.Map({container: 'map', style: 'https://demotiles.maplibre.org/style.json', center: [10, 50], zoom: 3}); ``` maplibre-gl 6 has no default export, so import it as a namespace or by name. ## API `https://livebranch.dev/api` lists the endpoints. They answer JSON and allow any origin. One address gets 30 requests a minute. ### Make a link ```sh curl -X POST https://livebranch.dev/api/link -H 'content-type: application/json' -d '{ "gh": "maplibre/maplibre-gl-js@pull/8628", "base": "main", "files": {"demo.ts": "import * as maplibregl from \"maplibre-gl\";\nnew maplibregl.Map({container: \"map\", style: \"https://demotiles.maplibre.org/style.json\"});\n"} }' ``` It answers `{"url": "https://livebranch.dev/#gh=...&base=main&code=..."}`. The body takes `gh`, `base`, `entry`, `gist`, `adapter`, `files` (name to text), `result` (true for `view=result`) and `pin` (true to turn a branch or pull request into its commit, which reads from GitHub). ### Check without a browser ```sh curl -X POST https://livebranch.dev/api/check -H 'content-type: application/json' -d '{ "files": {"index.html": ""} }' ``` It compiles and type checks, and answers `{"problems": [{"file", "line", "column", "code", "category", "text"}], "modules", "entry", "skippedSetup"}`. It takes the same body as `/api/link`. Three limits apply to checks on the server: - Branches, tags, pull requests and full commit hashes need no token. Only an abbreviated commit hash or a gist is read through GitHub's API, and for those the server needs a token of yours as `Authorization: Bearer `, since it shares the anonymous limit with many others. A token without any permission is enough. It is passed on to api.github.com only and is not kept. - The server compiles up to 1.8 MB of source, which covers small and mid-size libraries. For a larger project, maplibre-gl-js among them, it answers with status 422 in a second or two. A browser has no such limit. - Setup commands of a project (code generators) do not run on the server. For such a project (`skippedSetup` is not empty) only problems in the demo's own files are reported. A browser runs the setup and gives the full result. ## Reading the result in a browser Any headless browser can open a link. The page says how it went: - `document.documentElement.dataset.livebranch` is `loading`, `ready` or `failed`. - `window.__livebranch.state.session` has `problems` (from the compile), `steps`, `url` (the running page) and `label`. - `window.__livebranch.state.typeProblems` has the type errors. They are final when `state.checking` is false. - `window.__livebranch.state.console` has what the page logged, and its uncaught errors. - In a comparison, `window.__livebranch.state.base` is the build of the other ref and `state.baseConsole` has what its page logged. The app shows the two consoles side by side. - `window.__livebranch.exec('grep -rn foo src')` runs a shell command over the project's files (bash, with `tsc` and `node`). - `window.__livebranch.edit('/demo.ts', text)` changes a file and rebuilds. `window.__livebranch.link()` returns the link with the edits in it, and `link(true, true)` the link of the previews alone at the commits that run. - `window.__livebranch.block()` returns the demo as code blocks for the description of a pull request. - `window.__livebranch.state.pull` has what GitHub says about an opened pull request: `title`, `base`, and `files` with the demo of its description. ## Which projects work - A plain TypeScript or TSX library with a `src/index.ts` works with no config. - A project with build steps says how it runs in a `livebranch.json`, at its root or in `.github`. A project without one gets its adapter, which is the same file kept at https://github.com/birkskyum/livebranch-adapters under the name of its npm package. That repository documents the fields. - Solid 2 works. Its JSX compiles with Solid's own compiler, and an app in the client mode of `@solidjs/vite-plugin`, which has no `index.html`, gets the page that the plugin makes: the head of `src/Document.tsx` and `src/App.tsx` rendered into the body. `/api/check` and the command line compile its JSX as React's, which is enough to check it. - A project that needs another bundler plugin or framework compiler (Vue, Svelte, Solid 1) does not work. ## Limits - The repository has to be public. - What runs is the source of the ref, compiled file by file. It is not the project's own bundle, so it does not show bundle size, bundler behaviour or load performance. - The site reads branches and file lists through GitHub's git endpoint, which has no hourly allowance. Gists and abbreviated commit hashes go through GitHub's API, where an anonymous visitor gets 60 requests an hour.