Skip to content

Commit 5fbb113

Browse files
authored
Fix notfound meta for homepage (#933)
* fix wrong permalink for rootpage * not-found to use schema/not-found instead * add not-found.json
1 parent fd5d5cc commit 5fbb113

File tree

4 files changed

+93
-10
lines changed

4 files changed

+93
-10
lines changed

tooling/build/scripts/publisher.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ mv data/ ../../../template/
8383
cp sitemap.json ../../../template/public/
8484
mv sitemap.json ../../../template/
8585
cd ../../../template
86+
# Create not-found.json by copying _index.json if it doesn't exist
87+
# Refer to tooling/template/app/not-found.tsx for more context
88+
if [ ! -f "schema/not-found.json" ]; then
89+
echo "Creating not-found.json..."
90+
cp schema/_index.json schema/not-found.json
91+
fi
8692
echo $(pwd)
8793
echo "Fetching cached tooling-template node_modules..."
8894
TOOLING_TEMPLATE_NODE_MODULES_CACHE_PATH="s3://$S3_CACHE_BUCKET_NAME/$ISOMER_BUILD_REPO_BRANCH/isomer-tooling-template/node_modules.tar.gz"

tooling/template/app/[[...permalink]]/page.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,23 @@ import {
1515
export const dynamic = "force-static"
1616

1717
const INDEX_PAGE_PERMALINK = "_index"
18+
19+
interface ParamsContent {
20+
permalink: string[]
21+
}
1822
interface DynamicPageProps {
19-
params: Promise<{
20-
permalink: string[]
21-
}>
23+
params: Promise<ParamsContent>
24+
}
25+
26+
// Note: permalink should not be able to be undefined
27+
// However, nextjs had some magic props passing going on that causes
28+
// { permalink: [""] } to be converted to {}
29+
// Thus the patch is necessary to convert it back if its undefined
30+
const getPatchedPermalink = async (
31+
props: DynamicPageProps,
32+
): Promise<ParamsContent["permalink"]> => {
33+
const params = await props.params
34+
return params.permalink ?? [""]
2235
}
2336

2437
const timeNow = new Date()
@@ -29,9 +42,8 @@ const lastUpdated =
2942
" " +
3043
timeNow.getFullYear()
3144

32-
const getSchema = async (paramsPromise: DynamicPageProps) => {
33-
const { permalink } = await paramsPromise.params
34-
const joinedPermalink = !!permalink ? permalink.join("/") : ""
45+
const getSchema = async ({ permalink }: Pick<ParamsContent, "permalink">) => {
46+
const joinedPermalink: string = permalink.join("/")
3547

3648
const schema = (await import(`@/schema/${joinedPermalink}.json`)
3749
.then((module) => module.default)
@@ -56,7 +68,7 @@ const getSchema = async (paramsPromise: DynamicPageProps) => {
5668
// @ts-expect-error to fix when types are proper
5769
getSitemapXml(sitemap).find(
5870
({ url }) => joinedPermalink === url.replace(/^\//, ""),
59-
)?.lastModified || new Date().toISOString()
71+
).lastModified || new Date().toISOString()
6072

6173
schema.page.permalink = "/" + joinedPermalink
6274
schema.page.lastModified = lastModified
@@ -76,7 +88,9 @@ export const generateMetadata = async (
7688
props: DynamicPageProps,
7789
_parent: ResolvingMetadata,
7890
): Promise<Metadata> => {
79-
const schema = await getSchema(props)
91+
const schema = await getSchema({
92+
permalink: await getPatchedPermalink(props),
93+
})
8094
schema.site = {
8195
...config.site,
8296
environment: process.env.NEXT_PUBLIC_ISOMER_NEXT_ENVIRONMENT,
@@ -94,7 +108,9 @@ export const generateMetadata = async (
94108
}
95109

96110
const Page = async (props: DynamicPageProps) => {
97-
const renderSchema = await getSchema(props)
111+
const renderSchema = await getSchema({
112+
permalink: await getPatchedPermalink(props),
113+
})
98114

99115
return (
100116
<RenderEngine

tooling/template/app/not-found.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ export const generateMetadata = async (
2626
_props: never,
2727
_parent: ResolvingMetadata,
2828
): Promise<Metadata> => {
29-
const schema = (await import(`@/schema/_index.json`).then(
29+
// Context for using @/schema/not-found.json
30+
// For some next15 magical reason, using @/schema/_index.json will cause
31+
// duplicated generation of the homepage, resulting in wrong meta values
32+
// Suspected to be due to next15 changing app router SSG to render twice and in async manner
33+
// During deployment, publisher.sh duplicate homepage "_index.json" to "not-found.json"
34+
// For development, if `not-found.json` isn't found, simply manually copy and rename
35+
const schema = (await import(`@/schema/not-found.json`).then(
3036
(module) => module.default,
3137
)) as IsomerPageSchemaType
3238
schema.site = {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"version": "0.1.0",
3+
"layout": "homepage",
4+
"page": {
5+
"title": "Home"
6+
},
7+
"content": [
8+
{
9+
"type": "hero",
10+
"variant": "gradient",
11+
"backgroundColor": "black",
12+
"title": "Ministry of Trade and Industry",
13+
"subtitle": "A leading global city of enterprise and talent, a vibrant nation of innovation and opportunity",
14+
"buttonLabel": "Main CTA",
15+
"buttonUrl": "/",
16+
"secondaryButtonLabel": "Sub CTA",
17+
"secondaryButtonUrl": "/",
18+
"backgroundUrl": "https://ohno.isomer.gov.sg/images/hero-banner.png"
19+
},
20+
{
21+
"type": "infobar",
22+
"title": "This is an infobar",
23+
"description": "This is the description that goes into the Infobar section"
24+
},
25+
{
26+
"type": "infopic",
27+
"title": "This is an infopic",
28+
"description": "This is the description for the infopic component",
29+
"imageSrc": "https://placehold.co/600x400"
30+
},
31+
{
32+
"type": "keystatistics",
33+
"statistics": [
34+
{
35+
"label": "Average all nighters pulled in a typical calendar month",
36+
"value": "3"
37+
},
38+
{
39+
"label": "Growth in tasks assigned Q4 2024 (YoY)",
40+
"value": "+12.2%"
41+
},
42+
{
43+
"label": "Creative blocks met per single evening",
44+
"value": "89"
45+
},
46+
{
47+
"value": "4.0",
48+
"label": "Number of lies in this stat block"
49+
}
50+
],
51+
"variant": "top",
52+
"title": "Irrationality in numbers"
53+
}
54+
]
55+
}

0 commit comments

Comments
 (0)