get new example working during build

This commit is contained in:
Fred K. Schott 2022-02-25 08:28:38 -08:00
parent 00dda8e056
commit 5370bba0b0
4 changed files with 21 additions and 16 deletions

View file

@ -22,7 +22,7 @@ Inside of your Astro project, you'll see the following folders and files:
├── src/
│ └── pages/
│ └── index.astro
│ └── company.json.ts
│ └── about.json.ts
└── package.json
```

View file

@ -0,0 +1,11 @@
// Returns the file body for this non-HTML file.
// The content type is based off of the extension in the filename,
// in this case: about.json.
export async function get() {
return {
body: JSON.stringify({
name: 'Astro',
url: 'https://astro.build/',
}),
};
}

View file

@ -1,8 +0,0 @@
export async function get() {
return {
body: JSON.stringify({
name: 'Astro',
url: 'https://astro.build/',
}),
};
}

View file

@ -1,8 +1,4 @@
---
const url = `${Astro.request.canonicalURL.origin}/company.json`;
const response = await fetch(url);
const data = await response.json();
---
<html lang="en">
<head>
<meta charset="utf-8" />
@ -10,7 +6,13 @@ const data = await response.json();
<title>Astro</title>
</head>
<body>
<h1>Astro</h1>
<div>{JSON.stringify(data)}</div>
<h1 id="result">Loading...</h1>
<script type="module">
// Non-HTML files will be included in your final build, so you
// can fetch them directly in the browser.
const response = await fetch(`/about.json`);
const data = await response.json();
document.getElementById('result').innerHTML = `Load complete!<br/>Built with: <a href="${data.url}">${data.name}!</a>`;
</script>
</body>
</html>