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

Conversation

dalechyn
Copy link

@dalechyn dalechyn commented Mar 4, 2024

Dup of #5458

🎯 Changes

What changes are made in this PR? Is it a feature or a bug fix?

βœ… 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.

@dalechyn dalechyn requested a review from a team as a code owner March 4, 2024 13:04
Copy link

vercel bot commented Mar 4, 2024

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

Name Status Preview Comments Updated (UTC)
trpc-next-app-dir πŸ›‘ Canceled (Inspect) Apr 22, 2024 2:00pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Apr 22, 2024 2:00pm

Copy link

vercel bot commented Mar 4, 2024

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

A member of the Team first needs to authorize it.

Doesn't serve any benefit since `server-invoker` exists.
I haven't found a way to mock `next-auth` from playwright so instead,
I'm setting different extra headers that go into the `cacheContext` fn.

/**
* This client invokes procedures directly on the server without fetching over HTTP.
*/
export const api = experimental_createTRPCNextAppDirServer<typeof appRouter>({
config() {
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 (typeof window !== 'undefined' && window.AbortController) {
return window.AbortController;
}
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
Copy link
Member

Choose a reason for hiding this comment

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

???

Copy link
Author

Choose a reason for hiding this comment

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

pnpm lint-fix did that, i undoed the changes and a workflow applied formatting once again πŸ˜΅β€πŸ’«

if (typeof window !== 'undefined' && window.TextDecoder) {
return new window.TextDecoder();
}
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain
Copy link
Member

Choose a reason for hiding this comment

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

???

Copy link
Author

Choose a reason for hiding this comment

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


const ctx = await config.createContext();
runtime.ctx = ctx;
runtime.cacheTagSeparators = config.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 dont think we can cache this anymore when it's procedure-specific, it should be invoked every call i think

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 ;)

Comment on lines 46 to 47
const config = opts.config();
return createTRPCUntypedClient(config);
});
const client = createTRPCUntypedClient(config);
Copy link
Member

Choose a reason for hiding this comment

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

this can still be deduped as it was before, no?

Copy link
Author

Choose a reason for hiding this comment

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

done!

pnpm-lock.yaml Outdated
Copy link
Member

Choose a reason for hiding this comment

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

revert lock changes

Copy link
Member

@juliusmarminge juliusmarminge left a comment

Choose a reason for hiding this comment

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

Changes overall looks good to me.

While we are considering deprecating all of this stuff in favor of the experimental_caller() approach (#5589), we could merge this in the interrim after resolving the few nits I left

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants