content-collections icon indicating copy to clipboard operation
content-collections copied to clipboard

is of type 'unknown' issue with Next.js app dir generateStaticParams on deploy

Open umutbozdag opened this issue 9 months ago • 6 comments

I have followed the docs and in my slug page I want to get params with generateStaticParams,

My code:

import { allPosts } from "@/.content-collections/generated"
import { MDXContent } from "@content-collections/mdx/react"

export const generateStaticParams = () => {
  const posts = allPosts.map((p) => ({ slug: p._meta.path.split("/") }))

  return posts.map((post) => ({
    slug: post.slug[0],
  }))
}

export default function Page({ params }: { params: { slug: string } }) {
  const { slug } = params
  const post = allPosts.find(
    (p: {
      body: string
      content: string
      title: string
      summary: string
      authorName: string
      _meta: any
    }) => (p as any)._meta.path.split("/")[0] === slug
  )

  if (!post) {
    return <p>Post not found</p>
  }
  return (
    <div className="prose dark:prose-invert mx-auto mt-6 max-w-2xl">
      <MDXContent code={post.body} />
    </div>
  )
}

I got error Type error: 'p' is of type 'unknown'. when I try to deploy my site static site with export: static

My build script: "content-collections build && next build"

I think this is the simplest thing on earth that should work out of box, so frustrated for hours..

umutbozdag avatar May 01 '24 13:05 umutbozdag

This is the hardest shit ever lol, still can't find a way to solve

umutbozdag avatar May 01 '24 14:05 umutbozdag

@umutbozdag i can't reproduce your issue. Can you provide a full repository or a StackBlitz to reproduce the issue?

sdorra avatar May 01 '24 19:05 sdorra

@umutbozdag here is the repository where I tried to reproduce the error.

https://github.com/sdorra/cc-issue-84

sdorra avatar May 02 '24 11:05 sdorra

Hi @sdorra, it's probably due to Next.js version, I was using 13.4.8 when I got this error and when I upgrade to version 14.2.3, this error was gone but still I had some other issues with Next.js, not this package.

It was working very good in dev enviroment but somehow it didn't work in build.

I will try with your configuration with Next 13.4.8 now and let you know.

umutbozdag avatar May 03 '24 08:05 umutbozdag

It's definitely related to Next.js and TypeScript version. When I upgraded to Next.js 14 and TypeScript version 5, errors were gone. Also I have updated tsconfig based on your setup. moduleResolution: bundler can be effective here. @sdorra

However, I think it should work with Next.js 13 too.

umutbozdag avatar May 03 '24 08:05 umutbozdag

@umutbozdag i've tested with the latest next.js 13:

pnpm create [email protected] --use-pnpm --typescript --tailwind --app --eslint cc-issue-84-n13

And it comes with moduleResolution: bundler and typescript ^5. I think you can run next 13 with typescript 5 and moduleResolution: bundler. However, could you please post your TypeScript version and your tsconfig? I will try to analyze the problem.

sdorra avatar May 03 '24 19:05 sdorra

Closed, because of inactivity. Reopen the issue, if the problem still exists.

sdorra avatar Jun 01 '24 11:06 sdorra