From 91a0525edbd398a59afc07eb77d7f44dd051e69b Mon Sep 17 00:00:00 2001 From: = Date: Thu, 11 Aug 2022 10:52:24 -0400 Subject: [PATCH 1/2] fix: test title typo --- cypress/integration/default/env.spec.ts | 4 ++-- cypress/integration/default/script.spec.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cypress/integration/default/env.spec.ts b/cypress/integration/default/env.spec.ts index d43fb07fd0..173d92f6a5 100644 --- a/cypress/integration/default/env.spec.ts +++ b/cypress/integration/default/env.spec.ts @@ -1,4 +1,4 @@ -describe('Environment Varialbes', () => { +describe('Environment Variables', () => { beforeEach(() => { cy.visit('/env') }) @@ -7,4 +7,4 @@ describe('Environment Varialbes', () => { cy.findByText('Greetings Vi') cy.findByText('Everything worked') }) -}) \ No newline at end of file +}) diff --git a/cypress/integration/default/script.spec.ts b/cypress/integration/default/script.spec.ts index ca67e370db..f9f1369f09 100644 --- a/cypress/integration/default/script.spec.ts +++ b/cypress/integration/default/script.spec.ts @@ -1,4 +1,4 @@ -describe('Environment Varialbes', () => { +describe('Environment Variables', () => { beforeEach(() => { cy.intercept('GET', 'https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX').as('test-gtm') cy.visit('https://61f2c7c9fad2c70ee2737a81--netlify-plugin-nextjs-demo.netlify.app/script/') @@ -7,4 +7,4 @@ describe('Environment Varialbes', () => { it('should load a script after interactive', () => { cy.wait('@test-gtm') }) -}) \ No newline at end of file +}) From 5f561cc4ab7bb994c11db9fd5046ae27298b5d9e Mon Sep 17 00:00:00 2001 From: = Date: Thu, 11 Aug 2022 10:53:10 -0400 Subject: [PATCH 2/2] feat: add demo for fs cached responses --- demos/default/.gitignore | 1 + demos/default/package.json | 3 +- .../getStaticProps/cachedResponses/[id].js | 60 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 demos/default/pages/getStaticProps/cachedResponses/[id].js diff --git a/demos/default/.gitignore b/demos/default/.gitignore index 9743d0abe9..846e3779cd 100644 --- a/demos/default/.gitignore +++ b/demos/default/.gitignore @@ -36,3 +36,4 @@ yarn-error.log* netlify/functions .netlify/functions .netlify/cache +cachedResponses/* \ No newline at end of file diff --git a/demos/default/package.json b/demos/default/package.json index 0222dbc0e8..7f03422bac 100644 --- a/demos/default/package.json +++ b/demos/default/package.json @@ -34,6 +34,7 @@ "husky": "^7.0.4", "if-env": "^1.0.4", "npm-run-all": "^4.1.5", - "typescript": "^4.6.3" + "typescript": "^4.6.3", + "v8": "^0.1.0" } } diff --git a/demos/default/pages/getStaticProps/cachedResponses/[id].js b/demos/default/pages/getStaticProps/cachedResponses/[id].js new file mode 100644 index 0000000000..a5b338d12b --- /dev/null +++ b/demos/default/pages/getStaticProps/cachedResponses/[id].js @@ -0,0 +1,60 @@ +import Link from 'next/link' +import { writeFile, readFile } from 'fs/promises' +import path from 'path' +import { serialize, deserialize } from 'v8' + +const Show = ({ show, time }) => ( +
+

+ This page bulk fetches all data and individually caches that data to the filesystem during path generation in + getStaticPaths(). Each entry is pulled from the filesystem and referenced indivually in getStaticProps(). +

+

This is a common optimization pattern for large sites.

+

Ids 1 and 2 are prerendered and others should 404.

+
+ +

Show #{show.id}

+

{show.name}

+

Rendered at {time} (slowly)

+
+ + + Go back home + +
+) + +export async function getStaticPaths() { + // Set the paths we want to pre-render + const paths = [{ params: { id: '1' } }, { params: { id: '2' } }] + + // Series could be faster in some cases + Promise.all( + // This is a somewhat convoluted example because it's typically pagination (less network overhead) that leads people to this pattern. + paths.map(async (entry) => { + const { id } = entry.params + const res = await fetch(`https://api.tvmaze.com/shows/${id}`) + + const props = { + show: await res.json(), + time: new Date().toLocaleTimeString(), + } + + await writeFile(path.join(process.cwd(), `demos/default/cachedResponses/${id}.db`), serialize(props)) + }), + ) + + // We'll pre-render only these paths at build time. + // { fallback: false } means other routes should 404. + return { paths, fallback: false } +} + +export async function getStaticProps({ params: { id } }) { + const data = await readFile(path.join(process.cwd(), `demos/default/cachedResponses/${id}.db`)) + + return { + props: deserialize(data), + } +} + +export default Show