Hone logo
Hone
Problems

Vue Keep-Alive Cache Implementation Challenge

Imagine you're building a single-page application where users navigate between different views or components. To improve user experience and performance, you want to cache frequently accessed components so they don't need to be re-rendered from scratch every time the user returns to them. This challenge asks you to implement a functional keep-alive cache mechanism in Vue using TypeScript.

Problem Description

Your task is to create a system that allows Vue components to be cached and reused rather than being destroyed and recreated on every unmount. This involves managing the lifecycle of components, deciding which components to keep alive, and providing a mechanism to clear the cache when necessary.

Key Requirements:

  1. Component Caching: Implement a mechanism to store and retrieve instances of Vue components.
  2. Conditional Caching: Allow for the exclusion of specific components from being cached.
  3. Cache Management: Provide a way to clear the entire cache or specific cached components.
  4. Lifecycle Hook Integration: Ensure that cached components correctly utilize Vue's lifecycle hooks (e.g., mounted, unmounted, activated, deactivated).
  5. TypeScript Support: The solution must be written in TypeScript, providing type safety.

Expected Behavior:

  • When a component that is configured to be kept alive is navigated away from, its state should be preserved.
  • When the user navigates back to a previously visited and cached component, the cached instance should be reused, and its state should be restored.
  • Components explicitly marked for exclusion should be unmounted and recreated as usual.
  • A method should be available to manually clear all cached components.
  • Another method should allow clearing a specific component from the cache based on its identifier.

Edge Cases to Consider:

  • Components with complex internal state.
  • Dynamic component loading.
  • Handling multiple instances of the same component type (if applicable to the cache key strategy).

Examples

Example 1: Basic Caching

Imagine a UserProfile component and a Settings component. You want to keep UserProfile alive but not Settings.

  • Scenario: User navigates from Home to UserProfile, then to Settings, then back to UserProfile.
  • Expected Behavior:
    • When navigating to UserProfile the first time, it mounts.
    • When navigating to Settings, UserProfile is deactivated but remains in the cache. Settings mounts.
    • When navigating back to UserProfile from Settings, the cached instance of UserProfile is reused, and its previous state is restored. The activated hook is called, not mounted.

Example 2: Cache Clearing

Continuing from Example 1, after the user has navigated back to UserProfile and it's cached.

  • Scenario: User triggers a "clear cache" action.
  • Expected Behavior:
    • All cached components (in this case, UserProfile) are destroyed and removed from memory.
    • If the user navigates back to UserProfile again, a new instance is created and mounted.

Example 3: Excluding a Component

Consider a LoadingSpinner component that should never be cached.

  • Scenario: A Dashboard component uses a LoadingSpinner while fetching data. The Dashboard itself is kept alive.
  • Expected Behavior:
    • When Dashboard is unmounted, LoadingSpinner (if it exists) is properly unmounted and destroyed.
    • Dashboard remains in the cache.

Constraints

  • The solution should be implementable within a Vue 3 application using the Composition API.
  • The caching mechanism should be configurable per component.
  • The cache should have a reasonable memory footprint; avoid excessive caching of large components if not explicitly intended.
  • The implementation should leverage TypeScript's type system to ensure safety.

Notes

This challenge is inspired by Vue's built-in <keep-alive> component but requires you to build a similar functionality from scratch. Consider how you will identify components for caching and retrieval (e.g., using component names, unique keys, or props). Think about the different lifecycle hooks and how they should behave for cached versus non-cached components. You might need to create a custom hook or a composable function to manage the cache.

Loading editor...
typescript