From eacaeed1a66963a3644fe0641a2b854945a827b1 Mon Sep 17 00:00:00 2001 From: Kris Zyp Date: Mon, 2 Dec 2024 21:04:54 -0700 Subject: [PATCH 1/2] Remove caching --- config.yaml | 2 -- extension.js | 90 +++----------------------------------------------- schema.graphql | 5 --- 3 files changed, 5 insertions(+), 92 deletions(-) delete mode 100644 schema.graphql diff --git a/config.yaml b/config.yaml index ffa6246..6db953c 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1 @@ extensionModule: ./extension.js -graphqlSchema: - files: '*.graphql' diff --git a/extension.js b/extension.js index 696ee8a..fa7bf8c 100644 --- a/extension.js +++ b/extension.js @@ -7,8 +7,6 @@ import { createRequire } from 'node:module'; import shellQuote from 'shell-quote'; -const { NextCache } = databases.cache; - /** * @typedef {Object} ExtensionOptions - The configuration options for the extension. These are all configurable via `config.yaml`. * @property {string=} buildCommand - A custom build command. Default to `next build`. @@ -287,42 +285,11 @@ export function start(options = {}) { if (config.subPath && !request._nodeRequest.url.startsWith(`/${config.subPath}/`)) { return nextHandler(request); } - const handler = (nodeResponse) => { - // define a handler that will call the Next.js app, that can pass through to the cache resolver function - let nodeRequest = request._nodeRequest; - nodeRequest.url = config.subPath - ? nodeRequest.url.replace(new RegExp(`^\/${config.subPath}\/`), '/') - : nodeRequest.url; - return requestHandler(nodeRequest, nodeResponse, url.parse(nodeRequest.url, true)); - }; - if (config.cache && request.method === 'POST' && request.url === '/invalidate') { - // invalidate the cache - let last; - for await (let entry of NextCache.search([], { onlyIfCached: true, noCacheStore: true })) { - last = NextCache.delete(entry.id); - } - await last; - return { status: 200, headers: {}, body: 'Cache invalidated' }; - } - // check if the request is cacheable - if (request.method === 'GET' && config.cache) { - request.handler = handler; - // use our cache table - let response = await NextCache.get(request.url, request); - // if have cache miss, we let the handler actually directly write to the node response object - // and stream the results to the client, so we don't need to return anything here - if (!request._nodeResponse.writableEnded) { - // but if we have a cache hit, we can return the cached response - return { - status: 200, - headers: { ...response.headers.toJSON(), 'X-HarperDB-Cache': 'HIT' }, - body: response.content, - }; - } - } else { - // else we just let the handler write to the node response object - return handler(request._nodeResponse); - } + let nodeRequest = request._nodeRequest; + nodeRequest.url = config.subPath + ? nodeRequest.url.replace(new RegExp(`^\/${config.subPath}\/`), '/') + : nodeRequest.url; + return requestHandler(nodeRequest, request._nodeResponse, url.parse(nodeRequest.url, true)); }, { port: config.port } ); @@ -340,50 +307,3 @@ export function start(options = {}) { }, }; } - -/** - * Source the Next.js cache from request resolution using the passed in Next.js request handler, - * and intercepting the response to cache it. - */ -NextCache.sourcedFrom({ - async get(path, context) { - const request = context.requestContext; - return new Promise((resolve, reject) => { - const nodeResponse = request._nodeResponse; - if (!nodeResponse) return; - let cacheable; - // intercept the main methods to get and cache the response - const writeHead = nodeResponse.writeHead; - nodeResponse.writeHead = (status, message, headers) => { - nodeResponse.setHeader('X-HarperDB-Cache', 'MISS'); - if (status === 200) cacheable = true; - writeHead.call(nodeResponse, status, message, headers); - }; - const blocks = []; // collect the blocks of response data to cache - const write = nodeResponse.write; - nodeResponse.write = (block) => { - if (typeof block === 'string') block = Buffer.from(block); - blocks.push(block); - write.call(nodeResponse, block); - }; - const end = nodeResponse.end; - nodeResponse.end = (block) => { - // now we have the full response, cache it - if (block) { - if (typeof block === 'string') block = Buffer.from(block); - blocks.push(block); - } - end.call(nodeResponse, block); - if (!cacheable) context.noCacheStore = true; - // cache the response, with the headers and content - resolve({ - id: path, - headers: nodeResponse._headers, - content: blocks.length > 1 ? Buffer.concat(blocks) : blocks[0], - }); - }; - - request.handler(nodeResponse); - }); - }, -}); diff --git a/schema.graphql b/schema.graphql deleted file mode 100644 index e74b5c4..0000000 --- a/schema.graphql +++ /dev/null @@ -1,5 +0,0 @@ -type NextCache @table(database: "cache", expiration: 3600) @export { - id: ID @primaryKey - headers: Any - content: Bytes -} From 159740c6f4f8c5a1a5b07ca1972fe733d3f39ef9 Mon Sep 17 00:00:00 2001 From: Ethan Arrowood Date: Tue, 3 Dec 2024 09:11:50 -0700 Subject: [PATCH 2/2] remove more references to schema.graphql --- package.json | 3 +-- util/cache-bust.js | 1 - util/docker/base.dockerfile | 2 +- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ef40864..4823e59 100644 --- a/package.json +++ b/package.json @@ -29,8 +29,7 @@ "files": [ "config.yaml", "extension.js", - "cli.js", - "schema.graphql" + "cli.js" ], "scripts": { "format": "prettier .", diff --git a/util/cache-bust.js b/util/cache-bust.js index 75c5edd..d5a389e 100644 --- a/util/cache-bust.js +++ b/util/cache-bust.js @@ -24,7 +24,6 @@ export const MODULE_CACHE_BUST = getCacheBustValue([ 'config.yaml', 'cli.js', 'extension.js', - 'schema.graphql', 'package.json', ]); diff --git a/util/docker/base.dockerfile b/util/docker/base.dockerfile index a8153e1..df733ea 100644 --- a/util/docker/base.dockerfile +++ b/util/docker/base.dockerfile @@ -35,7 +35,7 @@ RUN mkdir -p /@harperdb/nextjs # Cache Bust copying project files ARG CACHE_BUST RUN echo "${CACHE_BUST}" -COPY config.yaml extension.js cli.js schema.graphql package.json /@harperdb/nextjs/ +COPY config.yaml extension.js cli.js package.json /@harperdb/nextjs/ # Install dependencies for the @harperdb/nextjs module RUN npm install -C /@harperdb/nextjs