nitro icon indicating copy to clipboard operation
nitro copied to clipboard

fix(cache): fixed `defineCachedFunction` to use the function name

Open zsilbi opened this issue 9 months ago • 3 comments

🔗 Linked issue

Fixes https://github.com/nitrojs/nitro/issues/3019

❓ Type of change

  • [x] 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

The provided function name was overwritten by the defaults. I fixed that by removing it from the defaults when it's merged with the options.

📝 Checklist

  • [x] I have linked an issue or discussion.

zsilbi avatar Mar 05 '25 15:03 zsilbi

This is valid fix thanks but it can cause cache busting (for any current usage) can you checm this?

In case of changing hashes we should do it for nitro v3 only (and all cache utils being externalized)

pi0 avatar Mar 05 '25 15:03 pi0

This is valid fix thanks but it can cause cache busting (for any current usage) can you checm this?

In case of changing hashes we should do it for nitro v3 only (and all cache utils being externalized)

Only those are affected by this who use defineCachedFunction() without providing the name option.

I think they might have already noticed that this function is not working correctly because the cacheKey for these is always /cache:nitro/functions:_:.json unless they provided something explicitly.

Cached event handlers provide the default name _, custom getKey() and custom integrity hash in opts. An example of a default cachedEventHandler before/after (no change):

// Before console.log([group, name, integrity], opts);
[ 'nitro/handlers', '_', 'lU0KWljkQ4' ] {
  name: '_',
  base: '/cache',
  swr: true,
  maxAge: 1,
  getKey: [AsyncFunction: getKey],
  validate: [Function: validate],
  group: 'nitro/handlers',
  integrity: 'lU0KWljkQ4'
}

// Cache key: /cache:nitro/handlers:_:index.il7asoJjJE.json
// After console.log([group, name, integrity], opts);
['nitro/handlers', '_', 'lU0KWljkQ4' ] {
  name: '_',
  base: '/cache',
  swr: true,
  maxAge: 1,
  getKey: [AsyncFunction: getKey],
  validate: [Function: validate],
  group: 'nitro/handlers',
  integrity: 'lU0KWljkQ4'
}

// Cache key: /cache:nitro/handlers:_:index.il7asoJjJE.json

zsilbi avatar Mar 05 '25 16:03 zsilbi

Any progress on this? I'm having the same issue. As a workaround, I'm using the name option.

HiroyukiYANO avatar Apr 17 '25 14:04 HiroyukiYANO