From f6a8943b0f93dccc081226b5582b44ec8943e35f Mon Sep 17 00:00:00 2001 From: Roman Bobrovskiy Date: Wed, 27 Nov 2024 17:33:52 +0100 Subject: [PATCH] bug: fixed writing and reading data for nextjs --- src/cacheHandler/strategy/s3.ts | 23 +++++++++++------------ src/lambdas/edgeRouting.ts | 2 +- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/cacheHandler/strategy/s3.ts b/src/cacheHandler/strategy/s3.ts index d98593e..a4753a2 100644 --- a/src/cacheHandler/strategy/s3.ts +++ b/src/cacheHandler/strategy/s3.ts @@ -41,13 +41,13 @@ export class S3Cache implements CacheStrategy { ) } - async get(pageKey: string, cacheKey: string, ctx: CacheContext): Promise { + async get(pageKey: string, cacheKey: string): Promise { if (!this.client) return null const pageData = await this.client .getObject({ Bucket: this.bucketName, - Key: `${pageKey}/${cacheKey}.${ctx.isAppRouter ? CacheExtension.RSC : CacheExtension.JSON}` + Key: `${pageKey}/${cacheKey}.${CacheExtension.JSON}` }) .catch((error) => { if (NOT_FOUND_ERROR.includes(error.name)) return null @@ -58,7 +58,7 @@ export class S3Cache implements CacheStrategy { const response = await pageData.Body.transformToString('utf-8') - return ctx.isAppRouter ? response : JSON.parse(response) + return JSON.parse(response) } async set(pageKey: string, cacheKey: string, data: CacheEntry, ctx: CacheContext): Promise { @@ -82,6 +82,14 @@ export class S3Cache implements CacheStrategy { ContentType: 'text/html' }) ) + promises.push( + this.client.putObject({ + ...input, + Key: `${input.Key}.${CacheExtension.JSON}`, + Body: JSON.stringify(data), + ContentType: 'application/json' + }) + ) if (ctx.isAppRouter) { promises.push( this.client.putObject({ @@ -91,15 +99,6 @@ export class S3Cache implements CacheStrategy { ContentType: 'text/x-component' }) ) - } else { - promises.push( - this.client.putObject({ - ...input, - Key: `${input.Key}.${CacheExtension.JSON}`, - Body: JSON.stringify(data.value.pageData), - ContentType: 'application/json' - }) - ) } } else { promises.push( diff --git a/src/lambdas/edgeRouting.ts b/src/lambdas/edgeRouting.ts index 3ea8287..9380c47 100644 --- a/src/lambdas/edgeRouting.ts +++ b/src/lambdas/edgeRouting.ts @@ -97,7 +97,7 @@ export const handler = async ( if (isFileExists) { // Modify s3 path request - request.uri = `/${s3Key}${queryParams}` + request.uri = `/${s3Key}` // If file exists, allow the request to proceed to S3 callback(null, request)