vercel / next.js

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

i18n routing disables incremental static regeneration #18602

Closed Timer closed 4 years ago

Timer commented 4 years ago

I'm trying to find out why static revalidation seems to be disabled while using i18n routing.

Here is a quick demo: https://nextjs-i18n-static-error.wiesson.vercel.app, here is the repo: https://github.com/wiesson/nextjs-i18n-static-error/blob/fix/pre-generation/pages/posts/%5Bslug%5D.js

https://nextjs-i18n-static-error.wiesson.vercel.app/posts/123_a or https://nextjs-i18n-static-error.wiesson.vercel.app/en/posts/123_a does not regenerate with the code below.

In order to trigger regeneration, I have added const revalidatedAt = new Date().toISOString() so that the props will be updated every time I visit the page.

import { useRouter } from "next/router";
import posts from "../../posts";

function Post(props) {
  const router = useRouter();
  if (router.isFallback) {
    return <div>Loading...</div>;
  }

  return (
    <div>
      Hello from {props.slug} with id: {props.id} at {props.revalidatedAt},
      locale {props.locale}
    </div>
  );
}

export const getStaticPaths = async ({ locales }) => {
  const paths = locales.reduce(
    (acc, next) => [
      ...acc,
      ...posts.map((slug) => ({
        params: {
          slug,
        },
        locale: next,
      })),
    ],
    []
  );

  return {
    paths,
    fallback: true,
  };
};

export const getStaticProps = async ({ params, locale }) => {
  const { slug } = params;

  if (!slug) {
    console.log("slug not found");
    console.log({ params, locale });

    return {
      notFound: true,
    };
  }

  const id = slug.match(/\d{3}/)[0];

  // fake some updated props to trigger regeneration
  const revalidatedAt = new Date().toISOString();
  const props = { slug, id, locale, revalidatedAt };

  console.log(props);

  return {
    props,
    revalidate: 1,
  };
};

export default Post;

I have noticed that during the build on vercel (or in general), the first item that getStaticProps receives is empty (but locale is set) and the paths from getStaticPaths are valid.

Originally posted by @wiesson in https://github.com/vercel/next.js/discussions/18443

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.