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:
The pricing table uses Next.js's experimental caching directives to optimize performance:
"use cache"
- Enables caching for the entire component functioncacheLife()
- Configures cache timing:stale: 3600
(1 hour) - Data is considered fresh for 1 hourrevalidate: 30 * 24 * 3600
(30 days) - Background revalidation occurs every 30 days
cacheTag("pricing-table")
- Tags the cache for selective invalidation
- 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.
git clone https://github.com/clerk/cached-pricing-table
To run the example locally, you need to:
- Sign up for a Clerk account at https://clerk.com.
- Go to the Clerk dashboard and create an application.
- Set the required Clerk environment variables as shown in the example
env
file. - Go to "Billing" in your sidebar, create plans for your users and enable Billing.
npm install
the required dependencies.npm run dev
to launch the development server.
To learn more about Clerk and Next.js, check out the following resources:
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. 🚀
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!