bendotcodes / cookies

Load and save cookies within your Web application
MIT License
158 stars 18 forks source link

Cookie.get method errors with "TypeError: e is not a function" in production builds #469

Closed jaehanley closed 7 months ago

jaehanley commented 7 months ago

In universal-cookie, introduced with v7.0.0, when calling the get method from a Cookies object, production builds result in a TypeError, "TypeError: e is not a function". We haven't changed anything in terms of how we initialize or use the Cookies class or get method between v6 and v7, so I believe this is some sort of build issue that got introduced with the changes from v7.

const cookie = new Cookies();
const authCookie = cookie.get('COOKIE-STRING');

Within the built Cookies class constructor:

constructor(cookies, defaultSetOptions = {}) {
  this.changeListeners = [];
  this.HAS_DOCUMENT_COOKIE = false;
  this.update = () => {
    if (!this.HAS_DOCUMENT_COOKIE) {
      return;
    }
    const previousCookies = this.cookies;
    this.cookies = parse_1(document.cookie);
    // Fails here: TypeError: e is not a function
    this._checkChanges(previousCookies);
};
eXon commented 7 months ago

@jaehanley

What exact version are you using? Is it 7.1.0 or older?

What kind of build are you using? CommonJS? ES Module? UMD?

What version of NodeJS are you using?

What build system are you using and his version?

Also please provide a full example. I am not able to reproduce the issue.

Luke-SNAW commented 7 months ago

@eXon the same issue.

const cookies = new Cookies()
const cookieToken = cookies.get('token')

image

image

eXon commented 7 months ago

@Luke-SNAW By creating a new NextJS app, exporting it to static files, I was not able to reproduce the issue. I simply added your code to a useEffect and it was working as expected, without the error. Are you able to share a full project with the issue?

jaehanley commented 7 months ago

@eXon I've created this repository to reproduce the issue. When running development versions of Next 12, the package functions as expected, however when built and ran in production mode with yarn build && yarn start, the above error will manifest.

eXon commented 7 months ago

@jaehanley After investigating, it seems like the error happens when enabling swcMinify.

It minifies:

this.update = () => {
    if (!this.HAS_DOCUMENT_COOKIE) {
        return;
    }
    const previousCookies = this.cookies;
    this.cookies = parse_1(document.cookie);
    this._checkChanges(previousCookies);
};

Into:

this.update = ()=>{
    if (!this.HAS_DOCUMENT_COOKIE)
        return;
    let e = this.cookies;
    this.cookies = e(document.cookie),
    this._checkChanges(e)
}

It doesn't make any sense that e is this.cookies and used as a function. It must be a bug with the SWC version it is using.

The good news is this bug seems fixed in NextJS 13+. All I did is change the version and it worked. It means you have two choices:

  1. Disable swcMinify and stay on NextJS 12
  2. Upgrade to NextJS 13 or 14 and keep swcMinify

Let me know if I can do anything else for you