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.

Preview mode doesn't work for pages not specified by getStaticPaths

See original GitHub issue

Bug report

Describe the bug

Trying to access dynamic/catch-all routes in preview mode that are not specified by getStaticPaths returns 404 Not Found in production. I have verified that preview mode cookies are being sent on the requests, so that’s not the issue.

In development, the behaviour works as expected, though this might be due to the fact that getStaticProps is called on every request when in development mode.

To Reproduce

Repository: https://github.com/nextjs-preview-issue/nextjs-preview-issue

Demo: https://nextjs-preview-issue-demo.vercel.app

  • Accessing any pages not marked with “preview only” should render a page that has an appropriate title.
  • Accessing any pages marked with “preview only” should render a not found page.

Enable preview mode by clicking the “PREVIEW MODE” button and enter token1 or token2 into the prompt.

  • Accessing pages not marked with “preview only” should render a page that has an appropriate title with “(preview)” appended to it.
  • Accessing pages marked with “preview only” should render a draft page in preview mode, but it returns a not found page.

See “Additional context” for notable code snippets from the demo repository.

Expected behaviour

From the blog post:

Preview Mode allows users to bypass the statically generated page to on-demand render (SSR) a draft page from for example a CMS.

When trying to access pages that match a dynamic or catch-all route, I would expect that it always renders the page when in preview mode, even if it is not specified by getStaticPaths, because preview mode is intended for on-demand render of draft pages.

I considered using fallback: true in getStaticPaths, but that slightly changes behaviour in production regardless of preview mode - I shouldn’t need the server to check and render pages when not in preview mode.

Running the demo on a local machine in development mode using yarn dev exhibits the behaviour I expected.

System information

  • OS: macOS
  • Browser: tested on Chrome, Safari, Firefox
  • Version of Next.js: 9.5.2
  • Version of Node.js: 10.15.3

Additional context

Notable code snippets/files

Frontend: src/components/Preview.tsx

This function is attached to a button’s onClick handler which enables preview mode.

const previewMode = () => {
  const token = prompt("Enter preview token.");
  if (token) push(`/api/preview?token=${token}`)
};

Backend: src/pages/api/preview.ts

This API route enables preview mode and redirects the user to a page that matches the specified pageId if given, otherwise it redirects them to the index page. In the demo, the pageId parameter is not used (see above) so the user will always redirected to the index page when enabling preview mode.

Backend: src/utils.ts

This file contains the getStaticPaths and getStaticProps implementations used by the dynamic route (blog/[id].tsx) and the catch all route ([...slug].tsx).

The getStaticPaths implementations only lists pages/posts that have published: true because I do not want draft pages rendered at build time. getStaticProps retrieves the page’s/post’s data using the slug in the route params regardless of preview mode or not.

When in preview mode, getStaticProps passes the preview data to the page as the preview prop. The page will check this prop and render “(preview)” in the title when this is a non-null value.

See: Index page Blog post page Catch-all page

Backend: src/data.ts

This file contains the mock data used in the demo. In a real project these values would be retrieved from a database.

My use case and how I came across this issue

I’m building a page builder interface in my project and I want to allow users to preview pages they’ve created in the CMS that have not been published yet. Pages in my CMS database have a publishState flag. My getStaticPaths function only lists pages that have publishState set to "published" as I do not want draft pages to be rendered at build time. When draft pages are created, they have a publishState of "draft".

I deploy my project to Vercel and allow the users to publish new content by using a deploy hook to rebuild the project. Before the deploy hook is called, pages that are to be published have the state set to "published" so that getStaticPaths returns their ids for rendering during build time.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:26 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
Timercommented, Aug 30, 2020

There’s two ways to solve this:

  • Development should also 404 in preview mode for a fallback: false page and an unmatched slug
  • Production should match a path not returned from paths: [] even if fallback: false, making preview mode work like fallback: true

I’m not sure which is the most optimal fix, but we’ll think about this. Thanks for the issue!

3reactions
alexburnercommented, Mar 7, 2021

Update: I was able to enable the desired preview behavior, with only two small changes!

In this case, for a [slug].tsx page:

  1. Change fallback: false to fallback: 'blocking' in getStaticPaths()
  2. Return { notFound: true } from getStaticProps() if querying with context.params.slug fails to find data

Since our site is small, we’re doing full SSG instead of ISR, and this code has no impact on the normal production site.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Features: Preview Mode | Next.js
First, create a preview API route. It can have any name - e.g. pages/api/preview.js (or .ts if using TypeScript). In this API route,...
Read more >
next.js - Error 'getStaticPaths can only be used with dynamic ...
getStaticPaths defines which pages next.js has to render when exporting. It is used to generate all available dynamic routes.
Read more >
Preview feature not working in Production on drafts (NextJs)
Hello, I'm having some problems implementing previews on unpublished documents in production. In development it works as expected no issues ...
Read more >
How the new Next.js 9.3 Preview Mode works and why it's a ...
Explore with us how the new Next.js 9.3 Preview Mode works, and why it's a game-changer for content editors!
Read more >
Next.js + Contentful Preview Mode setup walkthrough - YouTube
One challenge when working with statically generated content is how to allow editors to preview their changes before publishing to ...
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