Question presented to candidate: "We need Google Tag Manager, a chat widget and a consent script on the site. How do you add third-party scripts in Next.js without hurting performance, and which strategy would each get?"
What a strong answer should cover:
- The
Scriptcomponent from next/script loads external or inline scripts once per document, deduplicated, with a loadingstrategy. beforeInteractive: before any Next.js code and before hydration; only allowed in the root layout; for scripts that must run first (consent, bot checks).afterInteractive(default): after some hydration; for analytics and tag managers.lazyOnload: during browser idle time after all resources load; for chat widgets and social embeds.worker(experimental) moves a script into a web worker with Partytown;onLoad,onReadyandonErrorcallbacks need a Client Component.
Clarifying questions expected:
- "Does this script need to run before the page becomes interactive?" β only then beforeInteractive.
- "Is it needed on every page or just some?" β place the Script in the layout or page accordingly.
Code / implementation expected: Yes β one script per strategy.