Frontend Development Guidelines

Prerequisites

Frontend for Remark42 is written in TypeScript with no framework. Most parts of the widget are modules that draw into an element they are given and return update, which brings them up to date with new props, and destroy, which takes their timers and listeners away; the root draws itself again from the state. The state is one plain object, app/state/state.ts, changed by plain functions that draw the widget again, and the i18n helpers are small local modules under app/ too. No entry imports a package from node_modules.

In order to inject Remark42 widgets into websites we use iframe and postMessage for communication between a site and the widget. Simple widgets like counter widget can be injected as a script because it doesn't have its own interface.

While developing, we set up environment which imitates real world example. We serve the page which uses Remark42 config and inject all the widgets on it. You can check it on our demo site. After successful installation you should have the same page running locally.

Installation

You must have at least 2GB RAM or swap enabled for building.

  • install Go, the release named in frontend/build/go.mod or a newer one: the widget is compiled by a Go program
  • install Node.js 26 or higher (we recommend using NVM for node version autoswitch) for the translation scripts and the dependency-free tests, which use no packages
  • install Docker for the source checks, which install their packages inside an image

No package is installed on your machine: npm runs only inside the checks image, described under Code Style.

The repository installs no git hooks. A clone where an earlier pnpm i set them up still has core.hooksPath pointing at frontend/.husky, where nothing runs any more; git config --unset core.hooksPath removes the setting, and the leftover frontend/.husky directory can be deleted.

🚨 Please use 127.0.0.1 and not localhost to access the server; otherwise, CORS will prevent your browser from authentication to work correctly. The address dev auth uses is the backend's REMARK_URL, set in the compose file you start the backend from.

Development

Run frontend with remote backend

You can run frontend against demo instance of Remark42. This method of running Remark42 frontend code is preferred when you make a translation or visual adjustments that are easy to see without extensive testing. For this method we use our demo instance of Remark42 served on https://demo.remark42.com

For local development, run go -C ../../build run . -serve -api-base=https://demo.remark42.com from the frontend/apps/remark42 folder. It builds the widget, builds it again whenever a source file changes, and serves it on 127.0.0.1:9000. By visiting http://127.0.0.1:9000/web/, you will get a page with the main comments' widget communicating with a demo server backend running on https://demo.remark42.com. But you will not be able to log in with any OAuth providers due to security reasons. Reload the page to see a change; there is no hot reloading.

go -C ../../build run . -serve, run from the frontend/apps/remark42 folder, attaches the frontend to a backend running locally on http://127.0.0.1:8080. To use another backend, pass its address:

go -C ../../build run . -serve -api-base=http://127.0.0.1:8081

The REMARK_API_BASE_URL environment variable sets the same address.

ℹ️ To serve on another port, pass the address, for example go -C ../../build run . -serve -addr=127.0.0.1:9001, or set the PORT environment variable. A flag takes precedence over its variable.

Run frontend with backend locally

This option of running Remark42 frontend code is preferred when you need extensive testing of your code changes, as you'll have your backend and configure it as you want, for example, enable any auth and notifications method you need to test. You can use that set up to develop and test both frontend and backend.

To bring the backend up, run:

cp compose-dev-frontend.yml compose-private.yml
# now, edit / debug `compose-private.yml` to your heart's content

# build and run
docker compose -f compose-private.yml up --build

It starts Remark42 backend on 127.0.0.1:8080 and adds local OAuth2 provider "Dev". Sign in with the Dev provider: its form offers dev_user, which the compose file makes an admin. You can tweak any of the supported parameters in the corresponding yml file.

Then in the new terminal tab or window, run the following to start the frontend, which is built again on every change:

cd frontend/apps/remark42
go -C ../../build run . -serve

To access the frontend served this way, go to http://127.0.0.1:9000/web/. The widget keeps its state in one plain object, state in app/state/state.ts, and every change of it goes through change() in the same file, so a breakpoint there stops on each one.

Manual testing after changes

Frontend Docker Compose config (compose-dev-frontend.yml) by default skips running backend related tests and the frontend build.

🚨 Before submitting your changes as a Pull Request, run the backend using the docker compose -f compose-private.yml build --build-arg SKIP_FRONTEND_BUILD="" && docker compose -f compose-private.yml up command and test your changes against http://127.0.0.1:8080/web/, frontend, built statically (unlike frontend on port 9000, which runs dynamically). That is how Remark42 authors will test your changes once you submit them.

Static build

Remark42 frontend can be built statically, and that's how the production version works: frontend is built and then resulting files embedded into the backend, which serves them as-is. Node is not running when a user starts Remark42, only the backend written in Go programming language, which also serves pre-built frontend HTML and JS and CSS files.

Run go -C ../../build run . inside ./frontend/apps/remark42, and result files will be saved in ./frontend/apps/remark42/public. The build is the Go program in ./frontend/build, which compiles the widget with esbuild; go run . in that directory does the same. The browsers the scripts and styles are written for are listed there, in build.go.

/web is served from two sources. This build output comes first; anything it does not emit is served from backend/app/webassets/assets, embedded in the backend binary, which is where privacy.html, markdown-help.html, 400x400.jpeg, the ghost avatar, the sign-in provider icons, the emoji table emoji.json, the message catalogues <locale>.json, the widget document iframe.html, the demo, counter, last-comments, deleteme and comments pages and the counter.mjs script live. A plain page or image the bundler does not process belongs there rather than here. Those files sit outside the frontend toolchain, so prettier, stylelint and eslint do not see them.

Code Style

  • The project uses TypeScript to analyze code statically
  • The last-comments entry, app/last-comments.ts with app/components/list-comments/list-comments.ts, imports no package and builds its elements with the DOM API
  • The type check, Eslint, Stylelint and the prettier check run with make frontend-checks from the repository root. It builds an image from frontend/apps/remark42/Dockerfile.checks, installs the packages inside it, and runs the checks on a copy of the widget; make frontend-checks CHECKS="types lint" runs some of them (types, lint, styles, format). CI runs the same image on every pull request
  • make frontend-format runs prettier, eslint --fix and stylelint --fix in the same image and writes the changed files back into frontend/apps/remark42
  • If you want IDE integration, you need Eslint and Stylelint plugins, and the packages installed for your editor to find. Nothing in the project needs them on your machine. Eslint reads eslint.config.mjs from the directory it is run in, so point your editor at the app directory: for VSCode, "eslint.workingDirectories": ["frontend/apps/remark42"]

CSS Styles

  • Component styles are plain CSS with fixed class names: the build renames nothing, so the name in the stylesheet is the name in the page.
  • Keep the stylesheet next to its component, for example app/components/select/select.css and app/components/select/select-element.ts, and add it to the list in app/remark.css. The build joins the files in the order of that list. The last comments widget has a list of its own, app/last-comments.css.
  • Start every class with the component's name: select for the block, select-arrow for an element, select_focused for a modifier. Class names that are older than this scheme stay as they are.
  • In a script a class name is a string, written out whole; combine conditional classes with classes() from app/common/dom.ts.
  • app/styles/class-names.unit.test.ts fails when two stylesheets have the same class or the same animation, when a script writes a class no stylesheet has where a class goes (className, class, mix, a variable whose name ends in Class, classes(), classList, the helpers that take a class such as preloaderElement(), a component's KIND/SIZE/VARIANT tables, the list element() takes in last comments), when no script puts a class of a component stylesheet on an element in one of those places, and when a stylesheet is on neither list.

Imports

  • Imports for TypeScript files should be without extension: ./index, not ./index.ts
  • The exceptions are the *.unit.test.ts files, the last-comments entry and the components: their imports are relative and name the extension, as in ./common/direction.ts
  • If the file resides in the same directory or subdirectory, the import should be relative: ./types/something
  • Otherwise, it should be imported by absolute path relative to the app folder like common/types which maps to ./app/common/types.ts in the build and tsconfig
  • The extension rule and the alias rule above are reversed inside the dependency-free test layer described under Testing, and in any module it imports: node's ESM resolver refuses an extensionless specifier and knows nothing of the alias, so those files write ./types.ts in full and never common/…. The build and tsc both resolve the explicit relative form too, which is why the exception costs nothing outside those files. It applies to value imports only; a type-only import erases before either runtime sees it, and keeps the ordinary style

Testing

The widget's tests come in two layers: the dependency-free tests below, and the end-to-end suite in e2e/, which drives the widget in Chromium, Firefox and WebKit against a running server. A behaviour a reader sees belongs in the end-to-end suite; a function over plain values, or a race between requests, belongs in the dependency-free layer. Nothing runs the tests on commit or push; CI runs them on pull requests and on pushes to master that change code under frontend/.

Dependency-free tests, *.unit.test.ts

Anything that is a function over plain values -- a parser, a reducer, a decoder -- is tested without a framework, a bundler or node_modules. These files import nothing but the module under test, its fixtures and node:test, and the runtime strips the types itself:

node --test "app/**/*.unit.test.ts" "tasks/**/*.unit.test.ts"

It needs nothing you do not already have.

The unit job in ci-frontend.yml runs them with no package install step at all. That missing step is the point: a test that starts needing node_modules to pass has quietly acquired a dependency, and the job fails instead of hiding it. release.yml runs the same command, with no install either, as a second gate.

node --test exits 0 on a glob that matches nothing, so on its own it cannot tell a renamed suffix from a passing run. The CI step counts the files first and fails when none match.

Two rules follow from having no bundler, and both apply to value imports only. A type-only import is erased before node sees the file, so it may keep the alias and drop the extension, which is why import type { Comment } from 'common/types' is correct inside this layer:

  • import with the extension in full, ./types.ts, since node resolves nothing else
  • import by relative path, never by the common/… alias, which only the build and tsc know about

Where a module mixes the two -- a part that both reads sessionStorage and computes something -- split the computation out and test that half here; app/hooks/session-storage.ts is kept apart for exactly this reason, and app/state/loading.ts takes the request it makes as an argument. A module that only reads the page's address, config or localStorage as it loads can be tested as it is, after importing app/tests/browser-globals.ts.

Notes

Frontend part being bundled on docker env gets placed in /srv/web and is available via http://{host}/web. For example, embed.mjs entry point will be available at http://{host}/web/embed.mjs

Learn More