Session Test Documentation Page
Ta treść nie jest jeszcze dostępna w Twoim języku.
Session Test in Starlight
Section titled “Session Test in Starlight”This page demonstrates session handling within Starlight documentation pages. Since Starlight pages are typically prerendered, we’ll show how to integrate dynamic session functionality.
Session Component
Section titled “Session Component”Loading session data...
How It Works
Section titled “How It Works”This Starlight page demonstrates several session concepts:
1. Client-Side Session Fetching
Section titled “1. Client-Side Session Fetching”Since Starlight pages are prerendered by default, we use a React component that fetches session data from an API endpoint after the page loads:
// Fetch session data on component mountuseEffect(() => {fetch('/api/session') .then(res => res.json()) .then(data => setSessionData(data));}, []);2. API Endpoint Integration
Section titled “2. API Endpoint Integration”The component communicates with an API endpoint at /api/session that handles:
- GET: Retrieve current session information
- POST: Store custom data in the session
- DELETE: Clear the session
3. Session Persistence
Section titled “3. Session Persistence”Sessions are persisted using HTTP-only cookies with these properties:
cookies.set('session_id', sessionId, {path: '/',maxAge: 60 * 60 * 24 * 30, // 30 dayshttpOnly: true,secure: true,sameSite: 'lax'});Key Differences from Standard Astro Pages
Section titled “Key Differences from Standard Astro Pages”| Feature | Standard Astro Page | Starlight Documentation |
|---|---|---|
| Rendering | SSR (server-side) | Prerendered (static) |
| Session Access | Direct in frontmatter | Via API endpoints |
| Cookie Handling | Astro.cookies | Client-side fetch |
| Form Handling | Native HTML forms | JavaScript/React |
| Database Access | Direct in page | Via API endpoints |
Testing the Session
Section titled “Testing the Session”- Visit Counter: Refresh this page and watch the visit count increase
- Custom Data: Enter some text and save it to the session
- Persistence: Navigate away and come back - your data persists
- Clear Session: Use the clear button to reset everything
Implementation Notes
Section titled “Implementation Notes”Next Steps
Section titled “Next Steps”To implement sessions in your own Starlight documentation:
- Create API endpoints for session management
- Use client-side components for dynamic content
- Implement proper error handling
- Consider caching strategies for performance
- Add session-based features like progress tracking
This test page demonstrates session functionality in Starlight. Visit the standard Astro test page to compare implementations.