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 #5458

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
6 changes: 4 additions & 2 deletions packages/next/package.json
Expand Up @@ -71,7 +71,6 @@
"ssrPrepass",
"!**/*.test.*"
],
"dependencies": {},
"peerDependencies": {
"@tanstack/react-query": "^5.0.0",
"@trpc/client": "10.45.1",
Expand Down Expand Up @@ -112,5 +111,8 @@
},
"funding": [
"https://trpc.io/sponsor"
]
],
"dependencies": {
"@noble/hashes": "^1.3.3"
dalechyn marked this conversation as resolved.
Show resolved Hide resolved
}
}
27 changes: 18 additions & 9 deletions packages/next/src/app-dir/links/nextCache.ts
Expand Up @@ -18,6 +18,11 @@ import { generateCacheTag } from '../shared';

type NextCacheLinkOptions<TRouter extends AnyRouter> = {
router: TRouter;
/**
* define which values from the context should be considered into the cache
* key
*/
cacheContext?: (ctx: inferRouterContext<TRouter>) => any[];
Copy link
Member

Choose a reason for hiding this comment

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

while we don't have docs for these stuff yet, maybe include this in the example we have that's using this link so that people can know about it somehow.

i'd almost be inclined to make it required even since it's a vital part of getting the right cache key and making sure different users doesn't share the same cache entries?

Copy link
Author

Choose a reason for hiding this comment

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

I suggest making this property | undefined instead of partial so that user is explicitly passing undefined if he doesn't want to manipulate the cache key.

Copy link
Author

@dalechyn dalechyn Feb 8, 2024

Choose a reason for hiding this comment

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

included in the example, and made the property | undefined.

createContext: () => Promise<inferRouterContext<TRouter>>;
/** how many seconds the cache should hold before revalidating */
revalidate?: number | false;
Expand All @@ -33,18 +38,22 @@ export function experimental_nextCacheLink<TRouter extends AnyRouter>(
observable((observer) => {
const { path, input, type, context } = op;

const cacheTag = generateCacheTag(path, input);
// Let per-request revalidate override global revalidate
const requestRevalidate =
typeof context['revalidate'] === 'number' ||
context['revalidate'] === false
? context['revalidate']
: undefined;
const revalidate = requestRevalidate ?? opts.revalidate ?? false;

const promise = opts
.createContext()
dalechyn marked this conversation as resolved.
Show resolved Hide resolved
.then(async (ctx) => {
const cacheTag = generateCacheTag(
path,
input,
opts.cacheContext?.(ctx),
);
// Let per-request revalidate override global revalidate
const requestRevalidate =
typeof context['revalidate'] === 'number' ||
context['revalidate'] === false
? context['revalidate']
: undefined;
const revalidate = requestRevalidate ?? opts.revalidate ?? false;
dalechyn marked this conversation as resolved.
Show resolved Hide resolved

const callProc = async (_cachebuster: string) => {
// // _cachebuster is not used by us but to make sure
// // that calls with different tags are properly separated
Expand Down
11 changes: 7 additions & 4 deletions packages/next/src/app-dir/shared.ts
@@ -1,3 +1,4 @@
import { sha256 } from '@noble/hashes/sha256';
import type {
CreateTRPCClientOptions,
Resolver,
Expand Down Expand Up @@ -86,10 +87,12 @@ export interface CreateTRPCNextAppRouterOptions<TRouter extends AnyRouter> {
/**
* @internal
*/
export function generateCacheTag(procedurePath: string, input: any) {
return input
? `${procedurePath}?input=${JSON.stringify(input)}`
: procedurePath;
export function generateCacheTag(
procedurePath: string,
input: any,
context?: any,
) {
return `${procedurePath}?hash=${sha256(JSON.stringify({ input, context }))}`;
}

export function isFormData(value: unknown): value is FormData {
Expand Down
54 changes: 37 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.