Skip to content

Commit 48c556d

Browse files
authored
fix 404 on index.json (#277)
1 parent d291e6a commit 48c556d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

examples/pages-router/src/pages/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import Nav from "@example/shared/components/Nav";
2-
import { Inter } from "next/font/google";
32
import Head from "next/head";
43

5-
const inter = Inter({ subsets: ["latin"] });
4+
// Not used, but necessary to get prefetching to work
5+
export function getStaticProps() {
6+
return {
7+
props: {
8+
hello: "world",
9+
},
10+
};
11+
}
612

713
export default function Home() {
814
return (

examples/pages-router/src/pages/isr/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { InferGetStaticPropsType } from "next";
2+
import Link from "next/link";
23

34
export async function getStaticProps() {
45
return {
@@ -12,5 +13,10 @@ export async function getStaticProps() {
1213
export default function Page({
1314
time,
1415
}: InferGetStaticPropsType<typeof getStaticProps>) {
15-
return <div className="flex">Time: {time}</div>;
16+
return (
17+
<div>
18+
<div className="flex">Time: {time}</div>
19+
<Link href="/">Home</Link>
20+
</div>
21+
);
1622
}

packages/open-next/src/adapters/routing/matcher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ export function fixDataPage(internalEvent: InternalEvent, buildId: string) {
223223
const dataPattern = `/_next/data/${buildId}`;
224224

225225
if (rawPath.startsWith(dataPattern) && rawPath.endsWith(".json")) {
226-
const newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");
226+
let newPath = rawPath.replace(dataPattern, "").replace(/\.json$/, "");
227+
newPath = newPath === "/index" ? "/" : newPath;
227228
query.__nextDataReq = "1";
228229
const urlQuery: Record<string, string> = {};
229230
Object.keys(query).forEach((k) => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, test } from "@playwright/test";
2+
3+
test("fix _next/data", async ({ page }) => {
4+
await page.goto("/");
5+
6+
const isrJson = page.waitForResponse("/_next/data/*/isr.json");
7+
await page.locator('[href="/isr/"]').click();
8+
const response = await isrJson;
9+
expect(response.ok()).toBe(true);
10+
expect(response.request().url()).toMatch(/\/_next\/data\/.*\/isr\.json$/);
11+
await page.waitForURL("/isr/");
12+
13+
const homeJson = page.waitForResponse("/_next/data/*/index.json");
14+
await page.locator('[href="/"]').click();
15+
const response2 = await homeJson;
16+
expect(response2.ok()).toBe(true);
17+
expect(response2.request().url()).toMatch(/\/_next\/data\/.*\/index\.json$/);
18+
await page.waitForURL("/");
19+
const body = await response2.json();
20+
expect(body).toEqual({ pageProps: { hello: "world" }, __N_SSG: true });
21+
});

0 commit comments

Comments
 (0)