cmux/web/app/[locale]/docs/browser-automation/page.tsx
Lawrence Chen 46589f531c
Fix SEO indexing: hreflang, canonicals, sitemap, trailing slash (#2193)
* Fix SEO indexing: add hreflang, canonicals, sitemap per-locale entries

Google Search Console showed 380 not-indexed vs 86 indexed pages.
Root causes: missing hreflang tags on rendered pages (only in sitemap),
no canonical on homepage, inconsistent canonicals wiping parent hreflang,
sitemap only listing English URLs, trailing slash duplicates, and
_next/static chunks being crawled as pages.

Changes:
- Add buildAlternates() utility for consistent canonical + hreflang
- Add hreflang tags to all pages via alternates.languages in metadata
- Add self-referencing canonical URLs to every page (homepage had none)
- Expand sitemap to emit separate entries for each locale
- Add missing /docs/custom-commands to sitemap
- Remove skipTrailingSlashRedirect to normalize trailing slashes
- Block /_next/ in robots.txt to stop chunk crawling

* Add per-page alternates to docs sub-pages and blog index

Docs sub-pages and blog index only returned title/description in
generateMetadata, so they inherited the parent layout's alternates
(pointing to /docs or /blog). Now each page sets its own
buildAlternates() with the correct path so canonical and hreflang
point to the actual page URL.

* Derive openGraph.url from buildAlternates to avoid drift

* Redirect non-English legal pages to English, remove from sitemap

Legal pages (privacy policy, TOS, EULA) are untranslated English content.
Serving them under every locale creates 54 duplicate URLs. Now:
- Middleware 301-redirects /ja/privacy-policy etc. to /privacy-policy
- Sitemap only includes English URLs for legal pages (no locale variants)
- Legal page metadata uses static English-only canonical

* Fix legal page redirect to only match /<locale>/<page> paths

endsWith matched too broadly (e.g. /docs/eula). Now only redirects
when the path after the first segment is an exact legal page match.

* Skip next-intl for legal pages to prevent locale redirect loop

Without this, a Japanese user hitting /privacy-policy could be
redirected by next-intl to /ja/privacy-policy, which our middleware
redirects back to /privacy-policy, creating a loop.

* Rewrite legal pages to /en/ instead of NextResponse.next()

Pages live under app/[locale]/, so skipping next-intl entirely
would break route resolution. Rewrite to /en/privacy-policy etc.
so Next.js can resolve the [locale] segment correctly.

---------

Co-authored-by: Lawrence Chen <lawrencecchen@users.noreply.github.com>
2026-03-26 15:10:39 -07:00

270 lines
10 KiB
TypeScript

import { useTranslations } from "next-intl";
import { getTranslations } from "next-intl/server";
import { buildAlternates } from "../../../../i18n/seo";
import { CodeBlock } from "../../components/code-block";
export async function generateMetadata({ params }: { params: Promise<{ locale: string }> }) {
const { locale } = await params;
const t = await getTranslations({ locale, namespace: "docs.browserAutomation" });
return {
title: t("metaTitle"),
description: t("metaDescription"),
alternates: buildAlternates(locale, "/docs/browser-automation"),
};
}
export default function BrowserAutomationPage() {
const t = useTranslations("docs.browserAutomation");
return (
<>
<h1>{t("title")}</h1>
<p>{t("intro")}</p>
<h2>{t("commandIndex")}</h2>
<table>
<thead>
<tr>
<th>{t("categoryHeader")}</th>
<th>{t("subcommandsHeader")}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{t("navAndTargeting")}</td>
<td>
<code>identify</code>, <code>open</code>, <code>open-split</code>,{" "}
<code>navigate</code>, <code>back</code>, <code>forward</code>,{" "}
<code>reload</code>, <code>url</code>, <code>focus-webview</code>,{" "}
<code>is-webview-focused</code>
</td>
</tr>
<tr>
<td>{t("waiting")}</td>
<td>
<code>wait</code>
</td>
</tr>
<tr>
<td>{t("domInteraction")}</td>
<td>
<code>click</code>, <code>dblclick</code>, <code>hover</code>,{" "}
<code>focus</code>, <code>check</code>, <code>uncheck</code>,{" "}
<code>scroll-into-view</code>, <code>type</code>, <code>fill</code>,{" "}
<code>press</code>, <code>keydown</code>, <code>keyup</code>,{" "}
<code>select</code>, <code>scroll</code>
</td>
</tr>
<tr>
<td>{t("inspection")}</td>
<td>
<code>snapshot</code>, <code>screenshot</code>, <code>get</code>,{" "}
<code>is</code>, <code>find</code>, <code>highlight</code>
</td>
</tr>
<tr>
<td>{t("jsAndInjection")}</td>
<td>
<code>eval</code>, <code>addinitscript</code>, <code>addscript</code>,{" "}
<code>addstyle</code>
</td>
</tr>
<tr>
<td>{t("framesDialogsDownloads")}</td>
<td>
<code>frame</code>, <code>dialog</code>, <code>download</code>
</td>
</tr>
<tr>
<td>{t("stateAndSession")}</td>
<td>
<code>cookies</code>, <code>storage</code>, <code>state</code>
</td>
</tr>
<tr>
<td>{t("tabsAndLogs")}</td>
<td>
<code>tab</code>, <code>console</code>, <code>errors</code>
</td>
</tr>
</tbody>
</table>
<h2>{t("targetingSurface")}</h2>
<p>{t("targetingDesc")}</p>
<CodeBlock lang="bash">{`# Open a new browser split
cmux browser open https://example.com
# Discover focused IDs and browser metadata
cmux browser identify
cmux browser identify --surface surface:2
# Positional vs flag targeting are equivalent
cmux browser surface:2 url
cmux browser --surface surface:2 url`}</CodeBlock>
<h2>{t("navigation")}</h2>
<CodeBlock lang="bash">{`cmux browser open https://example.com
cmux browser open-split https://news.ycombinator.com
cmux browser surface:2 navigate https://example.org/docs --snapshot-after
cmux browser surface:2 back
cmux browser surface:2 forward
cmux browser surface:2 reload --snapshot-after
cmux browser surface:2 url
cmux browser surface:2 focus-webview
cmux browser surface:2 is-webview-focused`}</CodeBlock>
<h2>{t("waitingSection")}</h2>
<p>{t("waitingDesc")}</p>
<CodeBlock lang="bash">{`cmux browser surface:2 wait --load-state complete --timeout-ms 15000
cmux browser surface:2 wait --selector "#checkout" --timeout-ms 10000
cmux browser surface:2 wait --text "Order confirmed"
cmux browser surface:2 wait --url-contains "/dashboard"
cmux browser surface:2 wait --function "window.__appReady === true"`}</CodeBlock>
<h2>{t("domSection")}</h2>
<p>{t("domDesc")}</p>
<CodeBlock lang="bash">{`cmux browser surface:2 click "button[type='submit']" --snapshot-after
cmux browser surface:2 dblclick ".item-row"
cmux browser surface:2 hover "#menu"
cmux browser surface:2 focus "#email"
cmux browser surface:2 check "#terms"
cmux browser surface:2 uncheck "#newsletter"
cmux browser surface:2 scroll-into-view "#pricing"
cmux browser surface:2 type "#search" "cmux"
cmux browser surface:2 fill "#email" --text "ops@example.com"
cmux browser surface:2 fill "#email" --text ""
cmux browser surface:2 press Enter
cmux browser surface:2 keydown Shift
cmux browser surface:2 keyup Shift
cmux browser surface:2 select "#region" "us-east"
cmux browser surface:2 scroll --dy 800 --snapshot-after
cmux browser surface:2 scroll --selector "#log-view" --dx 0 --dy 400`}</CodeBlock>
<h2>{t("inspectionSection")}</h2>
<p>{t("inspectionDesc")}</p>
<CodeBlock lang="bash">{`cmux browser surface:2 snapshot --interactive --compact
cmux browser surface:2 snapshot --selector "main" --max-depth 5
cmux browser surface:2 screenshot --out /tmp/cmux-page.png
cmux browser surface:2 get title
cmux browser surface:2 get url
cmux browser surface:2 get text "h1"
cmux browser surface:2 get html "main"
cmux browser surface:2 get value "#email"
cmux browser surface:2 get attr "a.primary" --attr href
cmux browser surface:2 get count ".row"
cmux browser surface:2 get box "#checkout"
cmux browser surface:2 get styles "#total" --property color
cmux browser surface:2 is visible "#checkout"
cmux browser surface:2 is enabled "button[type='submit']"
cmux browser surface:2 is checked "#terms"
cmux browser surface:2 find role button --name "Continue"
cmux browser surface:2 find text "Order confirmed"
cmux browser surface:2 find label "Email"
cmux browser surface:2 find placeholder "Search"
cmux browser surface:2 find alt "Product image"
cmux browser surface:2 find title "Open settings"
cmux browser surface:2 find testid "save-btn"
cmux browser surface:2 find first ".row"
cmux browser surface:2 find last ".row"
cmux browser surface:2 find nth 2 ".row"
cmux browser surface:2 highlight "#checkout"`}</CodeBlock>
<h2>{t("jsSection")}</h2>
<CodeBlock lang="bash">{`cmux browser surface:2 eval "document.title"
cmux browser surface:2 eval --script "window.location.href"
cmux browser surface:2 addinitscript "window.__cmuxReady = true;"
cmux browser surface:2 addscript "document.querySelector('#name')?.focus()"
cmux browser surface:2 addstyle "#debug-banner { display: none !important; }"`}</CodeBlock>
<h2>{t("stateSection")}</h2>
<p>{t("stateDesc")}</p>
<CodeBlock lang="bash">{`cmux browser surface:2 cookies get
cmux browser surface:2 cookies get --name session_id
cmux browser surface:2 cookies set session_id abc123 --domain example.com --path /
cmux browser surface:2 cookies clear --name session_id
cmux browser surface:2 cookies clear --all
cmux browser surface:2 storage local set theme dark
cmux browser surface:2 storage local get theme
cmux browser surface:2 storage local clear
cmux browser surface:2 storage session set flow onboarding
cmux browser surface:2 storage session get flow
cmux browser surface:2 state save /tmp/cmux-browser-state.json
cmux browser surface:2 state load /tmp/cmux-browser-state.json`}</CodeBlock>
<h2>{t("tabsSection")}</h2>
<p>{t("tabsDesc")}</p>
<CodeBlock lang="bash">{`cmux browser surface:2 tab list
cmux browser surface:2 tab new https://example.com/pricing
# Switch by index or by target surface
cmux browser surface:2 tab switch 1
cmux browser surface:2 tab switch surface:7
# Close current tab or a specific target
cmux browser surface:2 tab close
cmux browser surface:2 tab close surface:7`}</CodeBlock>
<h2>{t("consoleSection")}</h2>
<CodeBlock lang="bash">{`cmux browser surface:2 console list
cmux browser surface:2 console clear
cmux browser surface:2 errors list
cmux browser surface:2 errors clear`}</CodeBlock>
<h2>{t("dialogsSection")}</h2>
<CodeBlock lang="bash">{`cmux browser surface:2 dialog accept
cmux browser surface:2 dialog accept "Confirmed by automation"
cmux browser surface:2 dialog dismiss`}</CodeBlock>
<h2>{t("framesSection")}</h2>
<CodeBlock lang="bash">{`# Enter an iframe context
cmux browser surface:2 frame "iframe[name='checkout']"
cmux browser surface:2 click "#pay-now"
# Return to the top-level document
cmux browser surface:2 frame main`}</CodeBlock>
<h2>{t("downloadsSection")}</h2>
<CodeBlock lang="bash">{`cmux browser surface:2 click "a#download-report"
cmux browser surface:2 download --path /tmp/report.csv --timeout-ms 30000`}</CodeBlock>
<h2>{t("commonPatterns")}</h2>
<h3>{t("patternNavigate")}</h3>
<CodeBlock lang="bash">{`cmux browser open https://example.com/login
cmux browser surface:2 wait --load-state complete --timeout-ms 15000
cmux browser surface:2 snapshot --interactive --compact
cmux browser surface:2 get title`}</CodeBlock>
<h3>{t("patternForm")}</h3>
<CodeBlock lang="bash">{`cmux browser surface:2 fill "#email" --text "ops@example.com"
cmux browser surface:2 fill "#password" --text "$PASSWORD"
cmux browser surface:2 click "button[type='submit']" --snapshot-after
cmux browser surface:2 wait --text "Welcome"
cmux browser surface:2 is visible "#dashboard"`}</CodeBlock>
<h3>{t("patternDebug")}</h3>
<CodeBlock lang="bash">{`cmux browser surface:2 console list
cmux browser surface:2 errors list
cmux browser surface:2 screenshot --out /tmp/cmux-failure.png
cmux browser surface:2 snapshot --interactive --compact`}</CodeBlock>
<h3>{t("patternSession")}</h3>
<CodeBlock lang="bash">{`cmux browser surface:2 state save /tmp/session.json
# ...later...
cmux browser surface:2 state load /tmp/session.json
cmux browser surface:2 reload`}</CodeBlock>
</>
);
}