diff options
5 files changed, 36 insertions, 26 deletions
diff --git a/.changeset/grumpy-years-remember.md b/.changeset/grumpy-years-remember.md new file mode 100644 index 000000000..1e1906cd9 --- /dev/null +++ b/.changeset/grumpy-years-remember.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix, lazily initialize ResponseWithEncoding diff --git a/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro b/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro index 7235266bc..0a3a22913 100644 --- a/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro +++ b/packages/astro/e2e/fixtures/view-transitions/src/components/Video.astro @@ -1,3 +1,3 @@ -<video controls="" autoplay="" name="media" transition:persist transition:name="video"> +<video controls="" autoplay="" name="media" transition:persist transition:name="video" autoplay> <source src="https://ia804502.us.archive.org/33/items/GoldenGa1939_3/GoldenGa1939_3_512kb.mp4" type="video/mp4"> </video> diff --git a/packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro b/packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro index 76f221c63..a78c3c43b 100644 --- a/packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro +++ b/packages/astro/e2e/fixtures/view-transitions/src/pages/video-one.astro @@ -6,12 +6,4 @@ import Video from '../components/Video.astro'; <p id="video-one">Page 1</p> <a id="click-two" href="/video-two">go to 2</a> <Video /> - <script> - const vid = document.querySelector('video'); - vid.addEventListener('canplay', () => { - // Jump to the 1 minute mark - vid.currentTime = 60; - vid.dataset.ready = ''; - }, { once: true }); - </script> </Layout> diff --git a/packages/astro/e2e/view-transitions.test.js b/packages/astro/e2e/view-transitions.test.js index fc69ac640..12fc63e48 100644 --- a/packages/astro/e2e/view-transitions.test.js +++ b/packages/astro/e2e/view-transitions.test.js @@ -302,7 +302,7 @@ test.describe('View Transitions', () => { // Go to page 1 await page.goto(astro.resolveUrl('/video-one')); - const vid = page.locator('video[data-ready]'); + const vid = page.locator('video'); await expect(vid).toBeVisible(); const firstTime = await page.evaluate(getTime); diff --git a/packages/astro/src/core/endpoint/index.ts b/packages/astro/src/core/endpoint/index.ts index a6e062727..b8c7000fb 100644 --- a/packages/astro/src/core/endpoint/index.ts +++ b/packages/astro/src/core/endpoint/index.ts @@ -39,6 +39,7 @@ export function createAPIContext({ props, adapterName, }: CreateAPIContext): APIContext { + initResponseWithEncoding(); const context = { cookies: new AstroCookies(request), request, @@ -91,27 +92,39 @@ export function createAPIContext({ type ResponseParameters = ConstructorParameters<typeof Response>; -export class ResponseWithEncoding extends Response { - constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding) { - // If a body string is given, try to encode it to preserve the behaviour as simple objects. - // We don't do the full handling as simple objects so users can control how headers are set instead. - if (typeof body === 'string') { - // In NodeJS, we can use Buffer.from which supports all BufferEncoding - if (typeof Buffer !== 'undefined' && Buffer.from) { - body = Buffer.from(body, encoding); +export let ResponseWithEncoding: ReturnType<typeof initResponseWithEncoding>; +// TODO Remove this after StackBlitz supports Node 18. +let initResponseWithEncoding = () => { + class LocalResponseWithEncoding extends Response { + constructor(body: ResponseParameters[0], init: ResponseParameters[1], encoding?: BufferEncoding) { + // If a body string is given, try to encode it to preserve the behaviour as simple objects. + // We don't do the full handling as simple objects so users can control how headers are set instead. + if (typeof body === 'string') { + // In NodeJS, we can use Buffer.from which supports all BufferEncoding + if (typeof Buffer !== 'undefined' && Buffer.from) { + body = Buffer.from(body, encoding); + } + // In non-NodeJS, use the web-standard TextEncoder for utf-8 strings + else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') { + body = encoder.encode(body); + } } - // In non-NodeJS, use the web-standard TextEncoder for utf-8 strings - else if (encoding == null || encoding === 'utf8' || encoding === 'utf-8') { - body = encoder.encode(body); + + super(body, init); + + if (encoding) { + this.headers.set('X-Astro-Encoding', encoding); } } + } - super(body, init); + // Set the module scoped variable. + ResponseWithEncoding = LocalResponseWithEncoding; - if (encoding) { - this.headers.set('X-Astro-Encoding', encoding); - } - } + // Turn this into a noop. + initResponseWithEncoding = (() => {}) as any; + + return LocalResponseWithEncoding; } export async function callEndpoint<MiddlewareResult = Response | EndpointOutput>( |