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

createClient breaks in production, but works in development mode (on same machine) #1936

Closed
johannsonic opened this issue Jun 8, 2023 · 7 comments

Comments

@johannsonic
Copy link

Expected Behavior

The contentful client should be created with the createClient method provided by the library.

Actual Behavior

The createClient function works in DEV mode but breaks in PROD mode (both on local machine).
The error says 'r is not a function'

Steps to Reproduce

Use the createClient function in PROD mode (npm run built && npm run start in nextjs)

Environment

Next.JS 12.3.0
React 18.2.0

I load the library only on the client (using a dynamic() call from the nextjs package.

  • Package Manager Version: 18.4.0
  • Browser Version: Arc 0.106.1
  • Operating System: Mac OS 13.2.1 (22D68)
  • Package Version: 10.2.3
@johannsonic
Copy link
Author

I just found out that swcMinify (in next.config.json) is responsible for the error

@marcolink
Copy link
Member

@johannsonic sorry to hear, but good you already found the cause!

@Techn1x
Copy link
Contributor

Techn1x commented Jan 19, 2024

I've just updated from v9 to v10 and have also encountered this issue, where I cannot import anything from the contentful package, createClient is not a function

Specifically, between v10.1.3 (works) and v10.1.4 (breaks) - v10.1.3...v10.1.4
(exactly when the package exports were changed to use *.min.js)

The config seems to be here

const browserBundle = copy(baseBundleConfig)
browserBundle.output.library = {
type: 'umd2',
name: 'contentful',
}
browserBundle.target = 'browserslist'
browserBundle.output.filename = `${baseFileName}.browser${PROD ? '.min' : ''}.js`
module.exports = [nodeBundle, browserBundle]

I suspect the minified version might be being built differently or not exported correctly but can't quite pinpoint why or how, can we reopen @marcolink ?

@Techn1x
Copy link
Contributor

Techn1x commented Jan 19, 2024

In the meantime, I'm using a pnpm patch to go back to how it was, to get around the issue in my builds

diff --git a/package.json b/package.json
index 3a16b52ac0965ca27f25ed9fac2363487ba5aad3..15c2c04544b95a6d23d04ae20ae6f6e2e45aced4 100644
--- a/package.json
+++ b/package.json
@@ -3,8 +3,8 @@
   "description": "Client for Contentful's Content Delivery API",
   "version": "10.6.16",
   "homepage": "https://www.contentful.com/developers/documentation/content-delivery-api/",
-  "main": "./dist/contentful.node.min.js",
-  "browser": "./dist/contentful.browser.min.js",
+  "main": "./dist/contentful.node.js",
+  "browser": "./dist/contentful.browser.js",
   "types": "./dist/types/index.d.ts",
   "browserslist": [
     ">0.3%",

@Techn1x
Copy link
Contributor

Techn1x commented Jan 19, 2024

I noticed that the check:browser script doesn't check the minfied version 🤔

cloned the repo, installed packages, did a build and adjusted the build script to also check the minified file

"postbuild": "npm run check:browser && npm run check:browser:min && npm run check:node && npm run test:demo-projects",
"check:browser": "es-check es2017 --module ./dist/contentful.browser.js",
"check:browser:min": "es-check es2017 --module ./dist/contentful.browser.min.js", // i added this command

Sure enough it succeeds for the standard file, but fails for the minified file

> contentful@0.0.0-determined-by-semantic-release postbuild
> npm run check:browser && npm run check:browser:min && npm run check:node && npm run test:demo-projects


> contentful@0.0.0-determined-by-semantic-release check:browser
> es-check es2017 --module ./dist/contentful.browser.js

info: ES-Check: there were no ES version matching errors!  🎉

> contentful@0.0.0-determined-by-semantic-release check:browser:min
> es-check es2017 --module ./dist/contentful.browser.min.js

error: ES-Check: there were 1 ES version matching errors.
info: 
          ES-Check Error:
          ----
          · erroring file: ./dist/contentful.browser.min.js
          · error: SyntaxError: Unexpected token (1:50841)
          · see the printed err.stack below for context
          ----

          SyntaxError: Unexpected token (1:50841)

@Techn1x
Copy link
Contributor

Techn1x commented Jan 19, 2024

added a failing scenario #2130

@Techn1x
Copy link
Contributor

Techn1x commented May 3, 2024

Can confirm this is no longer an issue in 10.9.0+ thanks a bundle+ 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants