Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement custom cache handler logic #606

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

dario-piotrowicz
Copy link
Member

@dario-piotrowicz dario-piotrowicz commented Dec 20, 2023

resolves #529
resolves #628


Example app: https://github.com/dario-piotrowicz/next-geolocation-pages
(application using a supabase custom cache handler)

Vercel deployment: https://next-geolocation-pages-glqcs9da0-dario-piotrowicz.vercel.app/
Pages deployment: https://8e0d4e38.next-geolocation-pages.pages.dev/


TODO:

  • Next and next-on-pages seem to be calling the handler inconsistently, they should get aligned (example)
    (solved, see: dario-piotrowicz/next-geolocation-pages@a4eb865)
  • Add missing tsdocs and comments
  • Make sure that all different next.config.js files are correctly handled, this means function exports (as mentioned here and .mjs extensions)
  • Make sure that Cloudflare bindings are accessible from handlers (I can't see why not but it's still worth checking)
    (tested and everything seems to be in order, see: dario-piotrowicz/next-geolocation-pages@ac3f05e)
  • Update docs

TODO as a follow up, not to scope-creep this PR:

  • create a new next-on-pages submodule for caching utilities, it should export the KV handler and make it configurable so that developers can use it and set any binding name they want
  • explore if bindings in a cache handler can be used in next-dev, I suspect not but it's still worth exploring (if it's not possible it also needs to be properly documented)

Copy link

changeset-bot bot commented Dec 20, 2023

⚠️ No Changeset found

Latest commit: dc6770c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Dec 20, 2023

🧪 Prereleases are available for testing 🧪

@cloudflare/next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/7624406804/npm-package-next-on-pages-606

@cloudflare/eslint-plugin-next-on-pages

You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/next-on-pages/runs/7624406804/npm-package-eslint-plugin-next-on-pages-606

@dario-piotrowicz dario-piotrowicz marked this pull request as ready for review December 29, 2023 15:05
@dario-piotrowicz dario-piotrowicz force-pushed the custom-incremental-cache-handler branch 3 times, most recently from 859f576 to 5b98363 Compare January 2, 2024 11:31
Copy link
Collaborator

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - I left a bunch of nits but nothing blocking.
(Caveat - I have not actually tried this out locally!!)

packages/next-on-pages/build-metadata.d.ts Outdated Show resolved Hide resolved
packages/next-on-pages/src/buildApplication/nextConfig.ts Outdated Show resolved Hide resolved

test('processing a next.config.js file exporting a function', async () => {
mocks.nextConfigJsFileExists = true;
mocks.nextConfigMjsFileExists = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if both files exist??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both files exist Next.js picks the first/default one (next.config.js) and ignored the mjs one
(source)

We also do the same:

const configFilePath =
(await getConfigFilePath('js')) || (await getConfigFilePath('mjs'));

packages/next-on-pages/docs/caching.md Outdated Show resolved Hide resolved
packages/next-on-pages/docs/caching.md Show resolved Hide resolved
@dario-piotrowicz dario-piotrowicz force-pushed the custom-incremental-cache-handler branch 2 times, most recently from 72fb43d to 95cf34b Compare January 22, 2024 10:32
this includes:
 - refactoring existing logic so that it uses the incrementalCache class
   concept that Next.js also uses (so that we alight the two implementations)
 - reading the user next.config.(m)js file and create a custom cache
   handler and collecting the cache handler referenced in the
   incrementalCacheHandlerPath config
 - building the custom cache handler instead of the built-in ones in case one was
   indeed provided by the user
@james-elicx
Copy link
Contributor

james-elicx commented Jan 22, 2024

Revalidating a tag:

image

This logging can be enabled with the environment variable NEXT_PRIVATE_DEBUG_CACHE=true

Comment on lines +144 to +155
entry = {
isStale,
revalidateAfter,
value: null,
};

if (cacheData) {
entry.value = cacheData.value;
entry.age = age;
} else {
await this.set(cacheKey, entry.value, ctx);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I think this is incorrect

if cacheData is falsy we end up setting the cacheKay value to null here, which generates the error mentioned in #606 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dario-piotrowicz dario-piotrowicz marked this pull request as draft January 31, 2024 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants