Skip to content

Commit b9b658f

Browse files
Remove caching (#11)
* Remove caching * remove more references to schema.graphql --------- Co-authored-by: Ethan Arrowood <[email protected]>
1 parent 5519499 commit b9b658f

File tree

6 files changed

+7
-96
lines changed

6 files changed

+7
-96
lines changed

config.yaml

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

extension.js

Lines changed: 5 additions & 85 deletions
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-
});

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
"files": [
3030
"config.yaml",
3131
"extension.js",
32-
"cli.js",
33-
"schema.graphql"
32+
"cli.js"
3433
],
3534
"scripts": {
3635
"format": "prettier .",

schema.graphql

Lines changed: 0 additions & 5 deletions
This file was deleted.

util/cache-bust.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const MODULE_CACHE_BUST = getCacheBustValue([
2424
'config.yaml',
2525
'cli.js',
2626
'extension.js',
27-
'schema.graphql',
2827
'package.json',
2928
]);
3029

util/docker/base.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ RUN mkdir -p /@harperdb/nextjs
3535
# Cache Bust copying project files
3636
ARG CACHE_BUST
3737
RUN echo "${CACHE_BUST}"
38-
COPY config.yaml extension.js cli.js schema.graphql package.json /@harperdb/nextjs/
38+
COPY config.yaml extension.js cli.js package.json /@harperdb/nextjs/
3939

4040
# Install dependencies for the @harperdb/nextjs module
4141
RUN npm install -C /@harperdb/nextjs

0 commit comments

Comments
 (0)