Skip to content

Commit 27321bb

Browse files
committed
🔧 Fix cross origin access
1 parent 0f45aa5 commit 27321bb

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

.env.sample

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ MONGO_URI=mongodb://localhost:27017/
66

77
# API の公開 URL(サーバーコンポーネントからの内部アクセス用)
88
NEXT_PUBLIC_API_URL=http://localhost:3000/carshare-viewer
9+
10+
# Server Actions で許可する Origin(カンマ区切り,必要時のみ設定)
11+
# 例: SERVER_ACTIONS_ALLOWED_ORIGINS=ktak.dev,www.ktak.dev
12+
SERVER_ACTIONS_ALLOWED_ORIGINS=

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ cp .env.sample .env
170170
| `PORT` | ホストに公開するポート番号(Docker 開発環境のみ) | `3200` |
171171
| `MONGO_URI` | MongoDB 接続 URI(形式: `mongodb://host:port/`| `mongodb://db:27017/` |
172172
| `NEXT_PUBLIC_API_URL` | Server Components からの API アクセス URL(通常は変更不要) | `http://localhost:3000/carshare-viewer` |
173+
| `SERVER_ACTIONS_ALLOWED_ORIGINS` | Server Actions で許可する Origin(カンマ区切り) | - |
173174
| `DOCKER_IMAGE` | 本番用 Docker イメージ名(`docker-compose.prod.yml` で使用) | - |
174175

175176
### Docker を使用した開発
@@ -326,6 +327,7 @@ cd /path/to/deploy
326327
PORT=3200
327328
MONGO_URI=mongodb://db:27017/
328329
NEXT_PUBLIC_API_URL=https://yourdomain.com/carshare-viewer
330+
SERVER_ACTIONS_ALLOWED_ORIGINS=yourdomain.com,www.yourdomain.com
329331
DOCKER_IMAGE=ghcr.io/<owner>/<repo>:latest
330332
```
331333

@@ -344,6 +346,12 @@ DOCKER_IMAGE=ghcr.io/<owner>/<repo>:latest
344346
2. タイムズカー API 接続を確認: `curl https://api.timescar.jp/...`
345347
3. cron 実行ログを確認: `docker compose -f docker-compose.prod.yml logs app`
346348

349+
**`Invalid Server Actions request` が発生する場合**:
350+
351+
1. `.env``NEXT_PUBLIC_API_URL` が実際の公開ドメインと一致しているか確認
352+
2. `SERVER_ACTIONS_ALLOWED_ORIGINS` に公開ドメインを設定(例: `ktak.dev,www.ktak.dev`
353+
3. 再起動: `docker compose -f docker-compose.prod.yml up -d --force-recreate`
354+
347355
## 開発ガイド
348356

349357
### ページ構成

next.config.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
import type { NextConfig } from 'next';
22

3+
const normalizeOriginHost = (value: string): string =>
4+
value.replace(/^https?:\/\//, '').replace(/\/$/, '').trim();
5+
6+
const getApiUrlHost = (): string => {
7+
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
8+
if (!apiUrl) return '';
9+
10+
try {
11+
return new URL(apiUrl).host;
12+
} catch {
13+
return normalizeOriginHost(apiUrl);
14+
}
15+
};
16+
17+
const allowedOrigins = Array.from(
18+
new Set(
19+
[
20+
'localhost:3200',
21+
'127.0.0.1:3200',
22+
getApiUrlHost(),
23+
...(process.env.SERVER_ACTIONS_ALLOWED_ORIGINS ?? '')
24+
.split(',')
25+
.map((origin) => normalizeOriginHost(origin))
26+
.filter(Boolean),
27+
].filter(Boolean),
28+
),
29+
);
30+
331
const nextConfig: NextConfig = {
432
basePath: '/carshare-viewer',
533
output: 'standalone',
34+
experimental: {
35+
serverActions: {
36+
allowedOrigins,
37+
},
38+
},
639
images: {
740
remotePatterns: [
841
{

0 commit comments

Comments
 (0)