Skip to content

Cached controllers that specify an alternate view directive can't be retrieved #78

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

Open
jaysylvester opened this issue May 14, 2021 · 0 comments

Comments

@jaysylvester
Copy link
Owner

jaysylvester commented May 14, 2021

There is a longstanding bug preventing cached controllers that specify an alternate view from being retrieved correctly.

Use case:

  1. Call a controller as either the parent controller or as an include within another controller.
  2. Provide a view directive within said controller that specifies a view other than the default.
  3. Provide a cache directive within said controller to cache it using the provided view.

Result:

  1. The controller is rendered and cached, but the cache item isn't retrieved in subsequent requests.

Workarounds:

  1. Use a separate action different from the default handler() for each view so app.cache.set() creates a unique cache object for each action, and therefore each view
  2. For includes only: specify the view in the calling controller via the include directive so the cache check in server.js includes the view as a parameter

This happens because server.js checks for the existence of the controller cache before firing the controller itself (which is the whole point of caching it), which means if you cache a controller with an alternate view directive, that cache item can never be retrieved because the view is part of the cache key, and the view directive isn't known until after the controller is fired.

Looking at rendering logic in server.js, a second cache.exists() call after the controller is called to get the alternate view directive would avoid rendering time, but the controller itself would still have done its work, negating most of the value of caching it.

The only resolution I can think of is to specify the view in some way prior to the controller call, such as a URL parameter, but my gut tells me that allowing that behavior is a security risk (potential for hacking the URL and calling a view the dev didn't intend for the context).

It's likely this issue will never be resolved, so this ticket serves as a log/reminder of its existence because the question keeps coming up and I need to keep reinvestigating it to remember why it's still there. Maybe someday I'll have an epiphany as to the solution.

Edit: The new controller config is a possible workaround for this, but it feels a bit hacky:

export const config = {
  handler: {
    view: 'alternate-view-name'
  }
}
@jaysylvester jaysylvester changed the title Fix caching of include controllers that specify an alternate view directive Fix caching of controllers that specify an alternate view directive May 23, 2021
@jaysylvester jaysylvester changed the title Fix caching of controllers that specify an alternate view directive Fix caching of include controllers that specify an alternate view directive Nov 26, 2021
@jaysylvester jaysylvester changed the title Fix caching of include controllers that specify an alternate view directive Fix caching of controllers that specify an alternate view directive Nov 26, 2021
@jaysylvester jaysylvester changed the title Fix caching of controllers that specify an alternate view directive Cached controllers that specify an alternate view directive can't be retrieved Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant