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

Docs: Static site cache data body property silently changed type from String to Buffer #74986

Closed
AyronK opened this issue Jan 16, 2025 · 0 comments
Labels
Documentation Related to Next.js' official documentation.

Comments

@AyronK
Copy link

AyronK commented Jan 16, 2025

What is the documentation issue?

Changelog nor upgrade guide did not mention that cache handler now sends page data as buffer instead of a string. Which seems like a trivial change for in-memory caches, it actually broke our Redis integration.

To make our upgrade from 14 -> 15 compatible with our cache handler we had to add a decorator that parses data.value.body from Buffer to String and back.

function createNext15BufferResolverHandler(handler) {
    return {
        async get(key, ctx) {
            const hit = await handler.get(key, ctx);
            if (hit?.value?.body) {
                return {
                    ...hit,
                    value: { ...hit.value, body: Buffer.from(hit.value.body, "utf-8") }, // Converts string from Redis handler to Buffer
                };
            }
            return hit;
        },

        async set(key, data) {
            try {
                await handler.set(
                    key,
                    data?.value?.body
                        ? {
                              ...data,
                              value: {
                                  ...data.value,
                                  body: data.value.body.toString(), // Converts Buffer from to save it as string in Redis
                              },
                          }
                        : data,
                );
            } catch (e) {
                console.error(e);
            }
        },

        async revalidateTag(tag) {
            await handler.revalidateTag(tag);
        },

        async delete(key) {
            await handler.delete(key);
        },
    };
}

I am not even sure if this is a missing documentation or bug. What is expected behaviour? Should it be Buffer in Next15 or is it unintentional change? If this is intentional, then please add a breaking change notice as this was not included in the changelog or upgrade guide.

Is there any context that might help us understand?

Example cache handler from Next documentation.

const cache = new Map()
 
module.exports = class CacheHandler {
  constructor(options) {
    this.options = options
  }
 
  async get(key) {
    // This could be stored anywhere, like durable storage
    return cache.get(key)
  }
 
  async set(key, data, ctx) {
    // This could be stored anywhere, like durable storage
    cache.set(key, {
      value: data,
      lastModified: Date.now(),
      tags: ctx.tags,
    })
  }
 
  async revalidateTag(tags) {
    // tags is either a string or an array of strings
    tags = [tags].flat()
    // Iterate over all entries in the cache
    for (let [key, value] of cache) {
      // If the value's tags include the specified tag, delete this entry
      if (value.tags.some((tag) => tags.includes(tag))) {
        cache.delete(key)
      }
    }
  }
 
  // If you want to have temporary in memory cache for a single request that is reset
  // before the next request you can leverage this method
  resetRequestCache() {}
}

Does the docs page already exist? Please link to it.

No response

@AyronK AyronK added the Documentation Related to Next.js' official documentation. label Jan 16, 2025
@AyronK AyronK closed this as not planned Won't fix, can't repro, duplicate, stale Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

No branches or pull requests

1 participant