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
feat(next/app-dir): contextCache
to control context values to add to cacheTag
#5537
base: next
Are you sure you want to change the base?
Changes from 45 commits
6f32a91
7ee54e8
6cfe7f4
cf3c99f
6989a2e
b29441a
655888e
dfb02d1
a8e88f1
336f480
d133445
8e35dda
726931d
2f5fdff
2f1baad
f305da3
129d8f4
75af515
0866a5a
e66cd06
c054046
60976e9
1dfd889
2d4e0af
7348eec
b80e148
174cd91
64faf05
aaa7983
66ae725
f568388
df05c4e
6662379
0b24086
4e0b11a
68b265b
3edaaed
c89d7c2
342537a
bf86986
a01dfcd
68429bb
e38a826
a51b953
e0dda23
2ab36b7
525f326
531717e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
import { loggerLink } from '@trpc/client'; | ||
import { experimental_nextCacheLink } from '@trpc/next/app-dir/links/nextCache'; | ||
import { experimental_createTRPCNextAppDirServer } from '@trpc/next/app-dir/server'; | ||
import { auth } from '~/auth'; | ||
import { appRouter } from '~/server/routers/_app'; | ||
import { cookies } from 'next/headers'; | ||
import superjson from 'superjson'; | ||
import { createContext } from './shared-server'; | ||
|
||
/** | ||
* This client invokes procedures directly on the server without fetching over HTTP. | ||
*/ | ||
export const api = experimental_createTRPCNextAppDirServer<typeof appRouter>({ | ||
config() { | ||
return { | ||
createContext: () => createContext('invoke'), | ||
/** | ||
* Using the `contextSelector` property we can select the specific values to be included | ||
* in the `cacheTag` generation. | ||
* | ||
* In the following example, if the requests' path doesn't have `privateGreeting` in it | ||
* (which is a private route that uses some values from the context), we return an empty array | ||
* meaning that we don't use any values from the context in other routes. | ||
* On the other side if it has `privateGreeting`, we specify the values from the context that we use | ||
* in the `privateGreeting` procedure. | ||
*/ | ||
contextSelector: (ctx, callOpts) => { | ||
if (!['privateGreeting'].includes(callOpts.path[0])) return []; | ||
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. a comment would be nice - i don't know if even i would know what this does if i revisit this example in a month or 2 ;) |
||
return [ctx.session?.user.id, ctx._userIdMock]; | ||
}, | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => true, | ||
enabled: (_op) => true, | ||
}), | ||
experimental_nextCacheLink({ | ||
// requests are cached for 5 seconds | ||
revalidate: 5, | ||
router: appRouter, | ||
transformer: superjson, | ||
createContext: async () => { | ||
return { | ||
session: await auth(), | ||
headers: { | ||
cookie: cookies().toString(), | ||
'x-trpc-source': 'rsc-invoke', | ||
}, | ||
}; | ||
}, | ||
}), | ||
], | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { auth } from '~/auth'; | ||
import { cookies, headers } from 'next/headers'; | ||
|
||
export async function createContext(source: 'invoke' | 'http') { | ||
return { | ||
session: await auth(), | ||
// Mock user id which is used for testing. If you copy this file, delete the next property. | ||
_userIdMock: headers().get('x-trpc-user-id'), | ||
headers: { | ||
cookie: cookies().toString(), | ||
'x-trpc-source': `rsc-${source}`, | ||
}, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,10 @@ export function getAbortController( | |
return customAbortControllerImpl; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
if (typeof window !== 'undefined' && window.AbortController) { | ||
return window.AbortController; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
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. ??? 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.
|
||
|
||
if (typeof globalThis !== 'undefined' && globalThis.AbortController) { | ||
return globalThis.AbortController; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,10 @@ export function getTextDecoder( | |
return customTextDecoder; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
if (typeof window !== 'undefined' && window.TextDecoder) { | ||
return new window.TextDecoder(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
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. ??? 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. |
||
|
||
if (typeof globalThis !== 'undefined' && globalThis.TextDecoder) { | ||
return new globalThis.TextDecoder(); | ||
} | ||
|
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 think this example should show a "private by default" and opt-in to sharing cache for specific procedures thst doesnt have sensitive data
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.
Good thoughts, will implement