Skip to content

Commit d808251

Browse files
authored
chore: use a deploy scoped blob storage (#26)
1 parent aec9c41 commit d808251

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

src/helpers/blobs.ts

-10
This file was deleted.

src/helpers/cache.ts

+23-18
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,31 @@ export const buildCacheValue = (path: string, ext: string) => {
2424
}
2525

2626
const buildPageCacheValue = async (path: string, appDir: boolean) => {
27-
const pageData = appDir
28-
? await readFile(`${BUILD_DIR}/.next/${path}.rsc`, 'utf8')
29-
: JSON.parse(await readFile(`${BUILD_DIR}/.next/${path}.json`, 'utf8'))
30-
let meta: MetaFile = {}
27+
try {
28+
const pageData = appDir
29+
? await readFile(`${BUILD_DIR}/.next/${path}.rsc`, 'utf8')
30+
: JSON.parse(await readFile(`${BUILD_DIR}/.next/${path}.json`, 'utf8'))
31+
let meta: MetaFile = {}
3132

32-
if (appDir) {
33-
try {
34-
meta = await JSON.parse(await readFile(`${BUILD_DIR}/.next/${path}.meta`, 'utf8'))
35-
} catch {}
36-
}
33+
if (appDir) {
34+
// eslint-disable-next-line max-depth
35+
try {
36+
meta = await JSON.parse(await readFile(`${BUILD_DIR}/.next/${path}.meta`, 'utf8'))
37+
} catch {}
38+
}
3739

38-
return {
39-
lastModified: Date.now(),
40-
value: {
41-
kind: 'PAGE',
42-
html: await readFile(`${BUILD_DIR}/.next/${path}.html`, 'utf8'),
43-
pageData,
44-
headers: meta.headers,
45-
status: meta.status,
46-
},
40+
return {
41+
lastModified: Date.now(),
42+
value: {
43+
kind: 'PAGE',
44+
html: await readFile(`${BUILD_DIR}/.next/${path}.html`, 'utf8'),
45+
pageData,
46+
headers: meta.headers,
47+
status: meta.status,
48+
},
49+
}
50+
} catch (error) {
51+
console.log(error)
4752
}
4853
}
4954

src/helpers/files.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { cpus } from 'os'
22
import path from 'path'
33

4+
import { getDeployStore } from '@netlify/blobs'
45
import { NetlifyPluginConstants } from '@netlify/build'
56
import { copy, move, remove } from 'fs-extra/esm'
67
import { globby } from 'globby'
78
import pLimit from 'p-limit'
89

9-
import { netliBlob } from './blobs.js'
1010
import { buildCacheValue } from './cache.js'
1111
import { BUILD_DIR } from './constants.js'
1212

@@ -45,18 +45,28 @@ const getPrerenderedContent = async (cwd: string, get = true): Promise<string[]>
4545
*/
4646
export const storePrerenderedContent = async ({
4747
NETLIFY_API_TOKEN,
48+
NETLIFY_API_HOST,
4849
SITE_ID,
49-
}: {
50-
NETLIFY_API_TOKEN: string
51-
SITE_ID: string
52-
}) => {
53-
const deployID = `${process.env.DEPLOY_ID}`
54-
const blob = netliBlob(NETLIFY_API_TOKEN, deployID, SITE_ID)
50+
}: NetlifyPluginConstants & { NETLIFY_API_TOKEN: string; NETLIFY_API_HOST: string }) => {
51+
if (!process.env.DEPLOY_ID) {
52+
// TODO: maybe change to logging
53+
throw new Error(
54+
'Could not initizlize the Blob storage as the `DEPLOY_ID` environment variable is missing!',
55+
)
56+
}
57+
58+
const blob = getDeployStore({
59+
deployID: process.env.DEPLOY_ID,
60+
siteID: SITE_ID,
61+
token: NETLIFY_API_TOKEN,
62+
apiURL: `https://${NETLIFY_API_HOST}`,
63+
})
64+
5565
// todo: Check out setFiles within Blobs.js to see how to upload files to blob storage
5666
const limit = pLimit(Math.max(2, cpus().length))
5767

5868
const prerenderedContent = await getPrerenderedContent(`${BUILD_DIR}/.next`)
59-
return Promise.all(
69+
return await Promise.all(
6070
prerenderedContent.map(async (rawPath: string) => {
6171
// TODO: test this with files that have a double extension
6272
const ext = path.extname(rawPath)

0 commit comments

Comments
 (0)