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

Conversation

dalechyn
Copy link

@dalechyn dalechyn commented Feb 7, 2024

Closes: #5455

🎯 Changes

This change allows a user to specify contextCache method in the experimental_nextCacheLink, which lets one select the values from the context that should be considered when a cacheTag is generated.

It also changes the way cacheTag is generated, taking care of the fact that the cacheTag cannot be longer then 256 characters, thus instead of stringifying input values, it will form a sha-256 hash of those.

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

there are no test cases covering this and I haven't added any.

This change allows a user to specify `contextCache` method in the
`experimental_nextCacheLink`, which lets one select the values
from the context that should be considered when a `cacheTag` is
generated.

It also changes the way `cacheTag` is generated, taking care of
the fact that the `cacheTag` cannot be longer then 256 characters,
thus instead of stringifying input values, it will form a sha-256 hash
of those.

Implements trpc#5455
@dalechyn dalechyn requested a review from a team as a code owner February 7, 2024 00:43
Copy link

vercel bot commented Feb 7, 2024

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
trpc-next-app-dir βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Mar 1, 2024 3:31pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Mar 1, 2024 3:31pm

Copy link

vercel bot commented Feb 7, 2024

@dalechyn is attempting to deploy a commit to the trpc Team on Vercel.

A member of the Team first needs to authorize it.

@dalechyn
Copy link
Author

dalechyn commented Feb 7, 2024

@KATT, noCache middleware is not implemented as discussed in #5455, and I also don't know how to deal with other places where generateCacheTag is used as such don't have access to the context as opposed to experimental_nextCacheLink's createContext() function that exposes one.

I need some help figuring this part out. Anyway, the commit should not break those as the context parameter is also marked with any type, therefore is optional to be passed.

@dalechyn
Copy link
Author

dalechyn commented Feb 7, 2024

@juliusmarminge, ci fails as webpack cannot import node:crypto.

also found this thing: https://nextjs.org/docs/messages/node-module-in-edge-runtime, that might indicate that it would not be possible to use node:crypto in edge environment.

UPD: it is supported, https://nextjs.org/docs/pages/api-reference/edge#crypto-apis

* 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.

@dalechyn
Copy link
Author

dalechyn commented Feb 8, 2024

@juliusmarminge i need some guidance here, i'm not sure wether I'm on the right or wrong direction. Starting to have a real type battle here.

@dalechyn
Copy link
Author

Screenshot 2024-02-11 at 14 37 55
Just tried it all out, caching by hash seems working for experimental app dir example, even when a function has no input it includes whatever I pass to the cacheContext fn to the cache tag.

Copy link

github-actions bot commented Mar 5, 2024

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: better cacheTag control in next app dir layouts
2 participants