Skip to content

Commit

Permalink
Don’t use Buffer.byteLength() as Deno can’t use it (#4324)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
RoyalIcing and matthewp committed Aug 16, 2022
1 parent 166b3b8 commit 45fdbc4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-schools-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Use TextEncoder instead of Buffer.byteLength() for Deno compatibility
5 changes: 3 additions & 2 deletions packages/astro/src/runtime/server/render/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
]),
});
}
Expand Down
28 changes: 28 additions & 0 deletions packages/integrations/deno/test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
},
});
Original file line number Diff line number Diff line change
@@ -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',
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"astro": "workspace:*",
"@astrojs/deno": "workspace:*",
"@astrojs/react": "workspace:*",
"@astrojs/mdx": "workspace:*",
"react": "^18.1.0",
"react-dom": "^18.1.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Title
description: Description
---

# Heading from Markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: Title
description: Description
---

# Heading from MDX
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45fdbc4

Please sign in to comment.