# Parity Checks (Native vs Decoupled)


> How ForgeWP checks that one project gives the same result as a WordPress theme and as a separate Next.js app, how to run that check on your computer, and what to do when it fails.


ForgeWP can ship the same React project in two ways:

- **Native**: ForgeWP turns the project into a WordPress theme. WordPress itself shows the pages.
- **Decoupled**: the project runs as its own Next.js app, and reads its content from WordPress over the network.

Visitors should not be able to tell the difference. The same page should show the same text, the same menu and the same data either way. We call this **parity**, and a test called the **parity check** makes sure it stays true. It runs on GitHub for every pull request that changes the compiler or the runtime, so if you contribute to ForgeWP you will meet it sooner or later.

This page explains what the check does, how to run it yourself, and how to read a failure.

**Flowchart**

```mermaid
flowchart LR
  F["projects/parity-fixture<br/>(one small test project)"] --> N["Exported as a theme<br/>WordPress on :8080"]
  F --> D["Built as a Next.js app<br/>next start on :3100"]
  W[("One WordPress database")] --- N
  W --- D
  N --> C{"run-parity.mjs<br/>compares both"}
  D --> C
  C --> R["Report: passed / failed"]
```

> [!NOTE]
> **You don't need this to build themes**
> The parity check tests ForgeWP itself. If you only use ForgeWP to build your own site, you can skip this page.

---

## 1. What gets compared

The test project lives in `projects/parity-fixture`. It is deliberately small: a home page, an about page, a contact form, a blog with two posts, a category page and two pages that need a login. The check runs a set of smaller checks against it, one after the other:

| Check | What it makes sure of |
| :--- | :--- |
| `resources` | The data WordPress puts in a native page (`window.forgeWpHydration.resources`) is exactly what the matching REST address returns. The decoupled app reads that REST address, so both modes start from the same data. |
| `dom` | Text marked with a `data-parity="…"` attribute, and the links in the main menu, are the same on both versions of each page. |
| `leaks` | No page contains leftovers from the build, such as `__FORGEWP_…__` placeholders, `<?php` or a `<forgewp-…>` tag. Native pages must not load Next.js files (`/_next/static`). |
| `form` | The contact form sends successfully in both modes and shows the same message. |
| `auth` | A logged-out visitor who opens a page that needs a login ends up on the same address in both modes. |
| `locale` | The page language (`<html lang>`) and the translation data agree. |
| `commerce` | Only when WooCommerce is installed: the cart, checkout and customer account addresses behave as described in the contract (guest carts, cart tokens, orders, refusing other customers' orders, and so on). |

> [!TIP]
> **Marking text for the `dom` check**
> The `dom` check only compares what you mark. If you add a new feature to the fixture and want it compared, put `data-parity="some-name"` on the element, for example `<ul data-parity="posts">`. Use a name that is unique on that page.

---

## 2. What the check does, step by step

The script `scripts/parity/run.sh` does everything for you:

1. **Starts WordPress.** It runs WordPress, a MariaDB database and Mailpit (a fake mail inbox) in Docker, using `scripts/cloud-e2e/sandbox.sh`. With `PARITY_WOOCOMMERCE=1` it also installs WooCommerce and a small shop with a few products.
2. **Builds the native version.** It exports `projects/parity-fixture` as a theme, copies it into WordPress and activates it. It then adds some sample content: one comment, and a few stored values on the about page.
3. **Waits for WordPress to be ready.** It calls the ForgeWP REST addresses until each one answers with data, so the next step does not build pages from an empty answer.
4. **Builds the decoupled version.** It runs `next build` and `next start` for the same project on port `3100`, pointed at the same WordPress.
5. **Compares.** `scripts/parity/run-parity.mjs` opens both versions in a headless Chromium browser and runs the checks from the table above.
6. **Checks the WordPress error log.** Any PHP fatal error, parse error or warning, or any `[ForgeWP]` warning in `wp-content/debug.log`, also fails the run, even if every comparison passed.

---

## 3. Run it on your computer

### What you need

- **Linux**, or a Linux cloud machine. The script uses `sudo` and Docker. It is not tested on macOS or Windows.
- **Docker** installed and allowed to run.
- **Node.js 22** and **pnpm 10**, with `pnpm install` already run at the root of the ForgeWP repository.
- **Chromium for Playwright**. Install it once with:

**Terminal**

```bash
pnpm --filter @forgewp/parity-fixture exec playwright install --with-deps chromium
```

### Run the full check

From the root of the ForgeWP repository:

**Terminal**

```bash
PARITY_RESET=1 PARITY_WOOCOMMERCE=1 scripts/parity/run.sh
```

This is exactly what GitHub runs. The first run takes several minutes because Docker downloads WordPress and the script builds both versions. At the end you see one line per check and a summary:

**Output**

```text
▸ resources
  122 passed, 0 failed
...
▸ commerce
  23 passed, 0 failed

Parity: 173 passed, 0 failed, 0 known-gap differences

── WordPress debug.log
✓ debug.log clean
```

The exact numbers change as checks are added. What matters is `0 failed` and `✓ debug.log clean`.

### The settings you can change

All settings are environment variables. You put them before the command, as in the example above.

| Setting | What it does | Default |
| :--- | :--- | :--- |
| `PARITY_RESET=1` | Deletes the WordPress database and files and starts from a fresh install. Use it when old content from an earlier run might change the result. | Off: keep the running WordPress |
| `PARITY_WOOCOMMERCE=1` | Installs WooCommerce and the sample shop, and turns on the `commerce` check. | Off |
| `FORGEWP_E2E_DATA` | The folder where WordPress and the database keep their files. | `/home/user/e2e` |
| `PARITY_REPORT` | Where the JSON report is written. | `.forgewp/parity/report.json` |
| `PARITY_NEXT_PORT` | The port for the decoupled Next.js app. | `3100` |

> [!WARNING]
> **`PARITY_RESET=1` deletes that WordPress**
> Reset wipes the WordPress in the `FORGEWP_E2E_DATA` folder: every post, user and setting. It never touches LocalWP or any other site, but don't point `FORGEWP_E2E_DATA` at a folder you want to keep.

### Run only some checks again

Building both versions is the slow part. When WordPress and the decoupled app are still running from a full run, you can repeat just the comparison, and only for the checks you care about:

**Terminal**

```bash
node scripts/parity/run-parity.mjs --only dom,leaks
```

You can list any of `resources`, `dom`, `leaks`, `form`, `auth`, `locale` and `commerce`, separated by commas. Note that `run.sh` stops the Next.js app when it finishes, so start it again first if you need the `dom`, `leaks` or `form` checks. The commands are in step 4 of `run.sh`.

---

## 4. When the check fails

Each failure is printed under its check with the page it happened on and what was different:

**Output**

```text
▸ dom
  ✗ /about/ — native and decoupled DOM differ
      $.parity.about-tagline: "Seeded tagline from WordPress" != "Default tagline"
  6 passed, 1 failed
```

Read it as: on `/about/`, the element marked `data-parity="about-tagline"` showed different text in the two versions. The value on the left of `!=` comes from the native theme, the value on the right from the decoupled app.

Where to look next:

| Failure in | Usually means | Look at |
| :--- | :--- | :--- |
| `resources` | The PHP that prints data into native pages and the REST address disagree. | `packages/compiler/lib/functions/` and `docs/resource-contracts.md` in the ForgeWP repository |
| `dom` | One mode renders a component differently, or reads different data. | The component in `packages/react` and how the compiler turns it into PHP |
| `leaks` | A placeholder was not replaced during export, or a component was not compiled. | The compiler (`packages/compiler/lib/`) |
| `commerce` | A shop address answers differently than the contract says. | `commerce-resources.js` and contract §15 |
| debug.log | WordPress logged a PHP error or warning. | The lines printed above `✗ debug.log has PHP errors` |

> [!TIP]
> **The report has more detail**
> Every run writes `.forgewp/parity/report.json` with every pass and failure. The same folder holds `next-build.log`, `next.log`, the WordPress `debug.log` and the web server log. On GitHub, download them from the run's **parity-report** artifact.

---

## 5. Known gaps

Sometimes a difference is real, already understood, and someone is working on it, but it should not block every other pull request in the meantime. Such a difference can be listed in `scripts/parity/known-gaps.json`. This example is a real entry from earlier in the project, since fixed:

**scripts/parity/known-gaps.json**

```json
{
  "gaps": [
    {
      "id": "R2-native-getposts-frozen",
      "owner": "Who is fixing it",
      "title": "getPosts() outside WpQueryLoop shows the build-time list in the native theme",
      "check": "dom",
      "routes": ["/listing/"],
      "match": "\\$\\.parity\\.posts\\["
    }
  ]
}
```

A failure is treated as a known gap when its check matches `check`, its page is in `routes` (or `routes` is left out) and its message matches `match`, a regular expression (in JSON, each backslash is written twice). Known gaps are still printed, but they do not fail the run.

> [!IMPORTANT]
> **A gap that is fixed fails the run**
> If a listed gap no longer happens, the run fails with `known gap … no longer reproduces`. This is on purpose: it reminds you to delete the entry in the same pull request that fixes it, so the list only ever gets shorter. Today the list is empty, and we would like to keep it that way.

---

## 6. On GitHub

The workflow `.github/workflows/parity.yml` runs the full check (`PARITY_RESET=1 PARITY_WOOCOMMERCE=1`) on a fresh Linux machine:

- for every pull request that changes `packages/react`, `packages/compiler`, `projects/parity-fixture`, `scripts/parity`, `scripts/cloud-e2e` or `pnpm-lock.yaml`,
- for every push to `main` that changes those same folders,
- and whenever someone starts it by hand from the **Actions** tab (**Parity (native vs decoupled)**, then **Run workflow**).

A pull request is ready to merge only when this workflow is green, together with the other checks.

---

Source: [Parity Checks (Native vs Decoupled)](https://www.forgewp.dev/docs/core/contributing/parity-checks)
