[Next 9.5.5] res.setPreviewData only available in routes under /pages/api but not /api
See original GitHub issueBug report
Describe the bug
The example for preview mode indicates to test using res.setPreviewData
, and go to /api/preview
in the browser.
Currently, if your API folder is /api
, and not /pages/api
, this method does not work (returns TypeError: res.setPreviewData is not a function
), and you get a 502 error.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
- Clone this
create-next-app
repository (https://github.com/teamcandor/preview-data-replication) - Observe that the API folder is
./api
instead of./pages/api
- Run
vercel dev
, navigate tohttp://localhost:3000/api/preview
to encounter the error
Alternatively, I’ve deployed the repository as well here.
Expected behavior
- An error message in the terminal (
TypeError: res.setPreviewData is not a function
) - The method
res.setPreviewData
itself returnsundefined
if logged
Screenshots
System information
- OS: macOS Catalina
- Browser: Chrome
- Version of Next.js: 9.5.5
- Version of Node.js: 14.12.0
Additional context
Attempting to use Preview Mode in our application, example code fails - likely due to some failure to extend the res
object.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Advanced Features: Preview Mode
Step 1. Create and access a preview API route. Take a look at the API Routes documentation first if you're not familiar with...
Read more >How Next.js Preview Mode Works: An In-depth Guide
A tutorial to learn how to implement Preview Mode in Next.js and how it works, through a code walkthrough based on a real...
Read more >How to use Next.js API Routes?
Next.js has a page-based routing system, and an API route feature ... file found in the pages/api folder will be treated as an...
Read more >10 Next.js Tips You Might Not Know
Tip 1: Next.js Redirects. Next.js Redirects are new to version 9.5 (released in July 2020) and give the ability to reroute an incoming ......
Read more >Add a headless CMS to Next.js in 5 minutes
This package allows us to interact with the Storyblok API and will help us to enable ... the story with the slug home...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Makes a lot of sense, will be migrating our system over to
/pages/api
instead. Thanks for the help James!Correct, it would certainly be best while using a Next project to follow the convention of
pages/api
. I’m not certain of the underlying infrastructure for allowing both a Next app and serverless function to be run from the same project, but I can imagine it might lead to some more unexpected consequences eventually (similar to now with the lack ofsetPreviewData
being available in the Vercel serverless function).And additionally more feature may one day come to Next’s API routes and you’d likely miss out on those too being outside of the current directory structure requirements.