Skip to content

Commit 2445992

Browse files
chore: parse API pagination for getStaticPaths generation
1 parent 14371f9 commit 2445992

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

templates/next/pages/foos/[id]/edit.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,41 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
3232
{{{lc}}}: await fetch(`/{{{name}}}/${params.id}`),
3333
},
3434
revalidate: 1,
35-
}
35+
};
3636
}
3737

3838
export const getStaticPaths: GetStaticPaths = async () => {
3939
try {
4040
const response = await fetch("/{{{name}}}");
41+
} catch (e) {
42+
console.error(e);
4143

4244
return {
43-
paths: response.data["hydra:member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`),
45+
paths: [],
4446
fallback: true,
4547
};
46-
} catch (e) {
47-
console.error(e);
48+
}
49+
50+
const view = response.data['{{{hydraPrefix}}}view'];
51+
const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`);
52+
53+
if (view) {
54+
try {
55+
const {
56+
'{{{hydraPrefix}}}last': last
57+
} = view;
58+
for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) {
59+
paths.concat(
60+
await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`)
61+
);
62+
}
63+
} catch (e) {
64+
console.error(e);
65+
}
4866
}
4967

5068
return {
51-
paths: [],
69+
paths,
5270
fallback: true,
5371
};
5472
}

templates/next/pages/foos/[id]/index.tsx

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import { useMercure } from "../../../utils/mercure";
88

99
interface Props {
1010
{{{lc}}}: {{{ucf}}};
11-
hubURL: string;
11+
hubURL: ?string;
1212
};
1313

1414
const Page: NextComponentType<NextPageContext, Props, Props> = (props) => {
15-
const {{{lc}}} = useMercure(props.{{{lc}}}, props.hubURL);
15+
const {{{lc}}} = props.hubURL === null ? props.{{{lc}}} : useMercure(props.{{{lc}}}, props.hubURL);
1616

1717
if (!{{{lc}}}) {
1818
return <DefaultErrorPage statusCode={404} />;
@@ -39,23 +39,41 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
3939
hubURL: response.hubURL,
4040
},
4141
revalidate: 1,
42-
}
42+
};
4343
}
4444

4545
export const getStaticPaths: GetStaticPaths = async () => {
4646
try {
4747
const response = await fetch("/{{{name}}}");
48+
} catch (e) {
49+
console.error(e);
4850

4951
return {
50-
paths: response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => {{~lc}}['@id']),
52+
paths: [],
5153
fallback: true,
5254
};
53-
} catch (e) {
54-
console.error(e);
55+
}
56+
57+
const view = response.data['{{{hydraPrefix}}}view'];
58+
const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`);
59+
60+
if (view) {
61+
try {
62+
const {
63+
'{{{hydraPrefix}}}last': last
64+
} = view;
65+
for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) {
66+
paths.concat(
67+
await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`)
68+
);
69+
}
70+
} catch (e) {
71+
console.error(e);
72+
}
5573
}
5674

5775
return {
58-
paths: [],
76+
paths,
5977
fallback: true,
6078
};
6179
}

templates/next/utils/dataAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const fetch = async (id: string, init: RequestInit = {}) => {
3939
const json = await resp.json();
4040
if (resp.ok) {
4141
return {
42-
hubURL: extractHubURL(resp).toString(), // URL cannot be serialized as JSON, must be sent as string
42+
hubURL: extractHubURL(resp)?.toString(), // URL cannot be serialized as JSON, must be sent as string
4343
data: normalize(json),
4444
};
4545
}

0 commit comments

Comments
 (0)