Skip to content

Commit

Permalink
Refactor/Move custom incremental cache handler path to use ESM (#122)
Browse files Browse the repository at this point in the history
* 🚚 move custom incremental cache handler path to use ESM

* 👷 don't deploy on PR on old workflow

* 👷 log branch in CI

* 👷 update CI

* 💚 use correct branch naming
  • Loading branch information
Fredkiss3 authored Jan 5, 2024
1 parent bc07481 commit f1158b6
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 175 deletions.
File renamed without changes.
5 changes: 4 additions & 1 deletion .github/workflows/deploy-with-docker-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ jobs:
nvm use 20
echo Pulling latest version...
git pull origin ${GITHUB_REF##*/}
GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}
echo git branch=\$GIT_BRANCH
git pull origin \$GIT_BRANCH
git checkout \$GIT_BRANCH
echo 'Build with docker (and cache)...🔄'
export DOCKER_BUILDKIT=1
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/deploy-with-docker-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ jobs:
nvm use 20
echo Pulling latest version...
git pull origin ${GITHUB_REF##*/}
git pull origin main
git checkout main
echo 'Build with docker (and cache)...🔄'
export DOCKER_BUILDKIT=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: CI/CD For docker deploy
# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
pull_request:
# pull_request:
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down
File renamed without changes.
29 changes: 21 additions & 8 deletions webdis-cache-handler.js → custom-incremental-cache-handler.mjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// @ts-check
const {
import {
reviveFromBase64Representation,
replaceJsonWithBase64
} = require("@neshca/json-replacer-reviver");
const { IncrementalCache } = require("@neshca/cache-handler");
const { WebdisKV } = require("./webdis-kv");
} from "@neshca/json-replacer-reviver";
import { IncrementalCache } from "@neshca/cache-handler";
import { WebdisKV } from "./src/lib/server/kv/webdis.server.mjs";

/** @type {{ version: number, items: Record<string, {revalidatedAt: number}>}} */
const localTagsManifest = {
version: 1,
items: {}
};

const TAGS_MANIFEST_KEY = "sharedTagsManifest";
const CACHE_HANDLER_KEY = "incrementalCache";

/**
* @typedef {typeof CACHE_HANDLER_KEY} CACHE_HANDLER_KEY
*/

const client = new WebdisKV();
IncrementalCache.onCreation(() => {
Expand All @@ -20,22 +26,29 @@ IncrementalCache.onCreation(() => {
cache: {
async get(key) {
try {
const result = await client.get(key);
const result = /** @type {{ [CACHE_HANDLER_KEY]: string }} */ (
await client.get(key)
);

if (!result) {
return null;
}

// use reviveFromBase64Representation to restore binary data from Base64
return JSON.parse(result, reviveFromBase64Representation);
return JSON.parse(
result[CACHE_HANDLER_KEY],
reviveFromBase64Representation
);
} catch (error) {
return null;
}
},
async set(key, value) {
try {
// use replaceJsonWithBase64 to store binary data in Base64 and save space
await client.set(key, JSON.stringify(value, replaceJsonWithBase64));
await client.set(key, {
[CACHE_HANDLER_KEY]: JSON.stringify(value, replaceJsonWithBase64)
});
} catch (error) {
// ignore because value will be written to disk
}
Expand Down Expand Up @@ -73,4 +86,4 @@ IncrementalCache.onCreation(() => {
}
};
});
module.exports = IncrementalCache;
export default IncrementalCache;
8 changes: 5 additions & 3 deletions next.config.js → next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @ts-check
require("./src/env-config.js");
import "./src/env-config.mjs";

/** @type {import('next').NextConfig} */
module.exports = {
const nextConfig = {
reactStrictMode: true,
output: "standalone",
experimental: {
isrMemoryCacheSize: 0,
taint: true,
incrementalCacheHandlerPath:
process.env.NODE_ENV === "production"
? require.resolve("./webdis-cache-handler.js")
? "./custom-incremental-cache-handler.mjs"
: undefined
},
logging: {
Expand All @@ -28,3 +28,5 @@ module.exports = {
]
}
};

export default nextConfig;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"geist": "^1.0.0",
"isbot": "^3.6.13",
"nanoid": "^4.0.2",
"next": "14.0.4",
"next": "14.0.5-canary.38",
"nextjs-toploader": "^1.6.4",
"nprogress": "^0.2.0",
"pg": "^8.11.3",
Expand Down
86 changes: 43 additions & 43 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions src/env-config.js → src/env-config.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// @ts-check

const { createEnv } = require("@t3-oss/env-nextjs");
const { preprocess, z } = require("zod");
import { createEnv } from "@t3-oss/env-nextjs";
import { preprocess, z } from "zod";

/**
* BEWARE !!!
* This is only intended to be imported in `next.config.mjs`
* You should import from `src/env`.
* Modify this file if you want to add more env variables
*/
const _envObject = createEnv({
export const _envObject = createEnv({
server: {
SESSION_SECRET: z.string().min(32).max(32),
GITHUB_SECRET: z.string(),
Expand Down Expand Up @@ -48,6 +48,3 @@ const _envObject = createEnv({
GITHUB_PERSONAL_ACCESS_TOKEN: process.env.GITHUB_PERSONAL_ACCESS_TOKEN
}
});
module.exports = {
_envObject
};
Loading

0 comments on commit f1158b6

Please sign in to comment.