diff --git a/src/logic/last-modified-middleware.ts b/src/logic/last-modified-middleware.ts index 51a4cb7..39cb068 100644 --- a/src/logic/last-modified-middleware.ts +++ b/src/logic/last-modified-middleware.ts @@ -14,28 +14,31 @@ export function lastModifiedMiddleware( const cacheControlHeader = `max-age=${options.maxAge}, stale-while-revalidate=${options.staleWhileRevalidate}, public` return async (context, next): Promise => { - // const lastModifiedTime = getLastModifiedTime() - // const ifModifiedSinceHeader = - // context.request.headers.get('If-Modified-Since') + const lastModifiedTime = getLastModifiedTime() + const lastModifiedHeader = new Date(lastModifiedTime).toUTCString() + const ifModifiedSinceHeader = + context.request.headers.get('If-Modified-Since') - // if (ifModifiedSinceHeader) { - // const ifModifiedSinceTime = Date.parse(ifModifiedSinceHeader) - // if ( - // !isNaN(ifModifiedSinceTime) && - // lastModifiedTime <= ifModifiedSinceTime - // ) { - // return { - // status: 304, - // headers: { - // 'Cache-Control': cacheControlHeader, - // }, - // } - // } - // } + if (ifModifiedSinceHeader) { + const ifModifiedSinceTime = Date.parse(ifModifiedSinceHeader) + if ( + !isNaN(ifModifiedSinceTime) && + lastModifiedTime <= ifModifiedSinceTime + ) { + return { + status: 304, + headers: { + 'Last-Modified': lastModifiedHeader, + 'Cache-Control': cacheControlHeader, + }, + } + } + } const response = await next() response.headers = { ...response.headers, + 'Last-Modified': lastModifiedHeader, 'Cache-Control': cacheControlHeader, } diff --git a/tests/logic/last-modified-middlware-logic.spec.ts b/tests/logic/last-modified-middlware-logic.spec.ts index 1b53ec6..0221f59 100644 --- a/tests/logic/last-modified-middlware-logic.spec.ts +++ b/tests/logic/last-modified-middlware-logic.spec.ts @@ -23,7 +23,7 @@ beforeEach(() => { middleware = lastModifiedMiddleware(mockedGetLastModifiedTime) }) -describe.skip('when handling a request with a If-Modified-Since header', () => { +describe('when handling a request with a If-Modified-Since header', () => { describe('when the If-Modified-Since header is a valid date', () => { beforeEach(() => { mockedRequest.headers.set( @@ -50,7 +50,7 @@ describe.skip('when handling a request with a If-Modified-Since header', () => { }) }) - describe.skip('when the If-Modified-Since header is the same as last modified time', () => { + describe('when the If-Modified-Since header is the same as last modified time', () => { beforeEach(() => { lastModifiedUTSCString = 'Tue, 20 Jan 1970 11:55:03 GMT' mockedResponse = { status: 200, body: 'ok' } @@ -110,7 +110,7 @@ describe.skip('when handling a request with a If-Modified-Since header', () => { }) }) -describe.skip('when handling a request without a If-Modified-Since header', () => { +describe('when handling a request without a If-Modified-Since header', () => { beforeEach(() => { lastModifiedUTSCString = 'Sun, 25 Jan 1970 10:00:03 GMT' mockedResponse = { status: 200, body: 'ok' } @@ -129,7 +129,7 @@ describe.skip('when handling a request without a If-Modified-Since header', () = }) }) -describe.skip('when setting the max age and the stale while revalidate options', () => { +describe('when setting the max age and the stale while revalidate options', () => { beforeEach(() => { lastModifiedUTSCString = 'Sun, 25 Jan 1970 10:00:03 GMT' mockedResponse = { status: 200, body: 'ok' } diff --git a/tests/modules/api.spec.ts b/tests/modules/api.spec.ts index 70262be..7c7cc79 100644 --- a/tests/modules/api.spec.ts +++ b/tests/modules/api.spec.ts @@ -320,7 +320,7 @@ beforeEach(async () => { }) describe('when fetching data', () => { - describe('and fetching the graph fails but the rental listings', () => { + describe('and fetching the graph fails', () => { beforeEach(() => { subgraphComponentMock.query = jest .fn() @@ -394,8 +394,10 @@ describe('when fetching data', () => { let sixthParcelEstate: ParcelFragment let fourthParcelRentalListing: RentalListing let fifthParcelEstateRentalListing: RentalListing + let maxDateInSeconds: number beforeEach(() => { + maxDateInSeconds = fromMillisecondsToSeconds(date) + 5 fourthParcel = { ...defaultParcel, id: 'parcel-0x0-3', @@ -405,6 +407,7 @@ describe('when fetching data', () => { searchParcelEstateId: null, tokenId: '3', activeOrder: null, + updatedAt: maxDateInSeconds.toString(), parcel: { ...defaultParcel.parcel, data: { @@ -422,6 +425,7 @@ describe('when fetching data', () => { searchParcelY: '3', searchParcelEstateId: 'estate-0x0-2', tokenId: '4', + updatedAt: (maxDateInSeconds - 1).toString(), parcel: { data: { name: 'Parcel 4', @@ -452,6 +456,7 @@ describe('when fetching data', () => { name: 'Parcel 5', searchParcelX: '5', searchParcelY: '3', + updatedAt: (maxDateInSeconds - 2).toString(), parcel: { ...fifthParcelEstate.parcel, data: { @@ -764,7 +769,7 @@ describe('when fetching data', () => { background_color: '000000', }, ], - updatedAt: Number(defaultParcel.updatedAt), + updatedAt: maxDateInSeconds, }) }) }) @@ -772,12 +777,20 @@ describe('when fetching data', () => { describe('when fetching update data', () => { let fstEstate: EstateFragment + let fstParcel: ParcelFragment + let fstParcelTile: Tile let defaultParcelRentalListing: RentalListing let fstEstateRentalListing: RentalListing + let maxDateInSeconds: number beforeEach(() => { + maxDateInSeconds = fromMillisecondsToSeconds(date) + fstParcel = { + ...defaultParcel, + updatedAt: (maxDateInSeconds - 1).toString() + } fstEstate = { - updatedAt: fromMillisecondsToSeconds(date).toString(), + updatedAt: maxDateInSeconds.toString(), estate: { parcels: [ { nft: defaultFstParcelEstate }, @@ -785,6 +798,10 @@ describe('when fetching update data', () => { ], }, } + fstParcelTile = { + ...defaultParcelTile, + updatedAt: maxDateInSeconds - 1 + } defaultParcelRentalListing = { id: 'defaultParcelRentalId', status: RentalStatus.OPEN, @@ -975,7 +992,7 @@ describe('when fetching update data', () => { describe('and there are parcels and estates to be updated', () => { beforeEach(() => { subgraphComponentMock.query = jest.fn().mockResolvedValueOnce({ - parcels: [defaultParcel], + parcels: [fstParcel], estates: [fstEstate], }) rentalsComponentMock.getUpdatedRentalListings = jest @@ -987,7 +1004,7 @@ describe('when fetching update data', () => { return expect(apiComponent.fetchUpdatedData(100000, {})).resolves.toEqual( { tiles: [ - defaultParcelTile, + fstParcelTile, defaultFstParcelEstateTile, defaultSndParcelEstateTile, ], @@ -997,7 +1014,7 @@ describe('when fetching update data', () => { defaultSndParcelEstateNFT, ], estates: [fstEstateNFT], - updatedAt: Number(fstEstate.updatedAt), + updatedAt: maxDateInSeconds, } ) })