transitive-bullshit / nextjs-notion-starter-kit

Deploy your own Notion-powered website in minutes with Next.js and Vercel.
https://transitivebullsh.it/nextjs-notion-starter-kit
MIT License
6.12k stars 5.21k forks source link

Recently created pages are returning 404 error due memoized function #319

Open luizeboli opened 2 years ago

luizeboli commented 2 years ago

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 commented 2 years ago

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

transitive-bullshit commented 2 years ago

@luizeboli I think you're correct in the issue and the cause. Not sure if I'll get to a fix anytime soon, however.

luizeboli commented 2 years ago

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

yunzhongci commented 2 years ago

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

luizeboli commented 2 years ago

@yunzhongci this is the commit:

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

yunzhongci commented 2 years ago

@yunzhongci这是提交:

luizeboli/js-felicio-dev@ 9ace457

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