NotionX / react-notion-x

Fast and accurate React renderer for Notion. TS batteries included. ⚡️
https://react-notion-x-demo.transitivebullsh.it
MIT License
4.75k stars 552 forks source link

Fix broken search function #505

Closed Armster15 closed 1 year ago

Armster15 commented 1 year ago

Description

Problem: The search function is currently broken: we get a mysterious 405 error when we prop open the dev console and inspect the search request:

An image showing https://transitivebullsh.it with a failed POST request to the search api route, with a status code of 405

It turns out that Notion slightly changed the API for search, resulting in the way the library notion-client sends the API request to Notion for searching to be broken and throw an error:

An image of Postman showing the result of the API request sent to Notion to search by notion-client

The request body in the above screenshot was generated by notion-client and I just put it in Postman to show the error that is thrown by the Notion API.

Luckily this was a quick fix! I went to Notion and performed a search and inspected how they sent the API request, and noticed a slight difference:

An image showing an API request to the Notion search API route by the Notion web app, which reveals a difference in the way the sort key is sent

For the sort key, notion-client currently sends the following:

"sort": "Relevance"

While the Notion web app sends:

"sort": {
    "field": "relevance"
}

So I changed notion-client to send the sort the same way the Notion web app sends it (which you can see in the code of this pull request) and the search function started working again!

The search function working again!

Notion Test Page ID

cebbcc4b7b784d63819ef7db02054b49

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
react-notion-x ✅ Ready (Inspect) Visit Preview Jul 7, 2023 8:47pm
react-notion-x-minimal-demo ✅ Ready (Inspect) Visit Preview Jul 7, 2023 8:47pm
hilmanski commented 1 year ago

thanks @Armster15 appreciate it

dnx04 commented 1 year ago

@transitive-bullshit please approve this pull request

transitive-bullshit commented 1 year ago

Thanks!

Jessie-jzn commented 1 month ago

the bug has fixed, but version: "6.16.1" donesn't publish at npm