nextjs-notion-starter-kit icon indicating copy to clipboard operation
nextjs-notion-starter-kit copied to clipboard

Recently created pages are returning 404 error due memoized function

Open luizeboli opened this issue 2 years ago • 6 comments

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

  1. Do a deploy
  2. Create a new page
  3. Wait NextJs 10 seconds revalidation time
  4. Try to access this new created page
  5. 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
})

luizeboli avatar Jun 03 '22 23:06 luizeboli

Just realized that expiry-map is already in package dependencies, although it's not used anywhere.

luizeboli avatar Jun 03 '22 23:06 luizeboli

@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 avatar Jun 10 '22 03:06 transitive-bullshit

@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.

luizeboli avatar Jun 10 '22 11:06 luizeboli

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 avatar Jul 21 '22 13:07 yunzhongci

@yunzhongci this is the commit:

https://github.com/luizeboli/js-felicio-dev/commit/9ace457980f7ce7b5b9be57b10ab7b46b8f8e388

luizeboli avatar Jul 21 '22 13:07 luizeboli

@yunzhongci这是提交:

luizeboli/js-felicio-dev@ 9ace457

Thank you very much. Your way is very useful and has helped me solve this problem. thank you

yunzhongci avatar Jul 26 '22 04:07 yunzhongci