nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Caching proxyRequest

Open samydoesit opened this issue 2 years ago • 2 comments

Environment

Nitro: 2.2.2 Node: 18.14.0

Reproduction

Stackblitz Repro

Describe the bug

Hi i am trying to define a CachedEventHandler and want to cache a proxyRequest.

export default defineCachedEventHandler(
  (event) => {
    return proxyRequest(event, 'https://randomuser.me/api/');
  },
  {
    maxAge: 10,
  }
);

But it's always failing with a strange error.

Is there something wrong with the approach?

Additional context

No response

Logs

[nitro] [cache] TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor '_0x314c05'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at Module.stringify (file://file:///home/projects/github-vkrzub/node_modules/unstorage/dist/shared/unstorage.e9bf0023.mjs:43:17)
    at Object.setItem (file://file:///home/projects/github-vkrzub/node_modules/unstorage/dist/index.mjs:202:106)
    at _resolve (file://file:///home/projects/github-vkrzub/.nitro/dev/index.mjs:1191:24)
    at async eval (file://file:///home/projects/github-vkrzub/.nitro/dev/index.mjs:1209:19)
    at async eval (file://file:///home/projects/github-vkrzub/.nitro/dev/index.mjs:1350:22)
    at async Object.eval [as handler] (file://file:///home/projects/github-vkrzub/.nitro/dev/index.mjs:953:19)
    at async Server.toNodeHandle (file://file:///home/projects/github-vkrzub/.nitro/dev/index.mjs:1007:7)
[nitro] [request error] [unhandled] Cannot pipe, not readable
  at InternalError.get (https://githubvkrzub-kavc.w-credentialless.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:6:292489)  
  at H3Error.get (file://./.nitro/dev/index.mjs:319:24)  
  at normalizeError (file://./.nitro/dev/index.mjs:1448:24)  
  at Object.errorHandler [as onError] (file://./.nitro/dev/index.mjs:1468:57)  
  at Server.toNodeHandle (file://./.nitro/dev/index.mjs:1014:27)
[nitro] [dev] [unhandledRejection] Error [TypeError]: Cannot read properties of undefined (reading 'apply')
    at MessagePort._0x456ff7 (https://githubvkrzub-kavc.w-credentialless.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:8:174893)
[nitro] [dev] [unhandledRejection] Error [TypeError]: Cannot read properties of undefined (reading 'apply')
    at MessagePort._0x456ff7 (https://githubvkrzub-kavc.w-credentialless.staticblitz.com/blitz.927bd077ab89da68c6338c4363cc22e79cccb4ef.js:8:174893)

samydoesit avatar Feb 17 '23 16:02 samydoesit

Working on this

alex-key avatar Feb 26 '23 16:02 alex-key

hi,

any solution to the issue ?

geminigeek avatar Sep 12 '24 18:09 geminigeek

So there is no way to cache a proxy request using nitro? Does anyone have a workaround?

codeflorist avatar Jan 31 '25 00:01 codeflorist

Create your own proxy event handler and wrap it in a cachedFunction:

  export default defineEventHandler(async (event: H3Event): Promise<any> => {
    
    const { method } = event.node.req;
    const body = method !== "GET" && method !== "HEAD" ? await readBody(event) : undefined;
    
      return defineCachedFunction(
        async (event: H3Event) => {
          const response = await $fetch.raw(proxyUrl as NitroFetchRequest, {
            method: method as any,
            query: getQuery(event),
            headers: {
              ...getHeaders(event),
              Accept: "Application/json",
              "User-Agent": "Nitro",
            } as Record<string, string>,
            body,
          });
          return response._data;
        },
        {
          maxAge: 60 * 30,
          name: "proxyCache",
          getKey(event: H3Event) {
              return `default-${event.node.req.url}`;
          },
          swr: true,
          shouldBypassCache() {
            return false;
          },
        },
      )(event);
  });

juliavanmourik avatar Feb 14 '25 10:02 juliavanmourik