svobik7 / next-roots

Next.js utility to generate i18n routes in the new APP directory.
https://next-roots-svobik7.vercel.app/
MIT License
178 stars 12 forks source link

Feature Request: Include `generateStaticParams` Function in Generated `layout.tsx` Files #245

Closed zto-sbenning closed 1 month ago

zto-sbenning commented 1 month ago

Feature Request: Include generateStaticParams Function in Generated layout.tsx Files

Description

There is an issue in the current version of the package where the generateStaticParams function is not being included in the generated layout.tsx files. This function is useful for specifying static parameters direclty from layout in Nextjs app router.

Steps to Reproduce

  1. Create a new nextjs application with npx create-next-app
  2. Install next-roots and its dependencies
  3. Provide a basic roots.config.js file
  4. Move src/app/layout.tsx and src/app/page.tsx under src/roots/
  5. Create a layout.tsx file under a src/roots/[category] directory.
  6. Export a generateStaticParams function from the layout.tsx file.
  7. Use the package to generate files under the src/app/ directory.

Example:

roots.config.js

const path = require('path')

module.exports = {
    originDir: path.resolve(__dirname, 'src/roots'),
    localizedDir: path.resolve(__dirname, 'src/app'),
    locales: ['en', 'cs'],
    defaultLocale: 'en',
    prefixDefaultLocale: false, // serves "en" locale on / instead of /en
}

src/roots/[category]/layout.tsx

export default function CategoryLayout({ children }: { children: React.ReactNode }) {
    return <>{children}</>
}

export async function generateStaticParams(props: any) {
    return [{ category: 'a' }, { category: 'b' }]
}

Generated src/app/(en)/[category]/layout.tsx


import CategoryLayoutOrigin from '../../../roots/[category]/layout'

export default function CategoryLayout(props:any) {
  {/* @ts-ignore */}
  return <CategoryLayoutOrigin {...props} locale="en" />
}

// `generateStaticParams` function is missing here

Expected Behavior

The generateStaticParams function should be included in the generated layout.tsx files.

Expected Generated src/app/(en)/[category]/layout.tsx

import CategoryLayoutOrigin from '../../../roots/[category]/layout'

export default function CategoryLayout(props:any) {
  {/* @ts-ignore */}
  return <CategoryLayoutOrigin {...props} locale="en" />
}

import { generateStaticParams as generateStaticParamsOrigin } from '../../../roots/[category]/layout'

export async function generateStaticParams() {
  return generateStaticParamsOrigin({ locale: 'en' })
}

Environment

Proposed Solution

Include the generateStaticParams function from the original layout.tsx file in the generated layout.tsx files.

Pull Request

I work on a PR for this.

Additional Context

Related to Issue #242


Feel free to reach out if you need any additional information or clarification regarding this issue.

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 3.9.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: