nextjs-notion-starter-kit
nextjs-notion-starter-kit copied to clipboard
Recently created pages are returning 404 error due memoized function
Description
Hey Travis 👋🏻, thanks for your amazing work in this boilerplate!
I was giving this starter a try and noticed an unexpected behavior. When creating new pages in Notion after the first build, these new pages return 404 error for a while.
This seems to be related with resolveNotionPage
function at line 51, precisely at this memoized getAllPages
. The pMemoize
cacheKey
property will always be the same because it's not bind to the requested page id.
As a result, the canonicalPageMap
will be stale.
Steps to reproduce
- Do a deploy
- Create a new page
- Wait NextJs 10 seconds revalidation time
- Try to access this new created page
- You should be redirect to 404
https://user-images.githubusercontent.com/13091635/171967292-da42a512-c0ea-426c-9e22-e8677536fc3d.mov
Proposed solution
I didn't find any infos about how p-memoize
invalidates it's cache in the docs, so a solution to this may be using expiry-map
and the same 10 seconds revalidate time.
const cache = new ExpiryMap(10000)
const getAllPages = pMemoize(getAllPagesImpl, {
cacheKey: (...args) => JSON.stringify(args),
cache
})
Just realized that expiry-map
is already in package dependencies, although it's not used anywhere.
@luizeboli I think you're correct in the issue and the cause. Not sure if I'll get to a fix anytime soon, however.
@transitive-bullshit I'm using the above proposed solution and it seems to be working fine. I can send you a PR if you prefer.
How did you solve this problem? Is this the only part of the code that's changed?
I want to make a mistake if I modify it like this.
I want to find your code to compare, but unfortunately I can't find it
@yunzhongci this is the commit:
https://github.com/luizeboli/js-felicio-dev/commit/9ace457980f7ce7b5b9be57b10ab7b46b8f8e388
@yunzhongci这是提交:
Thank you very much. Your way is very useful and has helped me solve this problem. thank you