Skip to content

cacheKey generated an incorrect value in incremental static builds. #17635

Closed as not planned

Description

@sgalcheung

Astro Info

Astro                    v7.2.0
Node                     v22.16.0
System                   macOS (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/starlight

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

According to the blog post, this cacheKey should be a hash, but it was generated as a string.

The cacheKey covers your data. Astro handles the code side for you: during the build it hashes...

Image

cacheKey is "[object Object], [object Object]".

What's the expected result?

cacheKey is hash type.

Link to Minimal Reproducible Example

https://github.com/sgalcheung/starmgz

Participation

  • I am willing to submit a pull request for this issue.

Activity

  1. astrobot-houston commented on Aug 8, 2026

    @astrobot-houston
    Contributor
    • Reproduced: Yes
    • Exploration: Yes — View branch
    • Unit Test: No — this is a user code issue, not an Astro bug
    • Priority: P2: has workaround. This is not an Astro bug — the [object Object] value originates from the reporter's own code. The workaround is to use JSON.stringify() or a proper hash instead of String() when serializing complex objects for cacheKey.

    The [object Object] cacheKey value comes from your own code in src/pages/[magazine]/[slug].astro, where you write cacheKey: String(entry.data.sections). JavaScript's String() on an array of objects produces \"[object Object],[object Object]\" — it does not serialize the contents. By the time Astro receives this value, it's already a meaningless string. The cacheKey API expects a string | number (like entry.digest), and Astro's internal handling works correctly for those types.

    I wasn't able to find a fix, but I identified some areas that may be relevant. The immediate fix is in your project code — replace String(entry.data.sections) with JSON.stringify(entry.data.sections) (or better yet, compute a hash of your data). Astro could improve DX here by adding a runtime warning when a cacheKey contains [object Object] — the relevant code is in packages/astro/src/runtime/prerender/static-paths.ts line 123.

    Full Triage Report

    Issue Details

    Title: cacheKey generated an incorrect value in incremental static builds.

    Reporter: sgalcheung

    Environment:

    • Astro v7.2.0
    • Node v22.16.0
    • macOS (x64)
    • pnpm
    • Output: static
    • Integration: @astrojs/starlight

    Description: When using experimental.incrementalBuild: true, the cacheKey stored in the incremental build cache manifest (incremental-build.json) is \"[object Object]\" or \"[object Object], [object Object]\" instead of a proper hash or meaningful string value.

    Reproduction

    Created a minimal project with experimental.incrementalBuild: true and a dynamic route returning object values as cacheKey. The incremental build cache showed:

    {
      \"paths\": {
        \"/posts/post-1\": {
          \"cacheKey\": \"[object Object]\",
          \"outputFile\": \"posts/post-1/index.html\"
        },
        \"/posts/post-2\": {
          \"cacheKey\": \"[object Object],[object Object]\",
          \"outputFile\": \"posts/post-2/index.html\"
        }
      }
    }

    With proper string cacheKeys (e.g. 'post-1-v1'), the cache stores correct values.

    Root Cause

    The reporter's code in src/pages/[magazine]/[slug].astro:

    cacheKey: String(entry.data.sections),

    Where entry.data.sections is z.array(z.object({...})). String() on an array of objects produces \"[object Object],[object Object]\". This is a JavaScript behavior — String() does not serialize object contents.

    Astro's internal String() coercion in packages/astro/src/runtime/prerender/static-paths.ts line 123 is designed to normalize string | number → string, which works correctly for the documented input types. The cacheKey TypeScript type is string | number (defined in packages/astro/src/types/public/common.ts:18), and the documented usage is cacheKey: post.digest (a string hash).

    Verdict: Intended Behavior (User Error)

    Confidence: high

    The [object Object] value is produced by the reporter's own String() call, not by Astro. The API contract (string | number) is correctly defined and documented. The fix is to use JSON.stringify(entry.data.sections) or compute a proper hash.

    Enhancement opportunity: Astro could add a runtime warning when cacheKey contains [object Object], helping users catch this common serialization mistake. The relevant validation gap is in packages/astro/src/core/routing/validation.ts (validates params but not cacheKey).

    This report was made by an LLM. The analysis may be wrong, and the potential fix might not work, but is intended as a starting point for exploring the issue."

  2. added
    triage: unable to fix
    - P2: has workaround
    pkg: astro
    on Aug 8, 2026
  3. matthewp commented on Aug 8, 2026

    @matthewp
    Contributor

    It doesn't have to be a hash. Just any distinct string, it depends on your data.

  4. sgalcheung commented on Aug 8, 2026

    @sgalcheung
    Author

    OK, I won't change the type of cacheKey; I just want to be clear: it is a hash string.

  5. sgalcheung commented on Aug 8, 2026

    @sgalcheung
    Author

    cacheKey: staticPath.cacheKey === undefined ? undefined : String(staticPath.cacheKey),

    This cacheKey should handle a hash, not a string?

  6. matthewp commented on Aug 8, 2026

    @matthewp
    Contributor

    Any string. The user application provides cacheKey, Astro doesn't create it internally.

  7. sgalcheung commented on Aug 8, 2026

    @sgalcheung
    Author

    If the cacheKey is an object type, this will return [object Object]; this will never change it.
    I think the key point is whether we should handle different types for cacheKey. And finally, should this cacheKey be converted to a hash value?

  8. wildfiremedia commented on Aug 9, 2026

    @wildfiremedia

    cacheKey should stay string | number only. Accepting objects is basically inviting an [object Object] objection from every unwitting dev's build log.

  9. sgalcheung commented on Aug 9, 2026

    @sgalcheung
    Author

    Hi, @wildfiremedia, I moved some discussion in the general channel to a Thread in Discord(https://discord.com/channels/830184174198718474/1535882518660653116); you can see this!

  10. wildfiremedia commented on Aug 9, 2026

    @wildfiremedia

    @sgalcheung Yeah, read that when you first posted in Discord.

  11. matthewp commented on Aug 10, 2026

    @matthewp
    Contributor

    @sgalcheung Yeah agreed, would accept a PR to limit the type to just those two.

  12. sgalcheung commented on Aug 11, 2026

    @sgalcheung
    Author

    Do you mean cacheKey only accepts strings or numbers? why? I think in this case, a hash string would be better.
    I have some reasons here:

    1. Users only need to care about semantics, not implementation details.

    The essence of cacheKey is "the data version/identifier corresponding to this static path," not "a pre-hashable string."

    Users can pass any structured data (objects, arrays, numbers, strings, etc.).
    Internally, it is uniformly converted into a stable hash, avoiding the need for users to implement serialization/encryption themselves.

    This makes the API more intuitive: cacheKey is a "value," not a "hash value."

    1. Guaranteeing consistency and stability.

    If the hash logic were left to the user, many variations would occur:

    Some users would directly pass strings.

    Some users would implement their own JSON.stringify.

    Some users would use different hash algorithms.

    Some users would pass objects but without proper processing order. Internal processing ensures:

    The same input always generates the same hash.

    Cache conflicts or misses will not occur due to different user implementations.

    The same rules do not need to be implemented repeatedly in every integration.

    1. Reducing the risk of disruptive changes. Now, by handling hashes internally, the algorithm and format can be encapsulated within the framework.

    If we change it to "users must provide their own hash," then we'll need to:

    Expand the current hash algorithm

    Commit compatibility

    Or force users to adapt individually.
    This exposes internal implementation details as a public contract, increasing maintenance burden.

    1. Prevent users from inputting unhashable/insecure values.

    Currently, cacheKey is unknown, meaning users might pass:

    Objects
    Containing functions
    Nested structures
    Non-standard values. Internal processing can uniformly handle this:

    deterministicString(cacheKey)

    SHA-256

    generateContentHash

    Ensuring this value is safe and comparable within the internal cache.

    1. Better aligns with the design principle of "framework responsible for behavioral consistency."

    For internal caching mechanisms like prerender/incremental static builds, the framework itself is responsible for:

    Generating unique and stable cache keys

    Performing cache reuse based on the same semantics

    Managing consistency in the compilation pipeline. Leaving this part to the user forces them to bear the responsibility of "cache correctness," which should be the framework's responsibility.

  13. matthewp commented on Aug 12, 2026

    @matthewp
    Contributor

    Closing as this is based on a misunderstanding. The cacheKey is set by the user and is expected to be unique. It's up to each application to determine which key to use. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P2: has workaroundpkg: astrotriage: unable to fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions