Skip to content

Commit b4ad0f0

Browse files
conico974vicb
andauthored
fix #648 (#709)
* fix #648 * changeset * fix incomplete version * fix issue, add comment and throw on incorrect params * Update packages/open-next/src/build/helper.ts Co-authored-by: Victor Berchet <[email protected]> * review --------- Co-authored-by: Victor Berchet <[email protected]>
1 parent 7eda030 commit b4ad0f0

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

.changeset/unlucky-olives-try.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: stableIncrementalCache is only used for Next.js >= 14.1

packages/open-next/src/build/createServerBundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ async function generateBundle(
179179
const isBefore13413 =
180180
buildHelper.compareSemver(options.nextVersion, "13.4.13") <= 0;
181181
const isAfter141 =
182-
buildHelper.compareSemver(options.nextVersion, "14.0.4") >= 0;
182+
buildHelper.compareSemver(options.nextVersion, "14.1") >= 0;
183183

184184
const disableRouting = isBefore13413 || config.middleware?.external;
185185

packages/open-next/src/build/helper.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ export function getNextVersion(appPath: string): string {
275275
return version.split("-")[0];
276276
}
277277

278+
/**
279+
* Compare two semver versions.
280+
*
281+
* @param v1 - First version. Can be "latest", otherwise it should be a valid semver version in the format of `major.minor.patch`. Usually is the next version from the package.json without canary suffix. If minor or patch are missing, they are considered 0.
282+
* @param v2 - Second version. Should not be "latest", it should be a valid semver version in the format of `major.minor.patch`. If minor or patch are missing, they are considered 0.
283+
* @example
284+
* compareSemver("1.0.0", "1.0.0") // 0
285+
*/
278286
export function compareSemver(v1: string, v2: string): number {
279287
if (v1 === "latest") return 1;
280288
if (/^[^\d]/.test(v1)) {
@@ -285,8 +293,11 @@ export function compareSemver(v1: string, v2: string): number {
285293
// biome-ignore lint/style/noParameterAssign:
286294
v2 = v2.substring(1);
287295
}
288-
const [major1, minor1, patch1] = v1.split(".").map(Number);
289-
const [major2, minor2, patch2] = v2.split(".").map(Number);
296+
const [major1, minor1 = 0, patch1 = 0] = v1.split(".").map(Number);
297+
const [major2, minor2 = 0, patch2 = 0] = v2.split(".").map(Number);
298+
if (Number.isNaN(major1) || Number.isNaN(major2)) {
299+
throw new Error("The major version is required.");
300+
}
290301

291302
if (major1 !== major2) return major1 - major2;
292303
if (minor1 !== minor2) return minor1 - minor2;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { compareSemver } from "@opennextjs/aws/build/helper.js";
2+
3+
// We don't need to test canary versions, they are stripped out
4+
describe("compareSemver", () => {
5+
it("should return 0 when versions are equal", () => {
6+
expect(compareSemver("1.0.0", "1.0.0")).toBe(0);
7+
});
8+
9+
it("should return 1 when first version is greater", () => {
10+
expect(compareSemver("1.0.1", "1.0.0")).toBe(1);
11+
});
12+
13+
it("should return -1 when first version is smaller", () => {
14+
expect(compareSemver("1.0.0", "1.0.1")).toBe(-1);
15+
});
16+
17+
it("should work with latest", () => {
18+
expect(compareSemver("latest", "1.0.0")).toBe(1);
19+
});
20+
21+
it("should work with incomplete version for patch", () => {
22+
expect(compareSemver("14.1.0", "14.1")).toBe(0);
23+
expect(compareSemver("14.1", "14.1.0")).toBe(0);
24+
});
25+
26+
it("should work with incomplete version for minor", () => {
27+
expect(compareSemver("14.0.0", "14")).toBe(0);
28+
});
29+
30+
it("should throw if the major version is missing", () => {
31+
expect(() => compareSemver("incorrect", "14.0.0")).toThrow();
32+
expect(() => compareSemver("14.0.0", "latest")).toThrow();
33+
});
34+
});

0 commit comments

Comments
 (0)