MonetizationOS Docs
Proxies Reference

WordPress VIP Edge Worker Proxy

The WordPress VIP Edge Worker Proxy is an open source implementation of the MonetizationOS proxy that runs as a WordPress VIP edge worker. To deploy it, follow the WordPress VIP Edge Worker Proxy guide.

View on GitHub

VIP edge workers are AssemblyScript compiled to WebAssembly, so this worker can't use @monetizationos/proxy. It's a port of that pipeline, and the project's DIVERGENCES.md lists every intentional difference.

Environment variables

The worker reads its variables from the edge worker's environment.

VariableRequiredDescription
MOS_SECRET_KEYYesYour MonetizationOS secret key. Used to authenticate Surface Decision API requests and to derive the environment prefix for custom endpoint routing.
MOS_HOSTYesThe MonetizationOS API host, for example https://api.monetizationos.com.
MOS_SURFACE_SLUGYesThe MonetizationOS surface to evaluate for every HTML page.
MOS_JWT_COOKIENoCookie name containing the authenticated user's JWT. Defaults to mos_user_jwt.
MOS_ANON_COOKIENoCookie name for anonymous session identifiers. Defaults to mos_anon_id.
MOS_ANON_FALLBACKNoSet to false to stop creating an anonymous identifier when the reader has none. Defaults to true.
MOS_ENDPOINTS_PREFIXNoURL prefix for custom MOS endpoint routing. Defaults to /mos-endpoints/.
MOS_ENVIRONMENTNoOverrides the environment segment for custom endpoint routing, which is otherwise derived from MOS_SECRET_KEY.
MOS_AUTHENTICATED_ROUTESNoComma-separated METHOD /match>/apiPath routes, for POST, PUT, or PATCH. See Authenticated routes.
MOS_IGNORE_PATTERNSNoSpace-separated regex patterns for paths that should skip surface decisions. A malformed pattern makes page requests fail.
MOS_IGNORE_PREFIXESNoComma-separated path prefixes that should skip surface decisions.
MOS_SURFACE_DECISIONS_COOKIESNoSpace-separated regex patterns for cookie names sent to the Surface Decision API as http.cookies. When a name appears in both the request and the origin response's Set-Cookie headers, the origin's value is used.
MOS_DECISIONS_PERCENTNoThe share of readers, from 0 to 100, whose pages get a surface decision. Defaults to 100. See Rolling out gradually.

If MOS_SECRET_KEY, MOS_HOST, or MOS_SURFACE_SLUG is missing, the worker passes every request through unchanged.

Pattern lists are space-separated, where the other proxies use commas.

Request pipeline

The worker runs in two edge worker phases, onClientRequest before VIP's cache and onClientResponse after it:

YesNoNoYesNoYesClient requestCustom MOS endpointor authenticated route?Return MOS API responseVIP cache, or WordPressorigin on a missUncompressed HTML,not a redirect or ignoredpath?Serve the page as cachedSend the page with theSurface Decision APIrequestDecisionreturned?Apply surface behaviorheaders / cookies / statusReturn the transformedpagewith Cache-Control: private,no-store

The worker doesn't rewrite HTML itself. It sends the page with the Surface Decision API request, and MonetizationOS applies the surface's Component Behaviors and returns the transformed page. This needs a secret key and whole-body mode enabled for your organization.

A surface behavior that sets a response body replaces the page. Otherwise the reader gets the transformed page, or the cached page when no transform applies.

Caching

VIP caches the page as WordPress sent it, and the worker transforms each reader's copy on the way out. Your origin renders a page once per cache lifetime, and every page view makes one Surface Decision API call. Transformed responses are marked Cache-Control: private, no-store, so a reader's copy never enters a shared cache.

Compression

The worker needs the page text, so it asks for an uncompressed response on requests that look like pages. A response that still arrives compressed is served without a decision.

Skipped paths

The worker never calls MonetizationOS for WordPress paths that aren't reader pages, in addition to MOS_IGNORE_PATTERNS and MOS_IGNORE_PREFIXES:

  • /wp-admin/, /wp-login.php, /wp-json/, /xmlrpc.php, /wp-cron.php
  • /wp-includes/, /wp-content/
  • Feeds, such as /feed/ and /comments/feed/

Identity resolution

The worker resolves the reader's identity from cookies, as the other proxies do:

  • Authenticated users: identified by a JWT from the MOS_JWT_COOKIE cookie.
  • Anonymous users: identified by a session ID from the MOS_ANON_COOKIE cookie. If neither cookie is present, a new anonymous session ID is generated and set with Set-Cookie.

Cookies that WordPress sets in its response take precedence over the request's.

The reader's IP address is taken from the first X-Forwarded-For hop. The worker sends no viewer location, so location-based decisions see the edge server's location.

Rolling out gradually

MOS_DECISIONS_PERCENT limits surface decisions to a share of readers. Each reader gets a random token in a mos_bucket cookie, which keeps them in or out of the share across visits. Readers without cookies, such as most crawlers, are bucketed per request. Readers outside the share get the page as cached.

Custom endpoint routing

Requests whose path begins with MOS_ENDPOINTS_PREFIX (default /mos-endpoints/) are forwarded directly to the MonetizationOS API as Endpoint Workflow requests, with the client's method, body, and headers. The worker sets x-mos-key to your key and adds the reader's identity:

/mos-endpoints/my-route → https://api.monetizationos.com/api/v1/envs/{env}/endpoints/my-route

If the API can't be reached, the worker answers 502.

Authenticated routes

POST /mos-api/offer-redemptions, /mos-api/access-checks, /mos-api/surface-decisions, and /mos-api/counter-updates are always routed to the matching /api/v1/ API path. MOS_AUTHENTICATED_ROUTES adds routes ahead of these. The worker merges the client's JSON body with the reader's identity before calling the API.

Resource fields

The resource sent to the Surface Decision API is { id: path } by default. To add or override fields for your site, edit buildResource in assembly/resource.ts and rebuild. Values are strings only.

Error handling

If the Surface Decision API call fails, the reader gets the page as cached. If a transform fails after MonetizationOS has started returning the page, the response body errors, and VIP serves the page as it was.

On this page