Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 16bf96c

Browse files
committed
Merge branch 'jonasbuntinx-master' into master (#39)
Fix rewrite for data route when pages/index.js uses getServerSideProps. See: #39 Thanks, @jonasbuntinx 🙌
2 parents 6d66e1b + 6783390 commit 16bf96c

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

lib/allNextJsPages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const getAllPages = () => {
5555
// the JSON data to the Netlify Function.
5656
const dataRoute = dataRoutes.find(({ page }) => page === route);
5757
if (dataRoute)
58-
alternativeRoutes.push(join("/_next/data", buildId, `${route}.json`));
58+
alternativeRoutes.push(join("/_next/data", buildId, `${route === "/" ? "/index" : route}.json`));
5959

6060
pages.push(new Page({ route, type, filePath, alternativeRoutes }));
6161
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Link from "next/link";
2+
3+
const Index = ({ show }) => (
4+
<div>
5+
<p>
6+
This page uses getServerSideProps() to fetch the show with the ID provided
7+
in the URL: /shows/:id
8+
<br />
9+
Refresh the page to see server-side rendering in action.
10+
<br />
11+
You can also try changing the ID to any other number between 1-10000.
12+
</p>
13+
14+
<hr />
15+
16+
<h1>Show #{show.id}</h1>
17+
<p>{show.name}</p>
18+
19+
<hr />
20+
21+
<Link href="/">
22+
<a>Go back home</a>
23+
</Link>
24+
</div>
25+
);
26+
27+
export const getServerSideProps = async ({ params }) => {
28+
const res = await fetch("https://api.tvmaze.com/shows/42");
29+
const data = await res.json();
30+
31+
return {
32+
props: {
33+
show: data,
34+
},
35+
};
36+
};
37+
38+
export default Index;
+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Test that next-on-netlify does not crash with a getServerSideProps index.js file.
2+
// See: https://github.com/netlify/next-on-netlify/pull/39
3+
4+
const { parse, join } = require("path");
5+
const {
6+
copySync,
7+
emptyDirSync,
8+
existsSync,
9+
readdirSync,
10+
readFileSync,
11+
readJsonSync,
12+
} = require("fs-extra");
13+
const npmRunBuild = require("./helpers/npmRunBuild");
14+
15+
// The name of this test file (without extension)
16+
const FILENAME = parse(__filename).name;
17+
18+
// The directory which will be used for testing.
19+
// We simulate a NextJS app within that directory, with pages, and a
20+
// package.json file.
21+
const PROJECT_PATH = join(__dirname, "builds", FILENAME);
22+
23+
// The directory that contains the fixtures, such as NextJS pages,
24+
// NextJS config, and package.json
25+
const FIXTURE_PATH = join(__dirname, "fixtures");
26+
27+
// Capture the output of `npm run build` to verify successful build
28+
let BUILD_OUTPUT;
29+
30+
beforeAll(
31+
async () => {
32+
// Clear project directory
33+
emptyDirSync(PROJECT_PATH);
34+
emptyDirSync(join(PROJECT_PATH, "pages"));
35+
36+
// Copy NextJS pages and config
37+
copySync(
38+
join(FIXTURE_PATH, "pages-with-gssp-index"),
39+
join(PROJECT_PATH, "pages")
40+
);
41+
copySync(
42+
join(FIXTURE_PATH, "next.config.js"),
43+
join(PROJECT_PATH, "next.config.js")
44+
);
45+
46+
// Copy package.json
47+
copySync(
48+
join(FIXTURE_PATH, "package.json"),
49+
join(PROJECT_PATH, "package.json")
50+
);
51+
52+
// Invoke `npm run build`: Build Next and run next-on-netlify
53+
const { stdout } = await npmRunBuild({ directory: PROJECT_PATH });
54+
BUILD_OUTPUT = stdout;
55+
},
56+
// time out after 180 seconds
57+
180 * 1000
58+
);
59+
60+
describe("Next", () => {
61+
test("builds successfully", () => {
62+
// NextJS output
63+
expect(BUILD_OUTPUT).toMatch("Creating an optimized production build...");
64+
expect(BUILD_OUTPUT).toMatch("Finalizing page optimization...");
65+
expect(BUILD_OUTPUT).toMatch("First Load JS shared by all");
66+
67+
// Next on Netlify output
68+
expect(BUILD_OUTPUT).toMatch("Next on Netlify");
69+
expect(BUILD_OUTPUT).toMatch("Success! All done!");
70+
});
71+
});
72+
73+
describe("Routing", () => {
74+
test("it routes page and data route to Netlify Function", async () => {
75+
// Read _redirects file
76+
let contents = readFileSync(
77+
join(PROJECT_PATH, "out_publish", "_redirects")
78+
).toString();
79+
80+
// Replace non-persistent build ID with placeholder
81+
contents = contents.replace(
82+
/\/_next\/data\/[^\/]+\//g,
83+
"/_next/data/%BUILD_ID%/"
84+
);
85+
86+
const redirects = contents.trim().split(/\n/);
87+
88+
expect(redirects[0]).toEqual("# Next-on-Netlify Redirects");
89+
expect(redirects[1]).toEqual("/ /.netlify/functions/next_index 200");
90+
expect(redirects[2]).toEqual(
91+
"/_next/data/%BUILD_ID%/index.json /.netlify/functions/next_index 200"
92+
);
93+
94+
// Check that no other redirects are present
95+
expect(redirects).toHaveLength(3);
96+
});
97+
});

0 commit comments

Comments
 (0)