vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.83k stars 26.96k forks source link

Rewrite fallback isn't executed when getStaticProps returns notFound #30725

Closed vetalstar closed 3 years ago

vetalstar commented 3 years ago

What version of Next.js are you using?

12.0.1

What version of Node.js are you using?

16.13.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

npm run dev

Describe the Bug

next.config.js

async rewrites() {
        return {
            afterFiles: [
                {
                    source: '/:path(.*)',
                    has: [
                        {
                            type: 'host',
                            value: '(?<host>.*)',
                        },
                    ],
                    destination: '/_hosts/:host/:path*',
                },
            ],
            fallback: [
                {
                    source: '/:path(.*)',
                    has: [
                        {
                            type: 'host',
                            value: '(?<host>.*)',
                        },
                    ],
                    destination: '/_hosts/:host/404',
                },
            ],
        }
    },

_hosts/[host]/catalog/[category].tsx

export const getStaticProps: GetStaticProps = async ({params}) => {
    const data: PropsQueryResponse = await request(process.env.API_URL, propsQuery, variables)

    if (!data.category) {
        return {
            notFound: true,
        }
    }

    return {
        props: {
            ...data,
        },
        revalidate: 60,
    }
}

export const getStaticPaths: GetStaticPaths = async () => {
    const data: PathsQueryResponse = await request(process.env.API_URL, pathsQuery)

    return {
        paths: [
           { params: { ... } }
        ],
        fallback: 'blocking',
    }
}

Also I have custom 404 page _hosts/[host]/404.tsx

When I return notFound, fallback rewrites aren't executed. So I can't rewrite pages with dynamic routes to the custom 404 page

Expected Behavior

Fallback rewrites should be executed when dynamic routes are not found

To Reproduce

N/A

ijjk commented 3 years ago

Hi, rewrites are not meant to be triggered when returning notFound: true currently. I'm going to close this as a duplicate of https://github.com/vercel/next.js/issues/26883

vetalstar commented 3 years ago

@ijjk is there any way to set a custom path to 404 page? As I had showed I have 404 page in the subfolder: _hosts/[host]/404.tsx

ijjk commented 3 years ago

Nested 404 pages are not currently available, you can conditionally render on the client based on the props returned from getStaticProps instead though.

vetalstar commented 3 years ago

I see. I've done so. Thank you

balazsorban44 commented 2 years ago

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.