Skip to content
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

Open
wants to merge 48 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
6f32a91
feat(next/app-dir): `cacheTag` generation to consider context
dalechyn Feb 7, 2024
7ee54e8
nit: lint
dalechyn Feb 7, 2024
6cfe7f4
Merge remote-tracking branch 'upstream/next' into next
dalechyn Feb 7, 2024
cf3c99f
refactor(next/app-dir): `@noble/hashes` -> `crypto` for input hashing
dalechyn Feb 7, 2024
6989a2e
refactor(next/app-dir): use `crypto.subtle` instead
dalechyn Feb 8, 2024
b29441a
feat(next/app-dir): drop http link, add `createContext` param to serv…
dalechyn Feb 8, 2024
655888e
nit: lint
dalechyn Feb 8, 2024
dfb02d1
Merge remote-tracking branch 'upstream/next' into next
dalechyn Feb 8, 2024
a8e88f1
chore: apply lint and formatting fixes
autofix-ci[bot] Feb 8, 2024
336f480
feat: hoist createContext one level up
dalechyn Feb 11, 2024
d133445
Merge remote-tracking branch 'upstream/next' into next
dalechyn Feb 11, 2024
8e35dda
chore: apply lint and formatting fixes
autofix-ci[bot] Feb 11, 2024
726931d
refactor: don't change `AnyClientTypes`
dalechyn Mar 1, 2024
2f5fdff
Merge remote-tracking branch 'upstream/next' into next
dalechyn Mar 1, 2024
2f1baad
hoist
juliusmarminge Mar 4, 2024
f305da3
feat: delete `server-http`
dalechyn Mar 4, 2024
129d8f4
test: add regression test for `cacheContext`
dalechyn Mar 4, 2024
75af515
chore: apply lint and formatting fixes
autofix-ci[bot] Mar 4, 2024
0866a5a
fix(server): use the recommended connection handler from @fastify/web…
mat-sz Mar 2, 2024
e66cd06
fix(client / server): chained `.pipe()`s in observables (#5533)
KATT Mar 3, 2024
c054046
chore: use vitest workspaces (#5461)
juliusmarminge Mar 4, 2024
60976e9
Merge remote-tracking branch 'upstream/next' into cache-context
dalechyn Mar 4, 2024
1dfd889
feat: bring the http back
dalechyn Mar 4, 2024
2d4e0af
chore: apply lint and formatting fixes
autofix-ci[bot] Mar 4, 2024
7348eec
fix: nextHttp tests
dalechyn Mar 4, 2024
b80e148
revert unrelated
juliusmarminge Mar 4, 2024
174cd91
rev more
juliusmarminge Mar 4, 2024
64faf05
nice
juliusmarminge Mar 4, 2024
aaa7983
chore: apply lint and formatting fixes
autofix-ci[bot] Mar 4, 2024
66ae725
revert some stuff
juliusmarminge Mar 4, 2024
f568388
revert more unused
juliusmarminge Mar 4, 2024
df05c4e
wtf autolinter
juliusmarminge Mar 4, 2024
6662379
chore: apply lint and formatting fixes
autofix-ci[bot] Mar 4, 2024
0b24086
Merge branch 'next' into cache-context
juliusmarminge Mar 4, 2024
4e0b11a
revv lock
juliusmarminge Mar 4, 2024
68b265b
fuck this autolinter
juliusmarminge Mar 4, 2024
3edaaed
cache context
juliusmarminge Mar 4, 2024
c89d7c2
niceee
juliusmarminge Mar 4, 2024
342537a
fix
juliusmarminge Mar 4, 2024
bf86986
feat: add proxy callback opts to `contextSelector`
dalechyn Mar 12, 2024
a01dfcd
Merge remote-tracking branch 'upstream/next' into cache-context
dalechyn Mar 12, 2024
68429bb
chore: apply lint and formatting fixes
autofix-ci[bot] Mar 12, 2024
e38a826
feat: requested changes
dalechyn Mar 28, 2024
a51b953
Merge remote-tracking branch 'upstream/next' into cache-context
dalechyn Mar 28, 2024
e0dda23
nit: comment
dalechyn Apr 18, 2024
2ab36b7
nit: revert lockfile changes
dalechyn Apr 22, 2024
525f326
nit: initialize client lazily
dalechyn Apr 22, 2024
531717e
Merge branch 'next' into cache-context
dalechyn Apr 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,9 +13,8 @@ export const api = experimental_createTRPCNextAppDirServer<typeof appRouter>({
return {
createContext: () => createContext('invoke'),
contextSelector: (ctx, callOpts) => {
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thoughts, will implement

if (callOpts.path[0] === 'privateGreeting')
return [ctx.session?.user.id, ctx._userIdMock];
return [];
if (!['privateGreeting'].includes(callOpts.path[0])) return [];
Copy link
Member

Choose a reason for hiding this comment

The 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({
Expand Down
15 changes: 3 additions & 12 deletions packages/next/src/app-dir/server.ts
Expand Up @@ -11,7 +11,6 @@ import type {
inferClientTypes,
inferProcedureInput,
MaybePromise,
ProxyCallbackOptions,
RootConfig,
Simplify,
TRPCResponse,
Expand All @@ -24,7 +23,6 @@ import {
TRPCError,
} from '@trpc/server/unstable-core-do-not-import';
import { revalidateTag } from 'next/cache';
import { cache } from 'react';
import { formDataToObject } from './formDataToObject';
import type {
ActionHandlerDef,
Expand All @@ -41,21 +39,14 @@ import type {
export function experimental_createTRPCNextAppDirServer<
TRouter extends AnyRouter,
>(opts: CreateTRPCNextAppRouterServerOptions<TRouter>) {
const getClient = cache(async (callOpts: ProxyCallbackOptions) => {
return createRecursiveProxy(async (callOpts) => {
const config = opts.config();
const client = createTRPCUntypedClient(config);
const runtime = client.runtime as NextAppDirRuntime<TRouter>;

const ctx = await config.createContext();
runtime.ctx = ctx;
runtime.cacheTagSeparators = config.contextSelector?.(ctx, callOpts) ?? [];
return client;
});

return createRecursiveProxy(async (callOpts) => {
// lazily initialize client
const client = await getClient(callOpts);
const runtime = client.runtime as NextAppDirRuntime<TRouter>;
runtime.ctx = ctx;
runtime.cacheTagSeparators = config.contextSelector?.(ctx, callOpts) ?? [];

const pathCopy = [...callOpts.path];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down