# Next.js (App Router)

## What this is

A `/contact` page for the Next.js App Router. The page is a server component; the form is a client component that posts JSON **directly from the browser** to ContactUsIQ, so no API route, server action or secret is involved and the page works with static export and edge caching.

Also included: an optional one-line-style proxy at `app/api/enquiry/route.ts` for sites that want to keep an existing server URL. It is not needed for the direct flow.

Files: [`app/contact/page.tsx`](app/contact/page.tsx), [`app/contact/ContactForm.tsx`](app/contact/ContactForm.tsx), [`app/api/enquiry/route.ts`](app/api/enquiry/route.ts) (optional), [`package.json`](package.json) (versions only; nothing is installed for the sample).

This sample uses the placeholder form id `au_xxxxxxx` and the Turnstile site key placeholder `0x4AAAAAAA_PLACEHOLDER`. Replace both (or set the environment variables below).

## Prerequisites

- Next.js 14 or 15 with the App Router, React 18 or 19.
- A ContactUsIQ workspace with a verified domain and a published form.
- The site served on the verified domain (CORS preflight for JSON is answered for verified domains only).

## Steps

1. Copy `app/contact/page.tsx` and `app/contact/ContactForm.tsx` into your `app/` folder.
2. Set `NEXT_PUBLIC_CONTACTUSIQ_FORM_ID=au_xxxxxxx` and `NEXT_PUBLIC_TURNSTILE_SITE_KEY=0x4AAAAAAA_PLACEHOLDER` in `.env.local` (or hard-code them in `page.tsx`). Leave the Turnstile key unset to submit without Turnstile.
3. Deploy. Visit `/contact` on the verified domain.
4. (Optional) To post through your own server instead, copy `app/api/enquiry/route.ts`, set `CONTACTUSIQ_FORM_ID` in the server environment, and change `INTAKE_BASE + formId` in `ContactForm.tsx` to `'/api/enquiry'`.

## The code

- `page.tsx`: server component that renders the client form with the form id and Turnstile key from `NEXT_PUBLIC_*` env vars.
- `ContactForm.tsx` (`'use client'`): `idle` / `sending` / `sent` / `error` states; `next/script` loads Turnstile with `?render=explicit` and the widget is rendered into a `ref` once the script reports `onLoad`; the honeypot `_honeypot` is an off-screen text input; the submit `fetch`es `https://api.contactusiq.com/s/{formId}` with `Content-Type: application/json` and `Accept: application/json`; `200` -> thank-you with `id`, `400` -> the server's `error`, `429` -> `Retry-After` seconds. Turnstile is reset after every attempt.
- `route.ts`: an edge route that forwards the request body and content type to the intake URL and returns the upstream status, body and `Retry-After` unchanged. It also forwards `Origin`, `X-Forwarded-For` and `User-Agent` so origin checks and rate limits apply to the visitor, not to your server. This is what an existing `/api/enquiry` route becomes: a pass-through with no mail sending, no CAPTCHA verification and no secret of its own.

## How to test

1. `npm run dev` in your project and open `http://localhost:3000/contact`. Note that `localhost` is not a verified domain, so for the direct flow either enable "any origin" on the form for development or test on a deployed preview whose domain is verified.
2. Fill the form, complete Turnstile, click Send. Network tab: `OPTIONS` then `POST https://api.contactusiq.com/s/au_xxxxxxx` -> `200` `{"ok":true,"id":"sub_..."}`.
3. With the proxy: the browser posts to `/api/enquiry` (same origin, no preflight) and the route returns the same `200` / `400` / `429`.

## Troubleshooting

- **CORS error in the browser.** The page origin is not verified on the form. Verify the domain or use the proxy route during local development (same-origin requests need no preflight).
- **200 but nothing in the inbox.** Silent origin or spam rejection. With the proxy, make sure `Origin` is forwarded (the route does this) and that the `Origin` value is a verified domain.
- **Turnstile does not appear.** The `Script` `onLoad` fires only once per page load; on client-side navigation back to the page the widget is re-rendered by the effect because `window.turnstile` is already present and `turnstileReady` is true. If you see nothing, check the site key and that the domain is listed on the Turnstile widget in Cloudflare.
- **429.** 5 per IP per form per 10 minutes. With the proxy, if `X-Forwarded-For` were not forwarded every visitor would share your server's IP budget; the route forwards it.

## For AI agents

Add a ContactUsIQ contact page to a Next.js App Router project by copying `app/contact/page.tsx` and `app/contact/ContactForm.tsx`, then setting `NEXT_PUBLIC_CONTACTUSIQ_FORM_ID` and `NEXT_PUBLIC_TURNSTILE_SITE_KEY`. The client component posts JSON straight to `https://api.contactusiq.com/s/{formId}` with `Accept: application/json` and handles `200`, `400` and `429` (`Retry-After`). No API route is required; if the project already has a server route such as `/api/enquiry`, replace its body with the pass-through in `app/api/enquiry/route.ts` and point the component at `/api/enquiry`.

```tsx
// app/contact/page.tsx
import ContactForm from './ContactForm';
export default function ContactPage() {
  return <ContactForm formId="au_xxxxxxx" turnstileSiteKey="0x4AAAAAAA_PLACEHOLDER" />;
}
```
