0
0
NextJSframework~10 mins

Streaming long operations in NextJS - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a streaming response in a Next.js server component.

NextJS
export async function GET() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(new TextEncoder().encode('Hello, '));
      controller.enqueue(new TextEncoder().encode('world!'));
      controller.close();
    }
  });
  return new Response(stream, { headers: { 'Content-Type': [1] } });
}
Drag options to blanks, or click blank then click option'
A'text/plain'
B'text/html'
C'application/json'
D'application/octet-stream'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'application/json' when the content is plain text.
Forgetting to set the Content-Type header.
2fill in blank
medium

Complete the code to create a readable stream that sends chunks with a delay.

NextJS
export async function GET() {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue(new TextEncoder().encode('Loading'));
      await new Promise(resolve => setTimeout(resolve, [1]));
      controller.enqueue(new TextEncoder().encode('...done'));
      controller.close();
    }
  });
  return new Response(stream, { headers: { 'Content-Type': 'text/plain' } });
}
Drag options to blanks, or click blank then click option'
Anull
B'1000'
C1000
Dundefined
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a number to setTimeout.
Using null or undefined which causes errors.
3fill in blank
hard

Fix the error in the streaming function by completing the blank.

NextJS
export async function GET() {
  const encoder = new TextEncoder();
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(encoder.encode('Start'));
      controller.enqueue(encoder.encode('Middle'));
      controller.[1]();
    }
  });
  return new Response(stream, { headers: { 'Content-Type': 'text/plain' } });
}
Drag options to blanks, or click blank then click option'
Aend
Bclose
Cfinish
Dstop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'end' or 'finish' which are not valid methods.
Forgetting to close the stream causing it to hang.
4fill in blank
hard

Fill both blanks to create a streaming response that sends JSON chunks.

NextJS
export async function GET() {
  const stream = new ReadableStream({
    start(controller) {
      controller.enqueue(new TextEncoder().encode(JSON.[1]({ message: 'Hello' })));
      controller.enqueue(new TextEncoder().encode(JSON.[2]({ message: 'World' })));
      controller.close();
    }
  });
  return new Response(stream, { headers: { 'Content-Type': 'application/json' } });
}
Drag options to blanks, or click blank then click option'
Astringify
Bparse
CtoString
Dformat
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSON.parse which causes errors.
Using toString which does not produce JSON.
5fill in blank
hard

Fill all three blanks to create a streaming response that sends multiple delayed chunks.

NextJS
export async function GET() {
  const stream = new ReadableStream({
    async start(controller) {
      controller.enqueue(new TextEncoder().encode('Step 1'));
      await new Promise(resolve => setTimeout(resolve, [1]));
      controller.enqueue(new TextEncoder().encode('Step 2'));
      await new Promise(resolve => setTimeout(resolve, [2]));
      controller.enqueue(new TextEncoder().encode('Step 3'));
      controller.[3]();
    }
  });
  return new Response(stream, { headers: { 'Content-Type': 'text/plain' } });
}
Drag options to blanks, or click blank then click option'
A500
B1000
Cclose
Dfinish
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'finish' instead of 'close' to end the stream.
Using strings instead of numbers for delays.