Don’t use Buffer.byteLength() as Deno can’t use it (#4324)

* Don’t use Buffer.byteLength() as Deno can’t use it

* Add changeset

* Add tests for Markdown & MDX with Deno

Co-authored-by: Matthew Phillips <matthew@skypack.dev>
This commit is contained in:
Patrick Smith 2022-08-17 00:08:40 +10:00 committed by GitHub
parent 166b3b8a54
commit 45fdbc4650
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 53 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Use TextEncoder instead of Buffer.byteLength() for Deno compatibility

View file

@ -49,10 +49,11 @@ export async function renderPage(
}
html += rest;
}
return new Response(html, {
const bytes = encoder.encode(html);
return new Response(bytes, {
headers: new Headers([
['Content-Type', 'text/html; charset=utf-8'],
['Content-Length', Buffer.byteLength(html, 'utf-8').toString()],
['Content-Length', bytes.byteLength.toString()],
]),
});
}

View file

@ -59,3 +59,31 @@ Deno.test({
});
},
});
Deno.test({
name: 'Works with Markdown',
async fn() {
await startApp(async () => {
const resp = await fetch('http://127.0.0.1:8085/markdown');
const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`);
const h1 = doc.querySelector('h1');
assertEquals(h1.innerText, 'Heading from Markdown');
});
},
});
Deno.test({
name: 'Works with MDX',
async fn() {
await startApp(async () => {
const resp = await fetch('http://127.0.0.1:8085/mdx');
const html = await resp.text();
const doc = new DOMParser().parseFromString(html, `text/html`);
const h1 = doc.querySelector('h1');
assertEquals(h1.innerText, 'Heading from MDX');
});
},
});

View file

@ -1,9 +1,10 @@
import { defineConfig } from 'astro/config';
import deno from '@astrojs/deno';
import react from '@astrojs/react';
import mdx from '@astrojs/mdx';
export default defineConfig({
adapter: deno(),
integrations: [react()],
integrations: [react(), mdx()],
output: 'server',
})

View file

@ -6,6 +6,7 @@
"astro": "workspace:*",
"@astrojs/deno": "workspace:*",
"@astrojs/react": "workspace:*",
"@astrojs/mdx": "workspace:*",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}

View file

@ -0,0 +1,6 @@
---
title: Title
description: Description
---
# Heading from Markdown

View file

@ -0,0 +1,6 @@
---
title: Title
description: Description
---
# Heading from MDX

View file

@ -2146,12 +2146,14 @@ importers:
packages/integrations/deno/test/fixtures/basics:
specifiers:
'@astrojs/deno': workspace:*
'@astrojs/mdx': workspace:*
'@astrojs/react': workspace:*
astro: workspace:*
react: ^18.1.0
react-dom: ^18.1.0
dependencies:
'@astrojs/deno': link:../../..
'@astrojs/mdx': link:../../../../mdx
'@astrojs/react': link:../../../../react
astro: link:../../../../../astro
react: 18.2.0