Skip to content

Commit 98eb35f

Browse files
authoredMar 29, 2024
fix: don't mutate route data, instead create new object to set as blob (#391)
* test: add integration test for cacheable route handler response not produced at build * fix: don't mutate route data, instead create new object to set as blob
1 parent a5e9b11 commit 98eb35f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
 

‎src/run/handlers/cache.cts

+9-3
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,20 @@ export class NetlifyCacheHandler implements CacheHandler {
159159

160160
console.debug(`[NetlifyCacheHandler.set]: ${key}`)
161161

162+
let value = data
163+
162164
if (data?.kind === 'ROUTE') {
163-
// @ts-expect-error gotta find a better solution for this
164-
data.body = data.body.toString('base64')
165+
// don't mutate data, as it's used for the initial response - instead create a new object
166+
value = {
167+
...data,
168+
// @ts-expect-error gotta find a better solution for this
169+
body: data.body.toString('base64'),
170+
}
165171
}
166172

167173
await this.blobStore.setJSON(blobKey, {
168174
lastModified,
169-
value: data,
175+
value,
170176
})
171177

172178
if (data?.kind === 'PAGE') {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
3+
export function generateStaticParams() {
4+
return [{ slug: 'first' }, { slug: 'second' }]
5+
}
6+
7+
export const GET = (_req: NextRequest, { params }) => {
8+
return NextResponse.json({ params })
9+
}

‎tests/integration/cache-handler.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ describe('plugin', () => {
313313
expect(blobEntries.map(({ key }) => decodeBlobKey(key)).sort()).toEqual([
314314
'/404',
315315
'/api/revalidate-handler',
316+
'/api/static/first',
317+
'/api/static/second',
316318
'/index',
317319
'/revalidate-fetch',
318320
'/static-fetch-1',
@@ -444,4 +446,13 @@ describe('route', () => {
444446
).toBe(1)
445447
ctx.blobServerGetSpy.mockClear()
446448
})
449+
450+
test<FixtureTestContext>('cacheable route handler response not produced at build is served correctly', async (ctx) => {
451+
await createFixture('server-components', ctx)
452+
await runPlugin(ctx)
453+
454+
const call2 = await invokeFunction(ctx, { url: '/api/static/not-in-generateStaticParams' })
455+
456+
expect(call2.body).toBe('{"params":{"slug":"not-in-generateStaticParams"}}')
457+
})
447458
})

0 commit comments

Comments
 (0)
Please sign in to comment.