c4762c6437
La page d'accueil appelle auth() (cookies → dynamique) donc export const revalidate
ne s'applique pas au rendu. On cache à la place les appels Directus :
- fetchConstitution() → wrappée avec nextCache (unstable_cache)
- revalidate: 300 (5 min) — la constitution évolue peu
- tag 'constitution' — permet revalidateTag('constitution') depuis une API route
pour invalider le cache à la demande lors d'un changement Directus
Les appels API Directus (titres + articles) ne sont plus refaits à chaque requête.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
93 lines
2.4 KiB
JavaScript
93 lines
2.4 KiB
JavaScript
import {createDirectus, rest, readItems} from '@directus/sdk'
|
|
import {unstable_cache as nextCache} from 'next/cache'
|
|
import Container from '@mui/material/Container'
|
|
import Box from '@mui/material/Box'
|
|
import Typography from '@mui/material/Typography'
|
|
import AdminPanelSettingsIcon from '@mui/icons-material/AdminPanelSettings'
|
|
import {auth} from '../auth.js'
|
|
import Konstitisyon from '@/components/konstitisyon/index.js'
|
|
import Footer from '@/components/footer.js'
|
|
import Sign from '@/components/session/sign.js'
|
|
import Create from '@/components/konstitisyon/create/index.js'
|
|
|
|
const apiUrl = process.env.DIRECTUS_API_URL
|
|
const appTitle = process.env.APP_TITLE
|
|
|
|
const navButton = {
|
|
title: 'Tableau de bord',
|
|
path: '/dashboard',
|
|
color: 'success',
|
|
icon: <AdminPanelSettingsIcon fontSize='large' />
|
|
}
|
|
|
|
async function fetchConstitution() {
|
|
if (!apiUrl) {
|
|
throw new Error('DIRECTUS_API_URL is required')
|
|
}
|
|
|
|
const client = createDirectus(apiUrl).with(rest())
|
|
|
|
try {
|
|
const titres = await client.request(
|
|
readItems('titres', {
|
|
filter: {
|
|
status: {
|
|
_eq: 'published'
|
|
}
|
|
},
|
|
sort: 'date_created'
|
|
})
|
|
)
|
|
|
|
const articles = await client.request(
|
|
readItems('articles', {
|
|
filter: {
|
|
status: {
|
|
_eq: 'published'
|
|
}
|
|
},
|
|
sort: 'numero'
|
|
})
|
|
)
|
|
|
|
return {
|
|
titres,
|
|
articles
|
|
}
|
|
} catch (error) {
|
|
throw new Error(error.cause)
|
|
}
|
|
}
|
|
|
|
// Mise en cache des données constitution — revalidation toutes les 5 minutes.
|
|
// Le tag 'constitution' permet une invalidation à la demande via revalidateTag().
|
|
const getData = nextCache(
|
|
fetchConstitution,
|
|
['constitution-data'],
|
|
{revalidate: 300, tags: ['constitution']}
|
|
)
|
|
|
|
export default async function Page() {
|
|
const session = await auth()
|
|
const {titres, articles} = await getData()
|
|
|
|
return (
|
|
<Box sx={{
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
minHeight: '100vh',
|
|
}}
|
|
>
|
|
<Container maxWidth='sm'>
|
|
<Typography mt={1} component='h1' textAlign='center' variant='h4'>{appTitle.toUpperCase()}</Typography>
|
|
<Sign session={session} navButton={navButton} />
|
|
{session && (
|
|
<Create session={session} titres={titres} />
|
|
)}
|
|
<Konstitisyon session={session} titres={titres} articles={articles} />
|
|
</Container>
|
|
<Footer />
|
|
</Box>
|
|
)
|
|
}
|