Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 6acc057

Browse files
authored
fix(lambda-at-edge, e2e-tests): fix and add e2e tests for ISR pages when locales are used (#1622)
1 parent 9ad05e1 commit 6acc057

File tree

9 files changed

+396
-100
lines changed

9 files changed

+396
-100
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
describe("ISR Tests", () => {
2+
before(() => {
3+
cy.ensureAllRoutesNotErrored();
4+
});
5+
6+
describe("SSG page", () => {
7+
[
8+
{ path: "/revalidated-ssg-page", initialWaitSeconds: 0 },
9+
// Pre-rendered ISR page
10+
{ path: "/revalidated-ssg-pages/101", initialWaitSeconds: 0 },
11+
// Blocking dynamic generated page. As the page will be created and cached
12+
// on first request, we'll need to wait another 10+1 seconds to be sure
13+
// that we have exceeded the revalidate window.
14+
{ path: "/revalidated-ssg-pages/105", initialWaitSeconds: 11 },
15+
{ path: "/en-GB/revalidated-ssg-page", initialWaitSeconds: 0 },
16+
// Pre-rendered ISR page
17+
{ path: "/en-GB/revalidated-ssg-pages/101", initialWaitSeconds: 0 },
18+
// Blocking dynamic generated page. As the page will be created and cached
19+
// on first request, we'll need to wait another 10+1 seconds to be sure
20+
// that we have exceeded the revalidate window.
21+
{ path: "/en-GB/revalidated-ssg-pages/105", initialWaitSeconds: 11 },
22+
{ path: "/fr/revalidated-ssg-page", initialWaitSeconds: 0 },
23+
// Pre-rendered ISR page
24+
{ path: "/fr/revalidated-ssg-pages/101", initialWaitSeconds: 0 },
25+
// Blocking dynamic generated page. As the page will be created and cached
26+
// on first request, we'll need to wait another 10+1 seconds to be sure
27+
// that we have exceeded the revalidate window.
28+
{ path: "/fr/revalidated-ssg-pages/105", initialWaitSeconds: 11 }
29+
].forEach(({ path, initialWaitSeconds }) => {
30+
it(`serves the cached re-rendered page "${path}" after 2 reloads`, () => {
31+
// https://docs.cypress.io/guides/guides/test-retries#Can-I-access-the-current-attempt-counter-from-the-test
32+
const attempt = Cypress._.get(
33+
(cy as any).state("runnable"),
34+
"_currentRetry",
35+
0
36+
);
37+
38+
if (attempt) {
39+
// In retries we wait to ensure consistent, expired state
40+
cy.wait(11000);
41+
} else if (initialWaitSeconds) {
42+
// Here the page has never been generated
43+
cy.ensureRouteNotCached(path);
44+
cy.visit(path);
45+
cy.wait(initialWaitSeconds * 1000);
46+
}
47+
48+
// The initial load will have expired in the cache
49+
cy.ensureRouteNotCached(path);
50+
cy.visit(path);
51+
cy.location("pathname").should("eq", path);
52+
53+
cy.get("[data-cy=date-text]")
54+
.invoke("text")
55+
.then((text1) => {
56+
// Be sure that the regeneration has run and uploaded the file
57+
cy.wait(4000);
58+
// When we reload again the page still should not be cached as this
59+
// should be the first time its being served from the origin
60+
cy.ensureRouteNotCached(path);
61+
cy.reload();
62+
cy.get("[data-cy=date-text]")
63+
.invoke("text")
64+
.then((text2) => {
65+
// Check that the date text has changed since the initial page
66+
// load
67+
expect(text1).not.to.be.eq(text2);
68+
// The new date should be greater than the original
69+
expect(new Date(text2).getTime()).to.be.greaterThan(
70+
new Date(text1).getTime()
71+
);
72+
// Make sure the next load is cached
73+
cy.ensureRouteCached(path);
74+
cy.reload();
75+
});
76+
});
77+
78+
// Wait for the cache to expire after the 10s
79+
cy.wait(10000);
80+
cy.ensureRouteNotCached(path);
81+
cy.reload();
82+
});
83+
});
84+
});
85+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/// <reference types="next" />
22
/// <reference types="next/types/global" />
33
/// <reference types="next/image-types/global" />
4+
5+
// NOTE: This file should not be edited
6+
// see https://nextjs.org/docs/basic-features/typescript for more information.

packages/e2e-tests/next-app-with-locales/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"isomorphic-fetch": "2.2.1",
2323
"next-i18next": "^8.5.5",
24-
"next": "10.2.2",
24+
"next": "11.1.2",
2525
"react": "17.0.1",
2626
"react-dom": "17.0.1"
2727
},
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from "react";
2+
import { GetStaticPropsResult } from "next";
3+
4+
type SSGPageProps = {
5+
date: string;
6+
};
7+
8+
export default function RevalidatedSSGPage(props: SSGPageProps): JSX.Element {
9+
return (
10+
<React.Fragment>
11+
<div>
12+
<p data-cy="date-text">{props.date}</p>
13+
</div>
14+
</React.Fragment>
15+
);
16+
}
17+
18+
export function getStaticProps(): GetStaticPropsResult<SSGPageProps> {
19+
return {
20+
revalidate: 10,
21+
props: {
22+
date: new Date().toJSON()
23+
}
24+
};
25+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from "react";
2+
import {
3+
GetStaticPaths,
4+
GetStaticPropsContext,
5+
GetStaticPropsResult
6+
} from "next";
7+
8+
const sampleUserData = [
9+
{ id: 101, name: "Alice" },
10+
{ id: 102, name: "Bob" },
11+
{ id: 103, name: "Caroline" },
12+
{ id: 104, name: "Dave" }
13+
];
14+
15+
const newUser = { id: 105, name: "Henry" };
16+
17+
type SSGPageProps = {
18+
date: string;
19+
user?: typeof sampleUserData[number];
20+
};
21+
22+
export default function RevalidatedSSGPage(props: SSGPageProps): JSX.Element {
23+
return (
24+
<React.Fragment>
25+
<div>
26+
<p data-cy="date-text">{props.date}</p>
27+
<p data-cy="user-id-text">{props.user?.id ?? "No user found"}</p>
28+
</div>
29+
</React.Fragment>
30+
);
31+
}
32+
33+
export const getStaticPaths: GetStaticPaths = () => {
34+
// To simulate new data becoming available in a data source we only make the
35+
// `newUser` available when rendering on AWS, rather than at build time.
36+
const runningOnAws = !!process.env.AWS_LAMBDA_FUNCTION_NAME;
37+
const paths = sampleUserData.map((user) => ({
38+
params: { id: user.id.toString() }
39+
}));
40+
41+
if (runningOnAws) {
42+
paths.push({ params: { id: newUser.id.toString() } });
43+
}
44+
45+
return { paths, fallback: "blocking" };
46+
};
47+
48+
export function getStaticProps(
49+
context: GetStaticPropsContext<{ id: string }>
50+
): GetStaticPropsResult<SSGPageProps> {
51+
const users = [...sampleUserData, newUser];
52+
const user = users.find(
53+
({ id }) => context.params?.id.toString() === id.toString()
54+
);
55+
return {
56+
revalidate: 10,
57+
props: {
58+
date: new Date().toJSON(),
59+
user
60+
}
61+
};
62+
}

0 commit comments

Comments
 (0)