惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Stack Overflow Blog
Stack Overflow Blog
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
IT之家
IT之家
V
V2EX
C
Check Point Blog
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI
B
Blog
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
雷峰网
雷峰网

Deno

Deno 2.8 | Deno Claw Patrol: an open-source security firewall for agents | Deno Fresh 2.3: Zero JS by default, View Transitions, and Temporal support | Deno Deno 2.7: Temporal API, Windows ARM, and npm overrides | Deno Build a dinosaur runner game with Deno, pt. 6 | Deno Build a dinosaur runner game with Deno, pt. 5 | Deno Deno Deploy is Generally Available | Deno Introducing Deno Sandbox | Deno Build a dinosaur runner game with Deno, pt. 4 | Deno Build a dinosaur runner game with Deno, pt. 3 | Deno Build a dinosaur runner game with Deno, pt. 2 | Deno React / Next.js Denial-of-Service Vulnerability: Deno Deploy users protected | Deno Deno 2.6: dx is the new npx | Deno Build a dinosaur runner game with Deno, pt. 1 | Deno React Server Functions / Next.js Vulnerability: Deno Deploy users protected | Deno My highlights from the new Deno Deploy | Deno Deno's Other Open Source Projects | Deno How Deno protects against npm exploits | Deno Help Us Raise $200k to Free JavaScript from Oracle | Deno Deno 2.5: Permissions in the config file | Deno Fresh 2.0 Graduates to Beta, Adds Vite Support | Deno Deno 2.4: deno bundle is back | Deno JavaScript™ Trademark Update | Deno What's coming to JavaScript | Deno A brief history of JavaScript | Deno Reports of Deno's Demise Have Been Greatly Exaggerated | Deno An Update on Fresh | Deno How Plaid migrated 100 services to a new database platform 5x faster with Deno | Deno Deno 2.3: Improved deno compile, local npm packages, and more | Deno Add JSR packages with pnpm and Yarn | Deno
Build an Astro site with Deno | Deno
Andy Jiang · 2024-10-29 · via Deno

Astro is a modern web framework focused on content-centric websites, which leverages islands architecture and sends zero JavaScript to the client by default. And with the recent release of Deno 2, now backwards compatible with Node and npm, the experience of using Astro and Deno has improved.

Deno 2 is backwards compatible and works with many JavaScript frameworks

Deno 2 is backwards compatible with Node and npm, allowing you to use your preferred JavaScript framework. Watch the full Deno 2 announcement video.

We’ll go over how to build a simple Astro project using Deno:

  • Scaffold an Astro project
  • Update index page
  • Add a dynamic SSR page
  • What’s next?

Feel free to skip directly to the source code or follow along below!

🚨️ Try Deno 2 today. 🚨️

Deno offers backwards compatibilty with Node/npm, built-in package management, all-in-one zero-config toolchain, native TypeScript support, and more.

Scaffold an Astro project

Astro provides a CLI tool to quickly scaffold a new Astro project. In your terminal, run the command deno -A npm:create-astro@latest to create a new Astro project with Deno. For this tutorial, we’ll select the “Empty” template so we can start from scratch, and skip installing dependencies so we can install them with Deno later:

deno -A npm:create-astro@latest

 astro   Launch sequence initiated.

   dir   Where should we create your new project?
         ./dino-app

  tmpl   How would you like to start your new project?
         Empty

    ts   Do you plan to write TypeScript?
         Yes

   use   How strict should TypeScript be?
         Strict

  deps   Install dependencies?
         NoNo problem!
         Remember to install dependencies after setup.

   git   Initialize a new git repository?
         YesProject initialized!Template copied
         ■ TypeScript customized
         ■ Git initialized

  next   Liftoff confirmed. Explore your project!

 Enter your project directory using cd ./dino-app
 Run npm run dev to start the dev server. CTRL+C to stop.
 Add frameworks like react or tailwind using astro add.

 Stuck? Join us at https://astro.build/chat

╭─────╮  Houston:
│ ◠ ◡ ◠  Good luck out there, astronaut! 🚀
╰──🍫─╯

As of Deno 2, Deno can also install packages with the new deno install command. So let’s run deno install with the flag --allow-scripts to execute any npm lifecycle scripts:

deno install --allow-scripts

To see what commands we have, let’s run deno task:

deno task
Available tasks:
- dev (package.json)
    astro dev
- start (package.json)
    astro dev
- build (package.json)
    astro check && astro build
- preview (package.json)
    astro preview
- astro (package.json)
    astro

We can start the Astro server with deno task dev:

Getting the Astro app to work

Update index page to list all dinosaurs

Our app will display facts about a variety of dinosaurs. The first page to create will be the index page that lists links to all dinosaurs in our “database”.

First, let’s create the data that will be used in the app. In this example, we’ll hardcode the data in a json file, but you can use any data storage in practice. We’ll create a data folder in the root of the project, then a dinosaurs.json file with this text in it.

⚠️️ In this tutorial we hard code the data. But you can connect to a variety of databases and even use ORMs like Prisma with Deno.

Once we have the data, let’s create an index page that lists all of the dinosaurs. In the ./src/pages/index.astro page, let’s write the following:

---
import data from "../../data/dinosaurs.json";
---

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
        <meta name="viewport" content="width=device-width" />
        <meta name="generator" content={Astro.generator} />
        <title>Dinosaurs</title>
    </head>
    <body>
        <h1>Dinosaurs</h1>
        <ul>
            {data.map((dinosaur) => (
                <li>
                    <a href={`/${dinosaur.name.toLowerCase()}`}>{ dinosaur.name }</a>
                </li>
            ))}
        </ul>
    </body>
</html>

Let’s start the server with deno task dev and point our browser to localhost:4321:

Index page that lists all dinosaurs

Awesome! But when you click on a dinosaur, it 404’s. Let’s fix that.

Add a dynamic SSR page

Our app will display facts about a variety of dinosaurs. In order to do that, we’ll create a dynamic server-side rendered (”SSR”), which offers better performance for end users while improving your pages SEO.

Next, let’s create a new file under /src/pages/ called [dinosaur].astro. At the top of the file, we’ll add some logic to pull data from our hardcoded data source and filter that against the dinosaur parameter set from the URL path. At the bottom, we’ll render the data. Your file should look like this:

---
import data from "../../data/dinosaurs.json";
const { dinosaur } = Astro.params;
const dinosaurObj = data.find((item) => item.name.toLowerCase() === dinosaur);
if (!dinosaurObj) return Astro.redirect("/404");
const { name, description } = dinosaurObj;
---

<h1>{ name }</h1>

<p>
    { description }
</p>

⚠️️ The Deno language server does not currently support .astro files, so you may experience false red squigglies. We’re working on improving this experience.

Let’s run it with deno task dev, and point our browser to localhost:4321/abrictosaurus:

Rendering a dynamic page for abrictosaurus

It works!

What’s next

We hope this tutorial gives you a good idea of how to get started building with Astro and Deno. You can learn more about Astro and their progressive approach to building websites. If you’re interested in swapping out our hardcoded data store, here are some resources on connecting to databases with Deno, including Planetscale, Redis, and more. Or you can learn how to deploy your Astro project to Deno Deploy, or follow these guides on how to self-host Deno to AWS, Digital Ocean, and Google Cloud Run.

🚨️ Introducing Learn Deno. 🚨️

Check out our new tutorial series, Learn Deno, to learn how to use Deno’s all-in-one toolchain, web platform APIs, and Deno-namespace APIs, and more, in short bite-sized videos.