@@ -7,8 +7,6 @@ import { createRequire } from 'node:module';
7
7
8
8
import shellQuote from 'shell-quote' ;
9
9
10
- const { NextCache } = databases . cache ;
11
-
12
10
/**
13
11
* @typedef {Object } ExtensionOptions - The configuration options for the extension. These are all configurable via `config.yaml`.
14
12
* @property {string= } buildCommand - A custom build command. Default to `next build`.
@@ -287,42 +285,11 @@ export function start(options = {}) {
287
285
if ( config . subPath && ! request . _nodeRequest . url . startsWith ( `/${ config . subPath } /` ) ) {
288
286
return nextHandler ( request ) ;
289
287
}
290
- const handler = ( nodeResponse ) => {
291
- // define a handler that will call the Next.js app, that can pass through to the cache resolver function
292
- let nodeRequest = request . _nodeRequest ;
293
- nodeRequest . url = config . subPath
294
- ? nodeRequest . url . replace ( new RegExp ( `^\/${ config . subPath } \/` ) , '/' )
295
- : nodeRequest . url ;
296
- return requestHandler ( nodeRequest , nodeResponse , url . parse ( nodeRequest . url , true ) ) ;
297
- } ;
298
- if ( config . cache && request . method === 'POST' && request . url === '/invalidate' ) {
299
- // invalidate the cache
300
- let last ;
301
- for await ( let entry of NextCache . search ( [ ] , { onlyIfCached : true , noCacheStore : true } ) ) {
302
- last = NextCache . delete ( entry . id ) ;
303
- }
304
- await last ;
305
- return { status : 200 , headers : { } , body : 'Cache invalidated' } ;
306
- }
307
- // check if the request is cacheable
308
- if ( request . method === 'GET' && config . cache ) {
309
- request . handler = handler ;
310
- // use our cache table
311
- let response = await NextCache . get ( request . url , request ) ;
312
- // if have cache miss, we let the handler actually directly write to the node response object
313
- // and stream the results to the client, so we don't need to return anything here
314
- if ( ! request . _nodeResponse . writableEnded ) {
315
- // but if we have a cache hit, we can return the cached response
316
- return {
317
- status : 200 ,
318
- headers : { ...response . headers . toJSON ( ) , 'X-HarperDB-Cache' : 'HIT' } ,
319
- body : response . content ,
320
- } ;
321
- }
322
- } else {
323
- // else we just let the handler write to the node response object
324
- return handler ( request . _nodeResponse ) ;
325
- }
288
+ let nodeRequest = request . _nodeRequest ;
289
+ nodeRequest . url = config . subPath
290
+ ? nodeRequest . url . replace ( new RegExp ( `^\/${ config . subPath } \/` ) , '/' )
291
+ : nodeRequest . url ;
292
+ return requestHandler ( nodeRequest , request . _nodeResponse , url . parse ( nodeRequest . url , true ) ) ;
326
293
} ,
327
294
{ port : config . port }
328
295
) ;
@@ -340,50 +307,3 @@ export function start(options = {}) {
340
307
} ,
341
308
} ;
342
309
}
343
-
344
- /**
345
- * Source the Next.js cache from request resolution using the passed in Next.js request handler,
346
- * and intercepting the response to cache it.
347
- */
348
- NextCache . sourcedFrom ( {
349
- async get ( path , context ) {
350
- const request = context . requestContext ;
351
- return new Promise ( ( resolve , reject ) => {
352
- const nodeResponse = request . _nodeResponse ;
353
- if ( ! nodeResponse ) return ;
354
- let cacheable ;
355
- // intercept the main methods to get and cache the response
356
- const writeHead = nodeResponse . writeHead ;
357
- nodeResponse . writeHead = ( status , message , headers ) => {
358
- nodeResponse . setHeader ( 'X-HarperDB-Cache' , 'MISS' ) ;
359
- if ( status === 200 ) cacheable = true ;
360
- writeHead . call ( nodeResponse , status , message , headers ) ;
361
- } ;
362
- const blocks = [ ] ; // collect the blocks of response data to cache
363
- const write = nodeResponse . write ;
364
- nodeResponse . write = ( block ) => {
365
- if ( typeof block === 'string' ) block = Buffer . from ( block ) ;
366
- blocks . push ( block ) ;
367
- write . call ( nodeResponse , block ) ;
368
- } ;
369
- const end = nodeResponse . end ;
370
- nodeResponse . end = ( block ) => {
371
- // now we have the full response, cache it
372
- if ( block ) {
373
- if ( typeof block === 'string' ) block = Buffer . from ( block ) ;
374
- blocks . push ( block ) ;
375
- }
376
- end . call ( nodeResponse , block ) ;
377
- if ( ! cacheable ) context . noCacheStore = true ;
378
- // cache the response, with the headers and content
379
- resolve ( {
380
- id : path ,
381
- headers : nodeResponse . _headers ,
382
- content : blocks . length > 1 ? Buffer . concat ( blocks ) : blocks [ 0 ] ,
383
- } ) ;
384
- } ;
385
-
386
- request . handler ( nodeResponse ) ;
387
- } ) ;
388
- } ,
389
- } ) ;
0 commit comments