Skip to content

clerk/cached-pricing-table

Repository files navigation


Cached Pricing table in Next.js with Clerk Billing

Discord Twitter

Introduction

This template demonstrates how to implement a cached pricing table using Next.js's advanced caching features and Clerk's billing API. The implementation is found in app/cached-pricing-table.tsx and showcases several key concepts:

Caching Strategy

The pricing table uses Next.js's experimental caching directives to optimize performance:

  • "use cache" - Enables caching for the entire component function
  • cacheLife() - Configures cache timing:
    • stale: 3600 (1 hour) - Data is considered fresh for 1 hour
    • revalidate: 30 * 24 * 3600 (30 days) - Background revalidation occurs every 30 days
  • cacheTag("pricing-table") - Tags the cache for selective invalidation

Benefits

  • Performance: Cached data reduces API calls and improves page load times
  • Flexibility: Cache tags allow for selective cache invalidation when plans are updated
  • Fresh Data: Shows the last updated timestamp to indicate data freshness

This approach ensures that pricing information loads quickly while staying reasonably up-to-date, making it ideal for production applications where pricing data doesn't change frequently.

Running the template

git clone https://github.com/clerk/cached-pricing-table

To run the example locally, you need to:

  1. Sign up for a Clerk account at https://clerk.com.
  2. Go to the Clerk dashboard and create an application.
  3. Set the required Clerk environment variables as shown in the example env file.
  4. Go to "Billing" in your sidebar, create plans for your users and enable Billing.
  5. npm install the required dependencies.
  6. npm run dev to launch the development server.

Learn more

To learn more about Clerk and Next.js, check out the following resources:

Found an issue or have feedback?

If you have found an issue with this repo or have feedback, please join our Discord and create a new thread inside of our support channel.

If it's a quick fix, such as a misspelled word or a broken link, feel free to skip creating a thread. Go ahead and create a pull request with the solution. 🚀

Connect with us

You can discuss ideas, ask questions, and meet others from the community in our Discord.

If you prefer, you can also find support through our Twitter, or you can email us!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published