> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# ネットワークとエッジ

> ウェブサイトアプリケーションのカスタムドメイン、DNS、エッジ分析、エラー追跡、リクエストログ、レイテンシーパーセンタイル、キャッシュの無効化を管理します。

`network` モジュールは **ウェブサイトアプリケーション** でのみ利用できます。まず `app.isWebsite()` で型を絞り込んでください。

```typescript theme={null}
const app = await api.applications.fetch("abc123def456abc123def456");

if (!app.isWebsite()) {
    throw new Error("This application is not a website");
}

app.network; // NetworkModule
```

## カスタムドメイン

`app.network.setCustomDomain(domain)` は、ウェブサイトに紐付けられたカスタムドメインを設定または削除します。

```typescript theme={null}
await app.network.setCustomDomain("yoursite.com");

// Pass "@" to remove the configured custom domain
await app.network.setCustomDomain("@");
```

<Note>
  カスタムドメインには [Senior プラン](https://squarecloud.app/plans) 以上が必要です。
</Note>

## DNS レコード

カスタムドメインを紐付けたら、レジストラで DNS を設定する必要があります。`app.network.dns()` は所有権レコードと SSL のステータスを返します。

```typescript theme={null}
const dns = await app.network.dns();

console.log(`${dns.ownership.type} ${dns.ownership.name} → ${dns.ownership.value}`);
console.log(`SSL: ${dns.ssl.status}`); // "pending" | "pending_validation" | "active"
```

<Warning>
  v4 で形式が変更されました。以前の `app.network.dns()` はレコードの配列を返していましたが、現在は単一のオブジェクト `{ ownership, ssl }` を返します。
</Warning>

## 分析

`app.network.analytics({ start, end })` は集計されたエッジ分析を返します。`start` と `end` は ISO 8601 文字列または `Date` オブジェクトのいずれかを受け付けます。最大保持期間は **7 日間** です。

```typescript theme={null}
const since = new Date(Date.now() - 24 * 60 * 60 * 1000);

const analytics = await app.network.analytics({
    start: since,
    end: new Date(),
});

if ("visits" in analytics) {
    console.log(analytics.visits.length, "time buckets");
    console.log("Top countries:", analytics.countries.slice(0, 5));
    console.log("Top paths:",     analytics.paths.slice(0, 5));
}
```

<Warning>
  データが空の期間（例: アプリ作成前）に対しては、API は `{}` を返します。読み取る前に `"visits" in analytics` でガードしてください。
</Warning>

<Warning>
  v3 ではこのエンドポイントを引数なしで呼び出していました。**v4 では `{ start, end }` が必須です。**
</Warning>

## エラー追跡

`app.network.errors({ start, end, include4xx? })` はエッジのエラー内訳を返します。デフォルトでは 5xx エラーのみが含まれます。

```typescript theme={null}
const errors = await app.network.errors({
    start: since,
    end: new Date(),
    include4xx: true, // include 4xx alongside 5xx
});

if ("summary" in errors) {
    console.log(`Total: ${errors.summary.total}`);
    console.log("By class:",          errors.summary.by_class);
    console.log("Top failing paths:", errors.top_paths.slice(0, 3));
}
```

## リクエストごとのログ

`app.network.logs({ start, end })` はリクエストごとのエッジログを返します。

```typescript theme={null}
const logs = await app.network.logs({ start: since, end: new Date() });

for (const log of logs.slice(0, 5)) {
    console.log(
        log.timestamp,
        log.request.method, log.request.path,
        "→",
        log.response.status,
        `(${log.client.country ?? "??"})`,
    );
}
```

<Note>
  リクエストごとのログには [Pro プラン](https://squarecloud.app/plans) 以上が必要です。
</Note>

## レイテンシーパーセンタイル

`app.network.performance({ start, end })` は、エッジ層とオリジン層の p50 / p95 / p99 レイテンシーを返します。

```typescript theme={null}
const perf = await app.network.performance({ start: since, end: new Date() });

if ("summary" in perf) {
    const { p50, p95, p99 } = perf.summary.edge;
    console.log(`Edge p50/p95/p99: ${p50}/${p95}/${p99}ms`);
    console.log("Slowest paths:", perf.slowest_paths.slice(0, 3));
}
```

<Note>
  パフォーマンス指標には [Pro プラン](https://squarecloud.app/plans) 以上が必要です。
</Note>

## エッジキャッシュのパージ

`app.network.purgeCache()` は、アプリケーションのドメインに対するエッジキャッシュ全体を無効化します。

```typescript theme={null}
await app.network.purgeCache();
```

<Warning>
  v3（および短命だった v4.0.0）では、`purgeCache(paths?)` はパージ対象のパスのリストを受け取り、選択的にパージできました。**v4.0.1 以降、この引数は削除され**、`purgeCache()` を呼び出すと常にキャッシュ全体がパージされます。
</Warning>
