Skip to content

Commit 1ab8d82

Browse files
authored
fix: init blobStore in CacheHandler constructor and not global scope (#164)
1 parent e270811 commit 1ab8d82

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/run/handlers/cache.cts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Buffer } from 'node:buffer'
55
import { readFileSync } from 'node:fs'
66
import { join } from 'node:path/posix'
77

8-
import { getDeployStore } from '@netlify/blobs'
8+
import { getDeployStore, Store } from '@netlify/blobs'
99
import { purgeCache } from '@netlify/functions'
1010
import type { PrerenderManifest } from 'next/dist/build/index.js'
1111
import { NEXT_CACHE_TAGS_HEADER } from 'next/dist/lib/constants.js'
@@ -20,8 +20,6 @@ import type { CacheEntry } from '../../build/plugin-context.ts'
2020

2121
type TagManifest = { revalidatedAt: number }
2222

23-
export const blobStore = getDeployStore()
24-
2523
// load the prerender manifest
2624
const prerenderManifest: PrerenderManifest = JSON.parse(
2725
readFileSync(join(process.cwd(), '.next/prerender-manifest.json'), 'utf-8'),
@@ -39,18 +37,20 @@ function encodeBlobKey(key: string) {
3937
export class NetlifyCacheHandler implements CacheHandler {
4038
options: CacheHandlerContext
4139
revalidatedTags: string[]
40+
blobStore: Store
4241

4342
constructor(options: CacheHandlerContext) {
4443
this.options = options
4544
this.revalidatedTags = options.revalidatedTags
45+
this.blobStore = getDeployStore()
4646
}
4747

4848
async get(...args: Parameters<CacheHandler['get']>): ReturnType<CacheHandler['get']> {
4949
const [key, ctx = {}] = args
5050

5151
console.debug(`[NetlifyCacheHandler.get]: ${key}`)
5252

53-
const blob = (await blobStore.get(encodeBlobKey(key), {
53+
const blob = (await this.blobStore.get(encodeBlobKey(key), {
5454
type: 'json',
5555
})) as CacheEntry | null
5656

@@ -104,7 +104,7 @@ export class NetlifyCacheHandler implements CacheHandler {
104104

105105
console.debug(`[NetlifyCacheHandler.set]: ${key}`)
106106

107-
await blobStore.setJSON(encodeBlobKey(key), {
107+
await this.blobStore.setJSON(encodeBlobKey(key), {
108108
lastModified: Date.now(),
109109
value: data,
110110
})
@@ -119,7 +119,7 @@ export class NetlifyCacheHandler implements CacheHandler {
119119
}
120120

121121
try {
122-
await blobStore.setJSON(encodeBlobKey(tag), data)
122+
await this.blobStore.setJSON(encodeBlobKey(tag), data)
123123
} catch (error) {
124124
console.warn(`Failed to update tag manifest for ${tag}`, error)
125125
}
@@ -142,7 +142,7 @@ export class NetlifyCacheHandler implements CacheHandler {
142142
const cacheTags = [...tags, ...softTags]
143143
const allManifests = await Promise.all(
144144
cacheTags.map(async (tag) => {
145-
const res = await blobStore
145+
const res = await this.blobStore
146146
.get(encodeBlobKey(tag), { type: 'json' })
147147
.then((value: TagManifest) => ({ [tag]: value }))
148148
.catch(console.error)

0 commit comments

Comments
 (0)