Skip to content

Commit

Permalink
make the cache private if user is logged in
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasMahe committed Feb 21, 2024
1 parent 6f6209e commit d4a903c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
5 changes: 0 additions & 5 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,6 @@ MyApp.getInitialProps = async (
const initialProps = (await App.getInitialProps(
appContext,
)) as AppInitialProps<{}> // force type of props to empty object instead of any so TS will properly require MyAppProps to be returned by this function
// set cache control header
appContext.ctx.res?.setHeader(
'Cache-Control',
'public, max-age=20, s-maxage=20, stale-while-revalidate=20',
)
const jwt = appContext.ctx.req?.cookies?.[COOKIE_JWT_TOKEN] || null
// Generate the now time, rounded to the second to avoid re-rendering on the client
// TOFIX: find a better way to share the time between the app and document
Expand Down
16 changes: 10 additions & 6 deletions pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,18 @@ class MyDocument extends Document {
req?: { cookies?: COOKIES }
},
): Promise<DocumentInitialProps & MyDocumentProps> {
// set cache control header
// context.res?.setHeader(
// 'Cache-Control',
// 'public, max-age=10, s-maxage=10, stale-while-revalidate=10',
// )

invariant(context.req)
const jwt = context.req?.cookies?.[COOKIE_JWT_TOKEN] || null

// set cache control header
context.res?.setHeader(
'Cache-Control',
`${
// make the cache private if user is logged in
jwt ? 'private' : 'public'
}, max-age=10, s-maxage=10, stale-while-revalidate=10`,
)

const environment = await getEnvironment()
// the `getClient` needs to be reset on every request as early as possible and before any rendering
const apolloClient = getClient(
Expand Down

0 comments on commit d4a903c

Please sign in to comment.