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

Need support referring same singleton between different page's getStaticProps during building time. #72005

Open
PrinOrange opened this issue Oct 29, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)

Comments

@PrinOrange
Copy link

PrinOrange commented Oct 29, 2024

Link to the code that reproduces this issue

https://github.com/PrinOrange/need-support-singleton-for-different-page.git

To Reproduce

  1. Create a ts module, and set and export a singleton, which needs time-costing initializing.
const buildMySingleton = async ():Promise<{value:string}> => {
  return new Promise((resolve) => {
    console.log("\nStart initializing my singleton");

    // Here is a progress of initializing singleton that costs much time
    setTimeout(() => {
      resolve({ value: "data" });
    }, 20000);
  });
};

export const MySingleton = await buildMySingleton();
  1. Refer this singleton in different page's getStaticProps.

In PageA,

import { MySingleton } from "../lib/my-module";
export default function PageA(props) {
  return <div>PageA</div>;
}
export const getStaticProps = () => {
  const mySingleton = MySingleton;  // refer the singleton
  return { props: { value: mySingleton.value } };
};

And in PageB

import { MySingleton } from "../lib/my-module";
export default function PageB(props) {
  return <div>PageB</div>;
}
export const getStaticProps = () => {
  const mySingleton = MySingleton; // refer the singleton
  return { props: { value: mySingleton.value } };
};

And so on, refer this singleton in different pages

  1. Run npm run build to build the next.js project
  2. I find that the terminal outputs Start initializing my singleton many times

image

This means that in the SSG process of different pages, each page will initialize its own MySingleton, and not use the same MySingleton.

This will lead to a problem: if initializing MySingleton is a very time-consuming process, each page will generate its own MySingleton, and this unnecessary multiple initialization will seriously slow down the build performance.

In addition, and more seriously: if initializing the Singleton is not an idempotent operation (such as connecting to a database), then this will cause errors to occur.

Current vs. Expected behavior

My purpose is set a singleton in a module, and every page's getStaticProps should use same one singleton during building time. It means that the module singleton is supposed to be initialized once. Because the singleton initializing is very time-costing.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 36670
  Available CPU cores: 8
Binaries:
  Node: 22.6.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.0.1 // Latest available version is detected (15.0.1).
  eslint-config-next: N/A
  react: 18.3.1
  react-dom: 18.3.1
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Module Resolution

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

No response

@PrinOrange PrinOrange added the bug Issue was opened via the bug report template. label Oct 29, 2024
@github-actions github-actions bot added the Module Resolution Module resolution (CJS / ESM, module resolving) label Oct 29, 2024
@codemetic
Copy link

Same here. Maybe the next.config.ts option, cpus:1 may be helpful.

@PrinOrange
Copy link
Author

May be the singleton should be serializable and temporarily stored in the disk, and read them during the building time?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving)
Projects
None yet
Development

No branches or pull requests

2 participants