Skip to content
PerspectDocs
Go to Admin
View as Markdown

Newsletter routes on your own Worker

Perspect serves a site's newsletter links from the site's own domain, under /_newsletter/. These include unsubscribe links, the one-click List-Unsubscribe header, and the bounce and complaint webhook (see Bounce and complaint handling).

Sites hosted on Perspect get these routes automatically. If your site runs on its own Cloudflare Worker and uses Perspect as a headless backend, the Worker has to forward them.

Forward /_newsletter/*

Add this at the very top of your Worker's fetch handler, before routing, authentication, server rendering, and static assets:

const url = new URL(request.url);
if (url.pathname.startsWith("/_newsletter/")) {
  const rest = url.pathname.slice("/_newsletter".length);
  return fetch(
    new Request(
      `https://api.perspect.com/newsletter/<site name>${rest}${url.search}`,
      request,
    ),
  );
}

Replace <site name> with your site's name in Perspect.

For example, a request to /_newsletter/unsubscribe/abc?x=1 on your domain goes to https://api.perspect.com/newsletter/<site name>/unsubscribe/abc?x=1.

Requirements

  • Pass every request through unchanged. Keep the method (GET and POST at least), the headers, and the raw body. Amazon SNS posts JSON labeled text/plain, and Perspect verifies the raw body's signature, so don't parse or re-encode it.
  • Return Perspect's response exactly as received: same status, headers, and body. Don't cache it, rewrite it, or redirect.
  • Make sure static assets don't intercept these paths. If the Worker serves static assets with single-page-app fallback, add /_newsletter/* to assets.run_worker_first in your Wrangler config. Otherwise these requests get index.html instead of reaching your code.
  • Leave these paths open. Mail clients and email providers call them anonymously, so the forward must run before any auth, CSRF, or bot-protection middleware.

How Perspect knows your domain forwards

A domain typed into a site's settings proves nothing on its own, and mailing unsubscribe links to a domain that doesn't forward would fail silently. So before Perspect uses your domain, it checks it:

GET https://<your domain>/_newsletter/domain-check/<random nonce>

The forward passes the check to Perspect. Perspect answers with a signature over your site name and the nonce, which only Perspect can produce. If that answer comes back with status 200 and no redirect, the domain counts as forwarding for your site. The result is cached for 10 minutes after a pass and 1 minute after a failure, so a fix shows up quickly.

Until the check passes, Email Settings shows no events URL, and campaigns carry no List-Unsubscribe header.

Domains on Perspect hosting skip this check, because Perspect routes them itself.

Check it yourself

curl -i https://<your domain>/_newsletter/domain-check/AAAAAAAAAAAAAAAAAAAAAAAA

You should get 200 with a 43-character plain-text body.

curl -i -X POST https://<your domain>/_newsletter/email-events -d '{}'

You should get a JSON error from Perspect, not your app's HTML or 404 page.

If either returns HTML, a 404, or a 405, the request isn't reaching the forward. Check the route order and run_worker_first.