It is well known that even if a module is import
ed multiple times in a JavaScript program, it only runs once (for the first import
).
The rules are quite simple: the same module is evaluated only once, in
other words, the module-level scope is executed just once. If the
module, once evaluated, is imported again, it’s second evaluation is
skipped and the resolved already exports are used.
Which is why code written above a React component only runs once, as confirmed in the docs:
Some logic should only run once when the application starts. You can
put it outside your components…this guarantees that such logic only
runs once after the browser loads the page.
My question is related to Next.js on Vercel:
Would import
s only run once when the site is built, and never again?
Or does it differ between static and dynamic routes (pages)? I.e. a page, if static, only runs an import upon build, whereas a dynamic route runs the import every time a browser visits the page?