-
Notifications
You must be signed in to change notification settings - Fork 73
test: sync e2e with latest aws #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
181bc4e
sync opennextjs/opennextjs-aws#792
vicb 2cfa261
sync opennextjs/opennextjs-aws#793
vicb 8ba89a9
Sync opennextjs/opennextjs-aws#794
vicb 8ff0a2b
Sync opennextjs/opennextjs-aws#799
vicb 617d05a
add changeset
vicb 7139723
Sync opennextjs/opennextjs-aws#816
vicb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@opennextjs/cloudflare": patch | ||
--- | ||
|
||
test: sync e2e with aws |
34 changes: 34 additions & 0 deletions
34
examples/e2e/app-router/app/isr/dynamic-params-false/[id]/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams | ||
export const dynamicParams = false; // or true, to make it try SSR unknown paths | ||
|
||
const POSTS = Array.from({ length: 20 }, (_, i) => ({ | ||
id: String(i + 1), | ||
title: `Post ${i + 1}`, | ||
content: `This is post ${i + 1}`, | ||
})); | ||
|
||
async function fakeGetPostsFetch() { | ||
return POSTS.slice(0, 10); | ||
} | ||
|
||
async function fakeGetPostFetch(id: string) { | ||
return POSTS.find((post) => post.id === id); | ||
} | ||
|
||
export async function generateStaticParams() { | ||
const fakePosts = await fakeGetPostsFetch(); | ||
return fakePosts.map((post) => ({ | ||
id: post.id, | ||
})); | ||
} | ||
|
||
export default async function Page({ params }: { params: Promise<{ id: string }> }) { | ||
const { id } = await params; | ||
const post = await fakeGetPostFetch(id); | ||
return ( | ||
<main> | ||
<h1 data-testid="title">{post?.title}</h1> | ||
<p data-testid="content">{post?.content}</p> | ||
</main> | ||
); | ||
} |
45 changes: 45 additions & 0 deletions
45
examples/e2e/app-router/app/isr/dynamic-params-true/[id]/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { notFound } from "next/navigation"; | ||
|
||
// We'll prerender only the params from `generateStaticParams` at build time. | ||
// If a request comes in for a path that hasn't been generated, | ||
// Next.js will server-render the page on-demand. | ||
// https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams | ||
export const dynamicParams = true; // or false, to 404 on unknown paths | ||
|
||
const POSTS = Array.from({ length: 20 }, (_, i) => ({ | ||
id: String(i + 1), | ||
title: `Post ${i + 1}`, | ||
content: `This is post ${i + 1}`, | ||
})); | ||
|
||
async function fakeGetPostsFetch() { | ||
return POSTS.slice(0, 10); | ||
} | ||
|
||
async function fakeGetPostFetch(id: string) { | ||
return POSTS.find((post) => post.id === id); | ||
} | ||
|
||
export async function generateStaticParams() { | ||
const fakePosts = await fakeGetPostsFetch(); | ||
return fakePosts.map((post) => ({ | ||
id: post.id, | ||
})); | ||
} | ||
|
||
export default async function Page({ params }: { params: Promise<{ id: string }> }) { | ||
const { id } = await params; | ||
const post = await fakeGetPostFetch(id); | ||
if (Number(id) === 1337) { | ||
throw new Error("This is an error!"); | ||
} | ||
if (!post) { | ||
notFound(); | ||
} | ||
return ( | ||
<main> | ||
<h1 data-testid="title">{post.title}</h1> | ||
<p data-testid="content">{post.content}</p> | ||
</main> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { expect, test } from "@playwright/test"; | ||
|
||
const SADE_SMOOTH_OPERATOR_LYRIC = `Diamond life, lover boy | ||
He move in space with minimum waste and maximum joy | ||
City lights and business nights | ||
When you require streetcar desire for higher heights | ||
No place for beginners or sensitive hearts | ||
When sentiment is left to chance | ||
No place to be ending but somewhere to start | ||
No need to ask, he's a smooth operator | ||
Smooth operator, smooth operator | ||
Smooth operator`; | ||
|
||
test("streaming should work in api route", async ({ page }) => { | ||
await page.goto("/sse"); | ||
|
||
// wait for first line to be present | ||
await page.getByTestId("line").first().waitFor(); | ||
const initialLines = await page.getByTestId("line").count(); | ||
// fail if all lines appear at once | ||
// this is a safeguard to ensure that the response is streamed and not buffered all at once | ||
expect(initialLines).toBe(1); | ||
|
||
const seenLines: Array<{ line: string; time: number }> = []; | ||
const startTime = Date.now(); | ||
|
||
// we loop until we see all lines | ||
while (seenLines.length < SADE_SMOOTH_OPERATOR_LYRIC.split("\n").length) { | ||
const lines = await page.getByTestId("line").all(); | ||
if (lines.length > seenLines.length) { | ||
expect(lines.length).toBe(seenLines.length + 1); | ||
const newLine = lines[lines.length - 1]; | ||
seenLines.push({ | ||
line: await newLine.innerText(), | ||
time: Date.now() - startTime, | ||
}); | ||
} | ||
// wait for a bit before checking again | ||
await page.waitForTimeout(200); | ||
} | ||
|
||
expect(seenLines.map((n) => n.line)).toEqual(SADE_SMOOTH_OPERATOR_LYRIC.split("\n")); | ||
for (let i = 1; i < seenLines.length; i++) { | ||
expect(seenLines[i].time - seenLines[i - 1].time).toBeGreaterThan(500); | ||
} | ||
|
||
await expect(page.getByTestId("video")).toBeVisible(); | ||
}); |
42 changes: 42 additions & 0 deletions
42
examples/e2e/pages-router/src/pages/api/streaming/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
const SADE_SMOOTH_OPERATOR_LYRIC = `Diamond life, lover boy | ||
He move in space with minimum waste and maximum joy | ||
City lights and business nights | ||
When you require streetcar desire for higher heights | ||
No place for beginners or sensitive hearts | ||
When sentiment is left to chance | ||
No place to be ending but somewhere to start | ||
No need to ask, he's a smooth operator | ||
Smooth operator, smooth operator | ||
Smooth operator`; | ||
|
||
function sleep(ms: number) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== "GET") { | ||
return res.status(405).json({ message: "Method not allowed" }); | ||
} | ||
|
||
res.setHeader("Content-Type", "text/event-stream"); | ||
res.setHeader("Connection", "keep-alive"); | ||
res.setHeader("Cache-Control", "no-cache, no-transform"); | ||
res.setHeader("Transfer-Encoding", "chunked"); | ||
|
||
res.write(`data: ${JSON.stringify({ type: "start", model: "ai-lyric-model" })}\n\n`); | ||
await sleep(1000); | ||
|
||
const lines = SADE_SMOOTH_OPERATOR_LYRIC.split("\n"); | ||
for (const line of lines) { | ||
res.write(`data: ${JSON.stringify({ type: "content", body: line })}\n\n`); | ||
await sleep(1000); | ||
} | ||
|
||
res.write(`data: ${JSON.stringify({ type: "complete" })}\n\n`); | ||
|
||
res.end(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
|
||
type Event = { | ||
type: "start" | "content" | "complete"; | ||
model?: string; | ||
body?: string; | ||
}; | ||
|
||
export default function SSE() { | ||
const [events, setEvents] = useState<Event[]>([]); | ||
const [finished, setFinished] = useState(false); | ||
|
||
useEffect(() => { | ||
const e = new EventSource("/api/streaming"); | ||
|
||
e.onmessage = (msg) => { | ||
console.log(msg); | ||
try { | ||
const data = JSON.parse(msg.data) as Event; | ||
if (data.type === "complete") { | ||
e.close(); | ||
setFinished(true); | ||
} | ||
if (data.type === "content") { | ||
setEvents((prev) => prev.concat(data)); | ||
} | ||
} catch (err) { | ||
console.error(err, msg); | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
padding: "20px", | ||
marginBottom: "20px", | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "40px", | ||
}} | ||
> | ||
<h1 | ||
style={{ | ||
fontSize: "2rem", | ||
marginBottom: "20px", | ||
}} | ||
> | ||
Sade - Smooth Operator | ||
</h1> | ||
<div> | ||
{events.map((e, i) => ( | ||
<p data-testid="line" key={i}> | ||
{e.body} | ||
</p> | ||
))} | ||
</div> | ||
{finished && ( | ||
<iframe | ||
data-testid="video" | ||
width="560" | ||
height="315" | ||
src="https://www.youtube.com/embed/4TYv2PhG89A?si=e1fmpiXZZ1PBKPE5" | ||
title="YouTube video player" | ||
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" | ||
referrerPolicy="strict-origin-when-cross-origin" | ||
allowFullScreen | ||
></iframe> | ||
)} | ||
</div> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.