0
0
NextJSframework~20 mins

Streaming long operations in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Streaming Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Next.js streaming component render first?

Consider this Next.js Server Component that streams a long operation result:

export default async function StreamExample() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(new TextEncoder().encode('Loading...\n'));
      setTimeout(() => {
        controller.enqueue(new TextEncoder().encode('Data chunk 1\n'));
        controller.close();
      }, 1000);
    }
  });

  const response = new Response(stream);
  const body = response.body;

  return (
    <pre>{body ? 'Streaming started' : 'No stream'}</pre>
  );
}

What will the user see immediately when this component renders?

NextJS
export default async function StreamExample() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(new TextEncoder().encode('Loading...\n'));
      setTimeout(() => {
        controller.enqueue(new TextEncoder().encode('Data chunk 1\n'));
        controller.close();
      }, 1000);
    }
  });

  const response = new Response(stream);
  const body = response.body;

  return (
    <pre>{body ? 'Streaming started' : 'No stream'}</pre>
  );
}
AThe text 'Loading...' appears immediately, then 'Data chunk 1' after 1 second
BThe text 'Streaming started' appears immediately, then updates after 1 second
CNothing appears until the stream closes after 1 second
DThe text 'No stream' appears because body is undefined
Attempts:
2 left
💡 Hint

Check what the variable body contains and how JSX renders it.

📝 Syntax
intermediate
2:00remaining
Which Next.js streaming code snippet correctly streams JSON chunks?

Which code snippet correctly streams JSON chunks from a Next.js Server Component?

Aconst stream = new ReadableStream({start(controller) {controller.enqueue(new TextEncoder().encode(JSON.stringify({part:1})));controller.close();}});
Bconst stream = new ReadableStream({start(controller) {controller.enqueue(Buffer.from(JSON.stringify({part:1})));controller.close();}});
Cconst stream = new ReadableStream({start(controller) {controller.enqueue(JSON.stringify({part:1}));controller.close();}});
Dconst stream = new ReadableStream({start(controller) {controller.enqueue(JSON.stringify({part:1}).buffer);controller.close();}});
Attempts:
2 left
💡 Hint

ReadableStream expects Uint8Array chunks, not strings or buffers.

🔧 Debug
advanced
2:00remaining
Why does this Next.js streaming component cause a runtime error?

Review this Next.js Server Component code:

export default async function StreamError() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue('Hello');
      controller.close();
    }
  });

  const response = new Response(stream);
  const text = await response.text();

  return <pre>{text}</pre>;
}

Why does this code cause a runtime error?

NextJS
export default async function StreamError() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue('Hello');
      controller.close();
    }
  });

  const response = new Response(stream);
  const text = await response.text();

  return <pre>{text}</pre>;
}
Acontroller.enqueue expects a Uint8Array, but a string was passed causing a TypeError
Bawait response.text() is invalid because Response has no text() method
CThe component cannot return JSX from an async function
DReadableStream cannot be used inside Next.js Server Components
Attempts:
2 left
💡 Hint

Check the type of argument passed to controller.enqueue.

state_output
advanced
2:00remaining
What is the output of this Next.js streaming component with delayed chunks?

Given this Next.js Server Component:

export default async function DelayedStream() {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue(new TextEncoder().encode('Start\n'));
      await new Promise(r => setTimeout(r, 500));
      controller.enqueue(new TextEncoder().encode('Middle\n'));
      await new Promise(r => setTimeout(r, 500));
      controller.enqueue(new TextEncoder().encode('End\n'));
      controller.close();
    }
  });

  const response = new Response(stream);
  const text = await response.text();

  return <pre>{text}</pre>;
}

What will the user see when this component renders?

NextJS
export default async function DelayedStream() {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue(new TextEncoder().encode('Start\n'));
      await new Promise(r => setTimeout(r, 500));
      controller.enqueue(new TextEncoder().encode('Middle\n'));
      await new Promise(r => setTimeout(r, 500));
      controller.enqueue(new TextEncoder().encode('End\n'));
      controller.close();
    }
  });

  const response = new Response(stream);
  const text = await response.text();

  return <pre>{text}</pre>;
}
AThe component throws an error because async start is not allowed
BThe text 'Start' appears immediately, then 'Middle' after 0.5s, then 'End' after 1s
CNothing appears until the stream closes, then 'Start\nMiddle\nEnd\n' appears
DThe text 'Start\nMiddle\nEnd\n' appears all at once after about 1 second
Attempts:
2 left
💡 Hint

Consider how await response.text() works with streams.

🧠 Conceptual
expert
3:00remaining
Which Next.js streaming approach enables progressive HTML rendering in the browser?

In Next.js 14+, which streaming approach allows the browser to progressively render HTML chunks as they arrive from the server?

AUsing client-side <code>useEffect</code> to fetch data and update state after full load
BReturning a plain string from a Server Component after awaiting all data
CUsing <code>fetch</code> with <code>ReadableStream</code> and returning a <code>Response</code> with <code>body</code> from a Server Component
DReturning a JSON object from a Server Component and parsing it on the client
Attempts:
2 left
💡 Hint

Think about how streaming HTML works in Next.js Server Components.