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

chore: better DX around for @cacheable #10847

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/app/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,25 @@ export const getCurrentURL = () => {

let { route: path } = currentModule

const queryParams = [] as { [key: string]: string }[]

if (currentModuleProps) {
Object.entries(currentModuleProps).map(([key, value]) => {
path = path.replace(`:${key}`, value as string)
if (path.includes(`:${key}`)) {
path = path.replace(`:${key}`, value as string)
} else {
queryParams.push({ [key]: value as string })
}
})
}

return encodeURI(`${webURL}${path}`)
const queryParamsString = queryParams.map((params) =>
Copy link
Member Author

Choose a reason for hiding this comment

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

the query params where not present in the helper so I added them here

Object.entries(params)
.map(([key, value]) => `${key}=${value}`)
.join("&")
)

return encodeURI(`${webURL}${path}?${queryParamsString}`)
}

export function getDomainMap(): Record<string, RouteMatcher[] | null> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const hasNoCacheParamPresent = (url: string) => {
return false
}

export const SKIP_CACHE_ARGUMENTS = ["includeArtworksByFollowedArtists", "collectionID"]
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't better to add some comments around it to give engineers direction and/or maybe add it to some docs about cache in Eigen?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's a valid point - we need something like this but for Eigen. I will draft something

// Important - Add any new personalized argument checks to this list. That way, logged-in queries
// _without_ this argument can still be `@cacheable`, and when queries include this argument,
// those queries will not be cached.
export const hasPersonalizedArguments = (variables: Variables) => {
const SKIP_CACHE_ARGUMENTS = ["includeArtworksByFollowedArtists"]
// return true if variables has at least one of the SKIP_CACHE_ARGUMENTS that is truthy
return SKIP_CACHE_ARGUMENTS.some((arg) => !!variables[arg])
}
25 changes: 21 additions & 4 deletions src/app/system/relay/middlewares/cacheHeaderMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
hasNoCacheParamPresent,
hasPersonalizedArguments,
isRequestCacheable,
SKIP_CACHE_ARGUMENTS,
} from "app/system/relay/helpers/cacheHeaderMiddlewareHelpers"
import { GraphQLRequest } from "app/system/relay/middlewares/types"
import { Middleware } from "react-relay-network-modern"
Expand All @@ -16,18 +17,34 @@ export const shouldSkipCDNCache = (req: GraphQLRequest) => {
// - a known personalized argument is present in the query (for example `include_artworks_by_followed_artists`)
// - nocache param is present in the url

if (req.cacheConfig?.force === true) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this down since it will be covered by the default case anyway

return true
}

if (isRequestCacheable(req)) {
if (req.cacheConfig?.force === true) {
if (__DEV__) {
console.warn(
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use info here? These warnings in the simulators/emulators are pretty annoying

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried both actually but they both seems equally ugly 🥲 except that the warn shows the count better

Screen.Recording.2024-09-26.at.10.38.46.mov

Copy link
Contributor

Choose a reason for hiding this comment

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

😢 then I'd vote only to console.log, these bubbles are bad for DX because they block the screen and all the time we have to close them

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that console.warns can be annoying, but that's only if you can't dismiss them or don't know why they're occuring. Here, the action item should be clear, for example you need to remove @cacheable. I do though think it's bad DX to declare your query as cacheable, expecting it to be cacheable, but it's not cacheable because something changed in the arguments and you aren't aware of it

Copy link
Contributor

@araujobarret araujobarret Sep 26, 2024

Choose a reason for hiding this comment

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

yeah, after a 2nd thought I'm fine with it, it should really pester someone in this case, 👍 for the .warn then

"You are setting force: true on a cacheable request, CDN cache will be ignored."
)
}
return true
}

const url = getCurrentURL()
if (url && hasNoCacheParamPresent(url)) {
if (__DEV__) {
console.warn(
"You are setting nocache param in the url on a cacheable request, CDN cache will be ignored."
)
}
// Don't use CDN cache if the url has the nocache param
return true
}

if (hasPersonalizedArguments(req.variables)) {
if (__DEV__) {
console.warn(
"You are setting a personalized argument on a cacheable request, CDN cache will be ignored.\nList of personalized arguments: ",
SKIP_CACHE_ARGUMENTS.join(", ")
)
}
// Don't use CDN cache if the query has a personalized argument
return true
}
Expand Down