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(defineCachedEventHandler): add event.context.cache #2519

Merged
merged 7 commits into from
Jun 13, 2024
Merged

Conversation

atinux
Copy link
Member

@atinux atinux commented Jun 13, 2024

This allow the frontend framework connected to Nitro to be aware that the page is cached, useful for handling a flag in the payload to handle a different logic on client-side (e.g: fetch the auth state after hydration)

@pi0
Copy link
Member

pi0 commented Jun 13, 2024

Thanks for PR. I thibnk we should make it to context.cache: {} to preserve more cache state. (#2520)

@pi0 pi0 mentioned this pull request Jun 13, 2024
@pi0 pi0 changed the title feat: add event.context.cached flag when using defineCachedEventHandler feat(cache): add event.context.cache with defineCachedEventHandler Jun 13, 2024
@atinux
Copy link
Member Author

atinux commented Jun 13, 2024

Updated to set cache as empty object to start, we can follow up later in other pull requests.

Also added a basic test case to improve later on too.

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks!

Updates:

  • Added cache.options
  • Added h3 type augmentations (locally broken on main)

@pi0 pi0 changed the title feat(cache): add event.context.cache with defineCachedEventHandler feat(defineCachedEventHandler): add event.context.cache Jun 13, 2024
@pi0 pi0 merged commit fc3968b into main Jun 13, 2024
5 checks passed
@pi0 pi0 deleted the feat/cached-flag branch June 13, 2024 12:36
@pi0 pi0 mentioned this pull request Jun 13, 2024
@meirroth
Copy link

Hi @atinux, Is this feature planned for Nuxt, or is it a use case you are developing? I'm curious to see how it can be used. I believe this can be very helpful for caching heavy pages while keeping them dynamic for users.

@pi0
Copy link
Member

pi0 commented Jun 14, 2024

@meirroth You can access the same event.cache.option object from Nuxt server routes also optionally use a Nitro plugin + hooks to expose it via SSR payload to the client side.

This feature initially has two usecases:

  • As a flag indicator to know if request is cached. So that if it is, client-side for example can make a fresh fetch
  • Expose cache options to other server composable utils so they can adjust their behavior knowing what cache options is used.

Dynamically enabling or disabling cache can be done using shouldBypassCache option.

@manniL
Copy link
Member

manniL commented Jun 14, 2024

And for "nuxt-rendered" parts (so no server api / server routes), nuxt/nuxt#26598 is where we track it.

@meirroth
Copy link

@pi0 and @manniL, thank you for your replies!

I'm trying to understand how to apply this in practice, so an example would be really helpful if you have one.

My use case involves caching a page with SWR for all except requests with an auth header. This way, visitors will see a cached page, while logged-in users will see a personalized version of the page, which differs for each user.

I just watched @manniL's video on Dynamic Rendering SSR/SPA. (Great video as always!) Is the approach similar? Would it involve defining server middleware that checks for an auth header and, if it exists, disables the cache with event.cache.option?

Additionally, I assume the i18n module adds complexity since you'd need to cache pages for each language.

pi0 added a commit that referenced this pull request Jun 27, 2024
Co-authored-by: Pooya Parsa <pooya@pi0.io>
@pi0 pi0 mentioned this pull request Jun 27, 2024
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