Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[appDir] Development Server throws Error: Dynamic server usage: previewData when using generateStaticParams with previewData from next/headers #43392

Closed
1 task done
brvnonascimento opened this issue Nov 25, 2022 · 4 comments · Fixed by #43395
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.

Comments

@brvnonascimento
Copy link
Contributor

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:14:30 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8103
Binaries:
  Node: 16.15.1
  npm: 8.11.0
  Yarn: 1.22.17
  pnpm: 7.14.1
Relevant packages:
  next: 13.0.6-canary.1
  eslint-config-next: 13.0.5
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Firefox 107.0

How are you deploying your application? (if relevant)

Vercel

Describe the Bug

Using generateStaticParams with previewData() will throw an error in development mode. It works fine in production.

Expected Behavior

We should be able to preview a statically generated page with previewData since this call does not make the page dynamic as calling cookies and headers does.

Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster

https://github.com/brvnonascimento/next-13-preview-data-error

To Reproduce

  1. Clone the reproduction repository
  2. Run yarn dev
  3. Visiting any route will give you Dynamic server usage: previewData
  4. Run yarn build && yarn start or visit the deployed version on Vercel
  5. Should work just fine

Screenshot 2022-11-25 at 14 41 27

import Image from "next/image";
import { previewData } from "next/headers";
import styles from "./page.module.css";

export default function Page() {
  const preview = previewData();

  console.log({ preview });

  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <h1 className={styles.title}>
          Welcome to <a href="https://nextjs.org">Next.js 13!</a>
        </h1>

        <p className={styles.description}>
          Get started by editing{" "}
          <code className={styles.code}>app/page.tsx</code>
        </p>

        <div className={styles.grid}>
          <a href="https://beta.nextjs.org/docs" className={styles.card}>
            <h2>Documentation &rarr;</h2>
            <p>Find in-depth information about Next.js 13</p>
          </a>

          <a
            href="https://github.com/vercel/next.js/tree/canary/examples"
            className={styles.card}
          >
            <h2>Examples &rarr;</h2>
            <p>Explore the Next.js 13 playground.</p>
          </a>

          <a
            href="https://vercel.com/templates/next.js/app-directory?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
            target="_blank"
            rel="noopener noreferrer"
            className={styles.card}
          >
            <h2>Deploy &rarr;</h2>
            <p>Deploy your Next.js site to a public URL with Vercel.</p>
          </a>
        </div>
      </main>

      <footer className={styles.footer}>
        <a
          href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
          target="_blank"
          rel="noopener noreferrer"
        >
          Powered by{" "}
          <span className={styles.logo}>
            <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
          </span>
        </a>
      </footer>
    </div>
  );
}

export const generateStaticParams = async () => {
  const paths = [
    {
      route: [""],
    },
    {
      route: ["about"],
    },
    {
      route: ["services"],
    },
  ];

  return paths;
};
@brvnonascimento brvnonascimento added the bug Issue was opened via the bug report template. label Nov 25, 2022
@balazsorban44 balazsorban44 added the area: app App directory (appDir: true) label Nov 28, 2022
ijjk added a commit that referenced this issue Dec 1, 2022
…rams and appDir (#43395)

fixes #43392 

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [] Related issues linked using `fixes #number`
- []
[e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [x] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Co-authored-by: JJ Kasper <jj@jjsweb.site>
@styled-man
Copy link

This exact bug occurs with the next-auth unstable_getServerSession.

It works perferctly fine in production mode, however development mode thows the Dynamic server usage error.
Commenting out the generateStaticParams function also seems to fix it.

// src/app/[auth]/layout.tsx
import { unstable_getServerSession } from "next-auth/next"
import { ReactNode } from "react"

interface LayoutProps {
    params: {
        auth: string
    }
    children: ReactNode
}

export default async function Layout({ params, children }: LayoutProps) {
     // ...
    const session = await unstable_getServerSession()
     // ...

    return children
}

export async function generateStaticParams() {
    return [{ auth: "login" }, { auth: "register" }]
}

image

@craftogrammer
Copy link

Same here, works perfectly fine in production mode.
image

@saurabhsri108
Copy link

Still the same in 2023

@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: app App directory (appDir: true) bug Issue was opened via the bug report template.
Projects
None yet
5 participants