question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Next 9.5.5] res.setPreviewData only available in routes under /pages/api but not /api

See original GitHub issue

Bug 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:

  1. Clone this create-next-app repository (https://github.com/teamcandor/preview-data-replication)
  2. Observe that the API folder is ./api instead of ./pages/api
  3. Run vercel dev, navigate to http://localhost:3000/api/preview to encounter the error

Alternatively, I’ve deployed the repository as well here.

Expected behavior

  1. An error message in the terminal (TypeError: res.setPreviewData is not a function)
  2. The method res.setPreviewData itself returns undefined if logged

Screenshots

image

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
colin-betheacommented, Oct 14, 2020

Makes a lot of sense, will be migrating our system over to /pages/api instead. Thanks for the help James!

1reaction
jamesmosiercommented, Oct 14, 2020

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 of setPreviewData 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found