opennextjs-aws: Image optimization fails due to version mismatch
I’m using OpenNext (v2.3.7) through the SST NextJsSite construct.
I recently had an issue where images loaded through the <Image> tag failed at the optimization step. After digging into the issue, I learned that Next changed the optimizer call signature in v14.1.1, hence #374 and #377. I was running v14.2.2.
Digging in further, I learned that the OpenNext image optimization plugin was not applied during my build step. This is because the plugin is gated behind a version check for v14.1.1. BUT when a Next version isn’t specified in the initial options, OpenNext attempts to extract it from package.json.
However, the version specified in package.json is not necessarily the version that’s installed. In my case, I had "next": "^14.0.4" in my package.json, but the actual installed version was v14.2.2. This resulted in a logic mismatch between versions, and the image optimizer function simply returned Cannot read properties of undefined (reading '0') every time it was invoked.
It seems like an appropriate solution would be to instead obtain the installed version number from package-lock.json or the module itself, but I defer to the original authors.
ETA: I was able to fix my original issue by bumping the next version in package.json to ^14.2.2.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (8 by maintainers)
@kelvinCJJ and anyone else who experiences this issue:
I’m simplifying things a bit, but this should cover most cases. You have two options.
Option 1
You may “pin” the
nextpackage to a specific version. This means that your package.json entry fornextshould NOT contain~or^.For example:
"next":"14.1.0"✅"next":"^14.1.0"❌ no good"next":"~14.1.0"❌ no goodOption 2
If you must continue to use
~or^in your package.json file, you must set the numeric portion of your version to something >=14.1.1.For example:
"next":"~14.1.1"✅"next":"^14.1.1"✅"next":"^14.2.0"✅"next":"~14.1.0"❌ no good"next":"^14.0.4"❌ no goodAfterwards
After adjusting your package.json file, you should consider performing a clean install of all of your dependencies.