[{"id":"nextbeint-001","courseId":"nextjs-backend-intermediate","domain":"server-backend","topic":"server-only package","prompt":"lib/billing.ts reads process.env.STRIPE_KEY and queries the database. Months later someone imports one helper from it into a Client Component. How can the team make that mistake fail the build instead of shipping?","hint":"Look for a marker that turns a client-side import into a build failure.","source":"https://nextjs.org/docs/app/guides/data-security#preventing-client-side-execution-of-server-only-code","interaction":{"type":"single"},"options":[{"id":"a","text":"Prefix the variable with NEXT_PUBLIC_ so Next.js can track where it's used.","whyNot":"That does the opposite: NEXT_PUBLIC_ values are inlined into the browser bundle."},{"id":"b","text":"Add import 'server-only' at the top of lib/billing.ts.","why":"A module marked with server-only causes a build error if it's imported in the client environment."},{"id":"c","text":"Add 'use server' at the top so the file can only ever run on the server.","whyNot":"'use server' turns exports into Server Functions the client can call; it doesn't block client imports."},{"id":"d","text":"Nothing: non-public env vars are empty in the browser, so the import is harmless.","whyNot":"The module's code and database logic would still ship to the client, and nothing fails at build."}],"answer":"b","explanation":"import 'server-only' marks a module as server-only, so importing it from client code fails the build and keeps secrets and internal logic off the client.","concept":"import 'server-only' marks a module as server-only, so importing it from client code fails the build and keeps secrets and internal logic off the client.","rev":1,"kind":"scenario","takeaways":["Put import 'server-only' in modules that touch secrets or the database."]},{"id":"nextbeint-002","courseId":"nextjs-backend-intermediate","domain":"server-backend","topic":"Authorization and IDOR in actions","prompt":"A Server Action deletePost(postId) is called from a post's menu. An interviewer asks what must happen inside the action, or the Data Access Layer it calls, before the delete runs. Which TWO checks belong there?","hint":"One check is about who the caller is; the other is about whether this caller may touch this row.","source":"https://nextjs.org/docs/app/guides/data-security#authentication-and-authorization","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"Confirm there is a signed-in session and reject the call when there is none.","why":"Authentication has to be re-checked in the action because the action can be POSTed directly."},{"id":"b","text":"Rely on the proxy.ts matcher covering /posts, since every action call passes through it.","whyNot":"Proxy coverage depends on the matcher and the route; the docs say never to rely on proxy alone for actions."},{"id":"c","text":"Load the post and confirm its authorId matches the session user before deleting it.","why":"Checking ownership of the specific resource is authorization and prevents insecure direct object references (IDOR)."},{"id":"d","text":"Rely on the encrypted, non-deterministic action ID to keep outsiders from calling it.","whyNot":"Secure IDs reduce risk but the docs say you must still verify authentication and authorization in each action."}],"answer":"ac","explanation":"Each action needs authentication (is someone signed in) and authorization (may this user act on this resource). Missing the second leads to IDOR bugs.","concept":"Each action needs authentication (is someone signed in) and authorization (may this user act on this resource). Missing the second leads to IDOR bugs.","rev":1,"kind":"scenario","takeaways":["Authenticated is not authorized: check ownership of the exact row the action changes."]},{"id":"nextbeint-003","courseId":"nextjs-backend-intermediate","domain":"server-backend","topic":"Server Action return values","prompt":"An updateProfile Server Action checks the session, then returns db.user.update(...) so the form can show 'Saved'. A security review flags it, even though only the owner can call it. What's the issue?","hint":"Follow the returned record after the action finishes on the server.","source":"https://nextjs.org/docs/app/guides/data-security#controlling-return-values","interaction":{"type":"single"},"options":[{"id":"a","text":"Server Actions can't return values, so the form must re-fetch the profile after saving.","whyNot":"Server Action return values are serialized and sent to the client; returning is allowed."},{"id":"b","text":"Database writes belong in a Route Handler, because Server Actions can't write data.","whyNot":"Server Actions are built for mutations like this one."},{"id":"c","text":"It's sent to the client, so the full record can leak internal fields; return only what the UI needs.","why":"The guide warns that return values are serialized to the client and recommends a minimal result such as { success: true }."},{"id":"d","text":"Returning a promise from an action makes Next.js skip the session check it ran first.","whyNot":"The check ran before the update; the problem is what goes back, not whether auth ran."}],"answer":"c","explanation":"A Server Action's return value is serialized and sent to the client. Return only what the UI needs, never raw database records.","concept":"A Server Action's return value is serialized and sent to the client. Return only what the UI needs, never raw database records.","rev":1,"kind":"scenario","takeaways":["Whatever an action returns reaches the browser: return a small DTO, not a row."]},{"id":"nextbeint-004","courseId":"nextjs-backend-intermediate","domain":"server-backend","topic":"Mutations during rendering","prompt":"To log users out, a page checks searchParams.logout and calls (await cookies()).delete('session') while it renders. It doesn't work as hoped. What does the Data Security guide say?","hint":"Ask whether rendering a page should ever change state.","source":"https://nextjs.org/docs/app/guides/data-security#avoiding-side-effects-during-rendering","interaction":{"type":"single"},"options":[{"id":"a","text":"Next.js blocks setting cookies in render; do the logout in a Server Action instead.","why":"Mutations must never be a side effect of rendering; Next.js prevents setting cookies or revalidating in render and points to Server Actions."},{"id":"b","text":"Cookies can only be deleted in proxy.ts, so the logout must move there.","whyNot":"Server Actions and Route Handlers can delete cookies too; the guide points to Server Actions."},{"id":"c","text":"It works once the page uses 'use cache', which makes cookie writes stick.","whyNot":"Cached scopes can't even read cookies; caching doesn't make render-time writes safe."},{"id":"d","text":"It works if the page awaits searchParams before calling cookies().","whyNot":"The order of awaits isn't the issue; changing state during render is."}],"answer":"a","explanation":"Rendering must not mutate anything (logging out, writing data, revalidating). Next.js blocks setting cookies and revalidating in render; use Server Actions, which use POST.","concept":"Rendering must not mutate anything (logging out, writing data, revalidating). Next.js blocks setting cookies and revalidating in render; use Server Actions, which use POST.","rev":1,"kind":"scenario","takeaways":["Never mutate in render: loading a page should change nothing."]},{"id":"nextbeint-005","courseId":"nextjs-backend-intermediate","domain":"server-backend","topic":"instrumentation register()","prompt":"A team wants OpenTelemetry set up once per server before any request is handled. One developer puts the setup in the root layout, another in a Route Handler. Where does Next.js expect it?","hint":"Next.js has a file convention for code that must run when a server instance boots.","source":"https://nextjs.org/docs/app/guides/instrumentation","interaction":{"type":"single"},"options":[{"id":"a","text":"In app/layout.tsx, because the root layout renders before every page.","whyNot":"Layouts render per request and don't cover Route Handlers, so they're no place for one-time setup."},{"id":"b","text":"In proxy.ts, because it runs before every request reaches a route.","whyNot":"Proxy runs on each matching request, not once at startup."},{"id":"c","text":"In next.config.ts, inside a function that runs when the server starts.","whyNot":"next.config configures the build and server; startup code for monitoring goes in instrumentation."},{"id":"d","text":"In a register function exported from instrumentation.ts at the project root.","why":"register is called once when a new server instance starts and must finish before the server handles requests."}],"answer":"d","explanation":"instrumentation.ts in the project root (or src) exports register, which Next.js calls once per new server instance before it serves requests.","concept":"instrumentation.ts in the project root (or src) exports register, which Next.js calls once per new server instance before it serves requests.","rev":1,"kind":"scenario","takeaways":["One-time server setup such as tracing goes in instrumentation.ts register()."]}]