Skip to content

Commit 1048aca

Browse files
authored
Lazily initialize ResponseWithEncoding (#8253)
* Lazily initialize ResponseWithEncoding * Fix things * Add a TODO about removing the workaround * Remove unused lint ignore * Use canplaythrough instead * Use an inline script * Check the readystate first * Download the video locally * Capture consoles * More debugging * Use autoplay instead of a ready event
1 parent 46c4c0e commit 1048aca

5 files changed

Lines changed: 36 additions & 26 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Fix, lazily initialize ResponseWithEncoding
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<video controls="" autoplay="" name="media" transition:persist transition:name="video">
1+
<video controls="" autoplay="" name="media" transition:persist transition:name="video" autoplay>
22
<source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4">
33
</video>

packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,4 @@ import Video from '../components/Video.astro';
66
<p id="video-one">Page 1</p>
77
<a id="click-two" href="/video-two">go to 2</a>
88
<Video />
9-
<script>
10-
const vid = document.querySelector('video');
11-
vid.addEventListener('canplay', () => {
12-
// Jump to the 1 minute mark
13-
vid.currentTime = 60;
14-
vid.dataset.ready = '';
15-
}, { once: true });
16-
</script>
179
</Layout>

packages/astro/e2e/view-transitions.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ test.describe('View Transitions', () => {
302302

303303
// Go to page 1
304304
await page.goto(astro.resolveUrl('/video-one'));
305-
const vid = page.locator('video[data-ready]');
305+
const vid = page.locator('video');
306306
await expect(vid).toBeVisible();
307307
const firstTime = await page.evaluate(getTime);
308308

packages/astro/src/core/endpoint/index.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export function createAPIContext({
3939
props,
4040
adapterName,
4141
}: CreateAPIContext): APIContext {
42+
initResponseWithEncoding();
4243
const context = {
4344
cookies: new AstroCookies(request),
4445
request,
@@ -91,27 +92,39 @@ export function createAPIContext({
9192

9293
type ResponseParameters = ConstructorParameters<typeof Response>;
9394

94-
export class ResponseWithEncoding extends Response {
95-
constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding) {
96-
// If a body string is given, try to encode it to preserve the behaviour as simple objects.
97-
// We don't do the full handling as simple objects so users can control how headers are set instead.
98-
if (typeof body === 'string') {
99-
// In NodeJS, we can use Buffer.from which supports all BufferEncoding
100-
if (typeof Buffer !== 'undefined' && Buffer.from) {
101-
body = Buffer.from(body, encoding);
95+
export let ResponseWithEncoding: ReturnType<typeof initResponseWithEncoding>;
96+
// TODO Remove this after StackBlitz supports Node 18.
97+
let initResponseWithEncoding = () => {
98+
class LocalResponseWithEncoding extends Response {
99+
constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding) {
100+
// If a body string is given, try to encode it to preserve the behaviour as simple objects.
101+
// We don't do the full handling as simple objects so users can control how headers are set instead.
102+
if (typeof body === 'string') {
103+
// In NodeJS, we can use Buffer.from which supports all BufferEncoding
104+
if (typeof Buffer !== 'undefined' && Buffer.from) {
105+
body = Buffer.from(body, encoding);
106+
}
107+
// In non-NodeJS, use the web-standard TextEncoder for utf-8 strings
108+
else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') {
109+
body = encoder.encode(body);
110+
}
102111
}
103-
// In non-NodeJS, use the web-standard TextEncoder for utf-8 strings
104-
else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') {
105-
body = encoder.encode(body);
112+
113+
super(body, init);
114+
115+
if (encoding) {
116+
this.headers.set('X-Astro-Encoding', encoding);
106117
}
107118
}
119+
}
108120

109-
super(body, init);
121+
// Set the module scoped variable.
122+
ResponseWithEncoding = LocalResponseWithEncoding;
110123

111-
if (encoding) {
112-
this.headers.set('X-Astro-Encoding', encoding);
113-
}
114-
}
124+
// Turn this into a noop.
125+
initResponseWithEncoding = (() => {}) as any;
126+
127+
return LocalResponseWithEncoding;
115128
}
116129

117130
export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>(

0 commit comments

Comments
 (0)