-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
i18n(ja): Add guides/sessions.mdx
#11507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jp-knj
wants to merge
6
commits into
withastro:main
Choose a base branch
from
jp-knj:i18n-ja-guides-sessions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−0
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
--- | ||
title: セッション | ||
description: オンデマンドレンダリングページのリクエスト間でデータを共有するための方法。 | ||
i18nReady: true | ||
--- | ||
|
||
import Since from '~/components/Since.astro'; | ||
import ReadMore from '~/components/ReadMore.astro'; | ||
|
||
<p> | ||
<Since v="5.7.0" /> | ||
</p> | ||
|
||
セッションは、[オンデマンドレンダリングページ](/ja/guides/on-demand-rendering/)のリクエスト間でデータを共有するための方法です。 | ||
|
||
[`cookies`](/ja/guides/on-demand-rendering/#cookies)とは異なり、セッションはサーバーでデータを保存するため、より大きなデータを保存することができます。また、サイズ制限やセキュリティの問題も気にする必要がありません。ユーザーのデータ、ショッピングカート、フォームの状態などを保存するのに便利です。クライアント側のJavaScriptを使わないで機能します。 | ||
|
||
```astro title="src/components/CartButton.astro" {3} | ||
--- | ||
export const prerender = false; // 'server'出力の場合は不要 | ||
const cart = await Astro.session?.get('cart'); | ||
--- | ||
|
||
<a href="/checkout">🛒 {cart?.length ?? 0} 点</a> | ||
``` | ||
|
||
## セッションの設定 | ||
|
||
セッションデータを保存するにはストレージドライバーが必要です。[Node](/ja/guides/integrations-guide/node/#sessions)・[Cloudflare](/ja/guides/integrations-guide/cloudflare/#sessions)・[Netlify](/ja/guides/integrations-guide/netlify/#sessions) の各アダプターはデフォルトドライバーを自動的に設定します。しかし、その他のアダプターでは [ドライバーを手動で指定](/ja/reference/configuration-reference/#sessiondriver) する必要があります。 | ||
|
||
```js title="astro.config.mjs" ins={4} | ||
{ | ||
adapter: vercel(), | ||
session: { | ||
driver: "redis", | ||
}, | ||
} | ||
``` | ||
|
||
<ReadMore> | ||
ストレージドライバーの設定方法やその他の設定項目の詳細については、[session設定オプション](/ja/reference/configuration-reference/#session-options)を参照してください。 | ||
</ReadMore> | ||
|
||
## セッションデータを操作する | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. “Interacting with session data” を直訳すると「セッションデータとのやり取り」や「セッションデータと対話する」といった不自然な見出しになる。ドキュメントの見出しとして読みやすく、内容も伝わる日本語にするなら、”セッションデータの扱い方”か”セッションデータを操作する” |
||
|
||
[`session` オブジェクト](/ja/reference/api-reference/#session) を使うと、保存されているユーザー状態(例:ショッピングカートへの商品の追加)やセッションID(例:ログアウト時にセッションID Cookieを削除)を操作できます。このオブジェクトはAstroのコンポーネントおよびページでは`Astro.session`として、API エンドポイント・ミドルウェア・アクションでは`context.session`として利用できます。 | ||
|
||
セッションは初回利用時に自動生成され、[`session.regenerate()`](/ja/reference/api-reference/#regenerate) でいつでも再生成でき、[`session.destroy()`](/ja/reference/api-reference/#destroy) で破棄できます。 | ||
|
||
ほとんどの場合は、[`session.get()`](/ja/reference/api-reference/#get) と [`session.set()`](/ja/reference/api-reference/#set) の2つだけで十分です。 | ||
|
||
<ReadMore> | ||
詳しくは [Sessions APIリファレンス](/ja/reference/api-reference/#session)を参照してください。 | ||
</ReadMore> | ||
|
||
### Astro コンポーネントとページ | ||
|
||
`.astro` コンポーネントやページでは、グローバル `Astro` オブジェクト経由でセッションにアクセスできます。たとえば、ショッピングカート内の商品数を表示する例です。 | ||
|
||
```astro title="src/components/CartButton.astro" "Astro.session" | ||
--- | ||
export const prerender = false; // 'server'出力の場合は不要 | ||
const cart = await Astro.session?.get('cart'); | ||
--- | ||
|
||
<a href="/checkout">🛒 {cart?.length ?? 0} 点</a> | ||
``` | ||
|
||
### API エンドポイント | ||
|
||
APIエンドポイントでは、セッションは`context`オブジェクト上で利用できます。たとえば、ショッピングカートに商品を追加する例です。 | ||
|
||
```ts title="src/pages/api/addToCart.ts" "context.session" | ||
export async function POST(context: APIContext) { | ||
const cart = await context.session?.get('cart') || []; | ||
const data = await context.request.json<{ item: string }>(); | ||
if (!data?.item) { | ||
return new Response('Item is required', { status: 400 }); | ||
} | ||
cart.push(data.item); | ||
await context.session?.set('cart', cart); | ||
return Response.json(cart); | ||
} | ||
``` | ||
|
||
### アクション | ||
|
||
アクション内でも、`context`オブジェクト上でセッションにアクセスできます。たとえば、ショッピングカートに商品を追加する例です。 | ||
|
||
```ts title="src/actions/addToCart.ts" "context.session" | ||
import { defineAction } from 'astro:actions'; | ||
import { z } from 'astro:schema'; | ||
|
||
export const server = { | ||
addToCart: defineAction({ | ||
input: z.object({ productId: z.string() }), | ||
handler: async (input, context) => { | ||
const cart = await context.session?.get('cart'); | ||
cart.push(input.productId); | ||
await context.session?.set('cart', cart); | ||
return cart; | ||
}, | ||
}), | ||
}; | ||
``` | ||
|
||
### ミドルウェア | ||
|
||
:::注意点 | ||
エッジミドルウェアでは、現在セッションはサポートされていません。 | ||
::: | ||
|
||
ミドルウェア内では、`context`オブジェクト上でセッションにアクセスできます。たとえば、セッションに最終訪問した時刻を保存する例です。 | ||
|
||
```ts title="src/middleware.ts" "context.session" | ||
import { defineMiddleware } from 'astro:middleware'; | ||
|
||
export const onRequest = defineMiddleware(async (context, next) => { | ||
context.session?.set('lastVisit', new Date()); | ||
return next(); | ||
}); | ||
``` | ||
|
||
## セッションデータの型 | ||
|
||
デフォルトではセッションデータに型はなく、任意のキーに好きなデータを保存できます。値のシリアライズ/デシリアライズには、コンテンツコレクションやアクションでも使われている [devalue](https://github.com/Rich-Harris/devalue) が使用されます。そのため、文字列・数値・`Date`・`Map`・`Set`・`URL`・配列・プレーンオブジェクトなど、同じ型がサポートされています。 | ||
|
||
必要に応じて、`src/env.d.ts` ファイルを作成し、`App.SessionData` 型を宣言することでセッションデータに TypeScript の型を付けることもできます。 | ||
|
||
```ts title="src/env.d.ts" | ||
declare namespace App { | ||
interface SessionData { | ||
user: { | ||
id: string; | ||
name: string; | ||
}; | ||
cart: string[]; | ||
} | ||
} | ||
``` | ||
|
||
これにより、エディター上で型チェックと補完を受けながらセッションデータにアクセスできます。 | ||
|
||
```ts title="src/components/CartButton.astro" | ||
--- | ||
const cart = await Astro.session?.get('cart'); | ||
// const cart: string[] | undefined | ||
|
||
const something = await Astro.session?.get('something'); | ||
// const something: any | ||
|
||
Astro.session?.set('user', { id: 1, name: 'Houston' }); | ||
// Error: Argument of type '{ id: number; name: string }' is not assignable to parameter of type '{ id: string; name: string; }'. | ||
--- | ||
``` | ||
|
||
:::注意点 | ||
これは型チェック専用であり、実行時のセッション動作には影響しません。ユーザーのセッションに既にデータが保存されている状態で型を変更すると、実行時エラーが発生する可能性があるため注意してください。 | ||
::: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve broken it up with periods for clarity.