From 4d206ac3ddb27d9df4db774e0f99920326ef2c81 Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Thu, 5 Sep 2024 13:22:21 +0200 Subject: [PATCH 1/3] Add node engine to package.jsons and gracefully handle when the PCC connection isn't correctly set up --- .../next.config.js | 8 +- .../nextjs-starter-approuter-ts/package.json | 3 + starters/nextjs-starter-ts/next.config.js | 8 +- starters/nextjs-starter-ts/package.json | 3 + .../pages/examples/ssg-isr/[uri].tsx | 102 ++++++++++-------- .../pages/examples/ssg-isr/index.tsx | 22 ++-- starters/nextjs-starter/next.config.js | 8 +- starters/nextjs-starter/package.json | 3 + 8 files changed, 95 insertions(+), 62 deletions(-) diff --git a/starters/nextjs-starter-approuter-ts/next.config.js b/starters/nextjs-starter-approuter-ts/next.config.js index f1d75f86..566ab0e5 100644 --- a/starters/nextjs-starter-approuter-ts/next.config.js +++ b/starters/nextjs-starter-approuter-ts/next.config.js @@ -9,12 +9,12 @@ require("dotenv").config({ function ensureEnvVariable(name) { if (process.env[name] == null) { if (process.env.NODE_ENV === "development") { - throw new Error( - `No ${name} found.\nSee the README.md for information on setting this variable locally.`, + console.error( + `No ${name} found.\nSee the README.md for information on setting this variable locally.` ); } else if (process.env.NODE_ENV === "production") { - throw new Error( - `No ${name} Endpoint found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.`, + console.warn( + `No ${name} environment variable found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.` ); } } diff --git a/starters/nextjs-starter-approuter-ts/package.json b/starters/nextjs-starter-approuter-ts/package.json index 884dbf10..d184f59d 100644 --- a/starters/nextjs-starter-approuter-ts/package.json +++ b/starters/nextjs-starter-approuter-ts/package.json @@ -22,6 +22,9 @@ "update-snapshots": "vitest run --update --silent", "coverage": "vitest run --coverage" }, + "engines": { + "node": ">=18.0.0" + }, "dependencies": { "@pantheon-systems/pcc-react-sdk": "~3.7.0", "@tailwindcss/typography": "^0.5.10", diff --git a/starters/nextjs-starter-ts/next.config.js b/starters/nextjs-starter-ts/next.config.js index f1d75f86..566ab0e5 100644 --- a/starters/nextjs-starter-ts/next.config.js +++ b/starters/nextjs-starter-ts/next.config.js @@ -9,12 +9,12 @@ require("dotenv").config({ function ensureEnvVariable(name) { if (process.env[name] == null) { if (process.env.NODE_ENV === "development") { - throw new Error( - `No ${name} found.\nSee the README.md for information on setting this variable locally.`, + console.error( + `No ${name} found.\nSee the README.md for information on setting this variable locally.` ); } else if (process.env.NODE_ENV === "production") { - throw new Error( - `No ${name} Endpoint found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.`, + console.warn( + `No ${name} environment variable found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.` ); } } diff --git a/starters/nextjs-starter-ts/package.json b/starters/nextjs-starter-ts/package.json index 27aeda44..b5608d9a 100644 --- a/starters/nextjs-starter-ts/package.json +++ b/starters/nextjs-starter-ts/package.json @@ -22,6 +22,9 @@ "update-snapshots": "vitest run --update --silent", "coverage": "vitest run --coverage" }, + "engines": { + "node": ">=18.0.0" + }, "dependencies": { "@pantheon-systems/pcc-react-sdk": "~3.7.0", "@pantheon-systems/pds-toolkit-react": "1.0.0-dev.55", diff --git a/starters/nextjs-starter-ts/pages/examples/ssg-isr/[uri].tsx b/starters/nextjs-starter-ts/pages/examples/ssg-isr/[uri].tsx index a7559868..7e3d47f7 100644 --- a/starters/nextjs-starter-ts/pages/examples/ssg-isr/[uri].tsx +++ b/starters/nextjs-starter-ts/pages/examples/ssg-isr/[uri].tsx @@ -62,61 +62,77 @@ export const getStaticProps: GetStaticProps<{}, { uri: string }> = async ({ }; } - const article = await PCCConvenienceFunctions.getArticleBySlugOrId(uri); + try { + const article = await PCCConvenienceFunctions.getArticleBySlugOrId(uri); + + if (!article) { + return { + notFound: true, + }; + } + + const recommendedArticles = + await PCCConvenienceFunctions.getRecommendedArticles(article.id); - if (!article) { + return { + props: { + article, + recommendedArticles, + }, + }; + } catch (e) { + console.error(e); return { notFound: true, }; } - - const recommendedArticles = - await PCCConvenienceFunctions.getRecommendedArticles(article.id); - - return { - props: { - article, - recommendedArticles, - }, - }; }; export const getStaticPaths: GetStaticPaths = async () => { - const publishedArticles = await PCCConvenienceFunctions.getAllArticles( - { - publishingLevel: "PRODUCTION", - }, - { - publishStatus: "published", - }, - ); - - const pagePaths = publishedArticles.map((article) => { - const id = article.id; - const slug = article.metadata.slug; - - // Generate both slug and id paths for each article - const paths = [ + try { + const publishedArticles = await PCCConvenienceFunctions.getAllArticles( { - params: { - uri: id, - }, + publishingLevel: "PRODUCTION", }, - ]; + { + publishStatus: "published", + }, + ); + + const pagePaths = publishedArticles.map((article) => { + const id = article.id; + const slug = article.metadata.slug; - if (slug) { - paths.push({ - params: { - uri: String(slug), + // Generate both slug and id paths for each article + const paths = [ + { + params: { + uri: id, + }, }, - }); - } + ]; + + if (slug) { + paths.push({ + params: { + uri: String(slug), + }, + }); + } - return paths; - }); + return paths; + }); - return { - paths: pagePaths.flat(), - fallback: "blocking", - }; + return { + paths: pagePaths.flat(), + fallback: "blocking", + }; + } catch (e) { + console.error(e); + + return { + paths: [], + fallback: "blocking", + }; + } }; diff --git a/starters/nextjs-starter-ts/pages/examples/ssg-isr/index.tsx b/starters/nextjs-starter-ts/pages/examples/ssg-isr/index.tsx index 349cff11..bf51fc30 100644 --- a/starters/nextjs-starter-ts/pages/examples/ssg-isr/index.tsx +++ b/starters/nextjs-starter-ts/pages/examples/ssg-isr/index.tsx @@ -34,12 +34,20 @@ export default function SSGISRExampleTemplate({ articles }) { } export async function getStaticProps() { - const articles = await PCCConvenienceFunctions.getAllArticles(); + try { + const articles = await PCCConvenienceFunctions.getAllArticles(); - return { - props: { - articles, - }, - revalidate: 60, - }; + return { + props: { + articles, + }, + revalidate: 60, + }; + } catch (e) { + console.error(e); + + return { + notFound: true, + }; + } } diff --git a/starters/nextjs-starter/next.config.js b/starters/nextjs-starter/next.config.js index f1d75f86..566ab0e5 100644 --- a/starters/nextjs-starter/next.config.js +++ b/starters/nextjs-starter/next.config.js @@ -9,12 +9,12 @@ require("dotenv").config({ function ensureEnvVariable(name) { if (process.env[name] == null) { if (process.env.NODE_ENV === "development") { - throw new Error( - `No ${name} found.\nSee the README.md for information on setting this variable locally.`, + console.error( + `No ${name} found.\nSee the README.md for information on setting this variable locally.` ); } else if (process.env.NODE_ENV === "production") { - throw new Error( - `No ${name} Endpoint found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.`, + console.warn( + `No ${name} environment variable found.\nLink to your PCC Instance or set the ${name} environment variable in the settings tab in the dashboard\nIf your site does not require a backend to build, remove this check from the next.config.js.` ); } } diff --git a/starters/nextjs-starter/package.json b/starters/nextjs-starter/package.json index 904a2149..8d2c6d65 100644 --- a/starters/nextjs-starter/package.json +++ b/starters/nextjs-starter/package.json @@ -22,6 +22,9 @@ "update-snapshots": "vitest run --update --silent", "coverage": "vitest run --coverage" }, + "engines": { + "node": ">=18.0.0" + }, "dependencies": { "@pantheon-systems/pcc-react-sdk": "~3.7.0", "@pantheon-systems/pds-toolkit-react": "1.0.0-dev.55", From f2ee6fa737a85b9766d8908f2d55dc2306ac3674 Mon Sep 17 00:00:00 2001 From: Kevin Stubbs Date: Thu, 5 Sep 2024 13:37:41 +0200 Subject: [PATCH 2/3] Fix typo in app router starter kit --- starters/nextjs-starter-approuter-ts/app/examples/page.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starters/nextjs-starter-approuter-ts/app/examples/page.tsx b/starters/nextjs-starter-approuter-ts/app/examples/page.tsx index 5297e740..4fc3d6da 100644 --- a/starters/nextjs-starter-approuter-ts/app/examples/page.tsx +++ b/starters/nextjs-starter-approuter-ts/app/examples/page.tsx @@ -13,7 +13,7 @@ export default function ExamplesPageTemplate() { This page outlines a growing list of common use cases that can be used as a reference when building using this starter kit. If you don't need them for your site, feel free to delete the - `pages/examples` directory in your codebase. + `app/examples` directory in your codebase.