builder icon indicating copy to clipboard operation
builder copied to clipboard

getPersonalizedURL not compatible with NextJS v13

Open MikeBLambert opened this issue 2 years ago • 2 comments

Describe the bug When running NextJS v13, getPersonalizedURL causes a request.cookies.entries is not a function error. getPersonalizedURL relies on the NextRequest, which has changed in NextJS 13. Cookies now have a name and a value prop, and there is no longer request.cookies.entries. To Reproduce Steps to reproduce the behavior:

  1. On an application running NextJS v13, run middleware using getPersonalizedURL following instruction in the documentation.

Expected behavior A personalized URL should be generated.

Additional context

Replacing

 const allCookies = Array.from(request.cookies.entries()).reduce(
    (acc, [key]) => ({
      ...acc,
      [key]: tryJsonParse(request.cookies.get(key)!),
    }),
    {}
  );

with

  const allCookies: Record<string, any> = {};
  for (const cookie of request.cookies) {
    const key = cookie[0];
    const value = request.cookies.get(key)?.value;
    if (value) {
      const parsedCookie = tryJsonParse(value);
      allCookies[key] = parsedCookie;
    }
  }

appears to solve the issue for us.

MikeBLambert avatar Jan 16 '23 16:01 MikeBLambert

@Ross-s can you please release this change in an updated NPM package? 2.0.0 on NPM still refers to the .entries() syntax (https://www.npmjs.com/package/@builder.io/personalization-utils?activeTab=code)

kylehotchkiss avatar Apr 03 '23 21:04 kylehotchkiss

@Ross-s can you please release this change in an updated NPM package? 2.0.0 on NPM still refers to the .entries() syntax (https://www.npmjs.com/package/@builder.io/personalization-utils?activeTab=code)

@teleaziz

Ross-s avatar Apr 04 '23 05:04 Ross-s