Skip to content

Commit b6b1fdb

Browse files
committed
feat: formatted page content for cache
1 parent 003cb93 commit b6b1fdb

File tree

4 files changed

+68
-25
lines changed

4 files changed

+68
-25
lines changed
File renamed without changes.

src/helpers/blobs/cacheFormat.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// eslint-disable-next-line unicorn/filename-case
2+
import type { OutgoingHttpHeaders } from 'http'
3+
import { join } from 'node:path'
4+
import path from 'path'
5+
6+
import pkg from 'fs-extra'
7+
8+
import { BUILD_DIR, SERVER_DIR } from '../constants.js'
9+
10+
// readfile not available in esm version of fs-extra
11+
const { readFile } = pkg
12+
13+
14+
export const removeFileDir = (file: string, num: number) => {
15+
return file.split('/').slice(num).join('/')
16+
}
17+
18+
19+
export const formatPageData = async (pathname: string, key: string, file: string) => {
20+
const isPage = pathname.startsWith('pages')
21+
const isApp = pathname.startsWith('app')
22+
const removedDir = removeFileDir(pathname, 1).replace(path.extname(file), '')
23+
// console.log({pathname, key, file, isPage, isApp, removedDir} )
24+
let data: any = {}
25+
if( isApp || isPage ){
26+
console.log("WIHTIN APP + PAGE CHECK")
27+
const getDataFile = async (files: string, appDir: boolean, ext: string) =>
28+
await readFile(
29+
join(SERVER_DIR, (appDir ? 'app' : 'pages'), `${files}.${ext}`),
30+
'utf8')
31+
32+
const pageData = isPage ? JSON.parse(await getDataFile(removedDir, false,'json')) : await getDataFile(removedDir, true, 'rsc')
33+
let meta: { status?: number, headers?: OutgoingHttpHeaders } = {}
34+
35+
try{
36+
meta = JSON.parse((await getDataFile(key, true, 'meta')))
37+
}catch{}
38+
39+
data = {
40+
lastModified: Date.now(),
41+
value: {
42+
kind: 'PAGE',
43+
html: await readFile(join(BUILD_DIR, file), 'utf8'),
44+
pageData,
45+
headers: meta.headers,
46+
status: meta.status,
47+
}
48+
}
49+
}
50+
return data
51+
}

src/helpers/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ export const PLUGIN_DIR = resolve(`${MODULE_DIR}../..`)
66
export const TASK_DIR = process.cwd()
77

88
export const BUILD_DIR = `${TASK_DIR}/.netlify/.next`
9-
export const SERVER_APP_DIR = `${BUILD_DIR}/server/app`
9+
export const SERVER_DIR = `${BUILD_DIR}/server`
1010
export const STANDALONE_BUILD_DIR = `${BUILD_DIR}/standalone`
11-
export const STANDALONE_APP_DIR = `${STANDALONE_BUILD_DIR}/.next/server/app`
11+
export const FETCH_CACHE_DIR = `${BUILD_DIR}/cache/fetch-cache`
1212

1313
export const FUNCTIONS_DIR = `${TASK_DIR}/.netlify/functions-internal`
1414
export const EDGE_FUNCTIONS_DIR = `${TASK_DIR}/.netlify/edge-functions`

src/helpers/files.ts

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2-
import { existsSync } from 'node:fs'
3-
import { resolve, join } from 'node:path'
42
import { cpus } from 'os'
3+
import path from 'path'
54

65
import { NetlifyPluginConstants } from '@netlify/build'
7-
import type { NetlifyConfig } from '@netlify/build'
8-
import pkg from 'fs-extra'
9-
import { copy, move, remove, readJSON } from 'fs-extra/esm'
6+
import { copy, move, remove } from 'fs-extra/esm'
107
import { globby } from 'globby'
118
import { outdent } from 'outdent'
129
import pLimit from 'p-limit'
1310

14-
import { netliBlob, getNormalizedBlobKey } from './blobs.cjs'
11+
import { getNormalizedBlobKey, netliBlob } from './blobs/blobs.cjs'
12+
import { removeFileDir, formatPageData } from './blobs/cacheFormat.js'
1513
import { BUILD_DIR, STANDALONE_BUILD_DIR } from './constants.js'
1614

17-
// readfile not available in esm version of fs-extra
18-
const { readFile } = pkg
1915
/**
2016
* Move the Next.js build output from the publish dir to a temp dir
2117
*/
@@ -57,10 +53,12 @@ export const storePrerenderedContent = async ({ NETLIFY_API_TOKEN, SITE_ID }:
5753
Uploading Files to Blob Storage...
5854
`)
5955

60-
const uploadFilesToBlob = async (key: string, file: string) => {
56+
const uploadFilesToBlob = async (pathName: string, file: string) => {
57+
const key = path.basename(pathName, path.extname(pathName))
58+
const content = await formatPageData(pathName, key, file)
59+
console.log(content)
6160
try{
62-
const content = await readFile(join(BUILD_DIR, file), 'utf8')
63-
await blob.set(getNormalizedBlobKey(key), content)
61+
await blob.setJSON(getNormalizedBlobKey(key), content)
6462
}catch(error){
6563
console.error(error)
6664
}
@@ -71,24 +69,18 @@ export const storePrerenderedContent = async ({ NETLIFY_API_TOKEN, SITE_ID }:
7169

7270
prerenderedContent.map((rawPath) => {
7371
const cacheFile = rawPath.startsWith('cache')
72+
const hasHtml = Boolean(rawPath.endsWith('.html'))
73+
7474
// Removing app, page, and cache/fetch-cache from file path
7575
const pathKey = removeFileDir(rawPath, (cacheFile ? 2 : 1))
76+
const errorPages = pathKey.includes('404') || pathKey.includes('500')
77+
7678
// Checking for blob access before uploading
77-
if( blob ){
79+
if( hasHtml && !errorPages ){
7880
return limit(uploadFilesToBlob, pathKey, rawPath)
7981
}
8082
})
81-
}
82-
83-
const removeFileDir = (file: string, num: number) => {
84-
return file.split('/').slice(num).join('/')
85-
}
86-
87-
const maybeLoadJson = <T>(path: string): Promise<T> | null => existsSync(path) ? readJSON(path) : null
88-
89-
// use this to load any manifest file by passing in name and/or dir if needed
90-
export const loadManifest = (netlifyConfig: NetlifyConfig, manifest: string, dir?: string): any =>
91-
maybeLoadJson(resolve(netlifyConfig.build.publish, dir ?? '', manifest))
83+
}
9284

9385
/**
9486
* Move static assets to the publish dir so they are uploaded to the CDN

0 commit comments

Comments
 (0)