Skip to content

Commit 155db90

Browse files
committed
Remove caching
1 parent c9a6219 commit 155db90

File tree

3 files changed

+5
-92
lines changed

3 files changed

+5
-92
lines changed

config.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
extensionModule: ./extension.js
2-
graphqlSchema:
3-
files: '*.graphql'

extension.js

+5-85
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { createRequire } from 'node:module';
77

88
import shellQuote from 'shell-quote';
99

10-
const { NextCache } = databases.cache;
11-
1210
/**
1311
* @typedef {Object} ExtensionOptions - The configuration options for the extension. These are all configurable via `config.yaml`.
1412
* @property {string=} buildCommand - A custom build command. Default to `next build`.
@@ -287,42 +285,11 @@ export function start(options = {}) {
287285
if (config.subPath && !request._nodeRequest.url.startsWith(`/${config.subPath}/`)) {
288286
return nextHandler(request);
289287
}
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));
326293
},
327294
{ port: config.port }
328295
);
@@ -340,50 +307,3 @@ export function start(options = {}) {
340307
},
341308
};
342309
}
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-
});

schema.graphql

-5
This file was deleted.

0 commit comments

Comments
 (0)