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

推荐订阅源

Cloudbric
Cloudbric
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
AWS News Blog
AWS News Blog
P
Privacy & Cybersecurity Law Blog
H
Help Net Security
云风的 BLOG
云风的 BLOG
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
A
Arctic Wolf
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
P
Privacy International News Feed
Blog — PlanetScale
Blog — PlanetScale
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
The Register - Security
The Register - Security
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cisco Blogs
PCI Perspectives
PCI Perspectives
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
A
About on SuperTechFans
W
WeLiveSecurity
GbyAI
GbyAI
V
Vulnerabilities – Threatpost
The GitHub Blog
The GitHub Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Check Point Blog
Y
Y Combinator Blog
月光博客
月光博客
Scott Helme
Scott Helme
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
U
Unit 42
G
Google Developers Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google Online Security Blog
Google Online Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cisco Talos Blog
Cisco Talos Blog
博客园 - 三生石上(FineUI控件)
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 司徒正美

Bryan Robinson's Blog

Does our technology still work for us? Product pricing, dev bad habits, and the role of the pit of success Astro Server Island for latest Bluesky post (heavily cached!) Type-safe environment variables in Astro 5.0 New Website, but really is it? Netlify Durable Cache: Caching for a third-party world Integrating Astro.js Starlight Documentation into a Next.js Project Using Proxies Jamstack is meaningless 😱 Book Release: Eleventy by Example – Learn 11ty with 5 in-depth projects 11ty Second 11ty: Creating Template Filters 11ty Second 11ty: Global Data files (JS and JSON) 11ty second 11ty: The Render Plugin Part 1 Help needed: Netlify Frontend environment variables with Astro.js Quick experiment with the Slinkity 11ty plugin Creating a dynamic color converter with 11ty Serverless Using 11ty JavaScript Data files to mix Markdown and CMS content into one collection How to show your template code in 11ty blog posts New City, New Job, New Content Using Nunjucks Climbing the 11ty Performance leaderboard with Cloudinary, critical CSS and more Three JAMstack movements to watch in 2020 Create a Codepen promo watermark with no additional HTML, CSS or JS 3 underused CSS features to learn for 2020 Use CSS Subgrid to layout full-width content stripes in an article template Adapt client-side JavaScript for use in 11ty (Eleventy) data files CSS Gap creates a bright future for margins in Flex as well as Grid Create your first CSS Custom Properties (Variables) Use CSS Grid to create a self-centering full-width element Creating an 11ty Plugin - SVG Embed Tool Now offering design and code reviews at PeerReviews.dev Routing contact-form emails to different addresses with Netlify, Zapier and SendGrid Create an Eleventy (11ty) theme based on a free HTML template Client work and the JAMstack Grid vs. Flex: A Tale of a "Simple" Promo Space Using Eleventy The Tech Barrier to Entry What Can We Learn from CERN Let Practical CSS Grid - Launching My First Course Build Trust on the Web incorporating User Worries with your User Stories 2019 The Year of Markup-First Development Refactoring CSS into a Sass mixin Starting a new journey with Code Contemporary Dynamic Static Sites with Netlify and iOS Shortcuts Top 3 uses for the ::before and ::after CSS pseudo elements How To: Use CSS Grid to Mix and Match Design Patterns Use CSS ::before and ::after for simple, spicy image overlays Modern CSS: Four Things Every Developer and Designer Should Know About CSS 3 Strategies for Getting Started with CSS Grid CSS Tip: Use rotate() and skew() together to introduce some clean punk rock to your CSS The 5 Stages of Grid Love How To: A CSS-Only Mobile Off Canvas Navigation How To: Use CSS Grid Layout to Make a Simple, Fluid Card Grid Make a More Flexible Cover Screen with CSS Grid Can CSS Grid open up interesting CMS Layout options? Firefox 52 to Introduce New Box-Alignment Values Falling Forward — Rethinking Progressive Enhancement, Graceful Degradation and Developer Morality Start Exploring the Magic of CSS Grid Layout I Converted My Blog to CSS Grid Layout and Regret Nothing Feature Queries are on the Rise CSS Shapes — Let the Text Flow Around You Flexbox -- Let Memorializing Prince and Print vs. The Web I went to Italy and noticed UX fails How to Get Designers to Contribute in Open Source The True Gift of Your Former Code
Introducing the Hygraph Astro Content Loader
2024-09-19 · via Bryan Robinson's Blog

The team at Astro just released a big change to how they handle content in their Content Layer in Astro v15. Now, instead of only having an internal content loader, they’ve opened the door for there to be custom content loaders.

To celebrate their launch today, we’ve got our own announcement to go with it: the Hygraph Content Loader for Astro.

The Hygraph Content Loader takes advantage of the new Content Layer API and provides a consistent data layer for your Hygraph Data and Astro Data. Instead of making requests per page or layout, you can create one configuration element for each type of data and have it ready at build time.

Implementation

To start, we need to install the loader in our Astro project.

npm install @hygraph/astro-content-loader

In your src/content/config.ts file, you can import the package and use Astro’s defineCollection method to define a new collection with a specific set of Hygraph data and even validate with Zod the types and amount of data coming back from Hygraph for your project.

import { defineCollection, z } from 'astro:content';
import { HygraphLoader } from '@hygraph/hygraph-astro-loader';

const withLoader = defineCollection({
  loader: HygraphLoader({
    endpoint: import.meta.env.ASTRO_HYGRAPH_ENDPOINT, 
    fields: ['id', 'title', 'slug', {body: ['text']}],
    operation: 'pages'
  }),
  schema: z.object({
      id: z.string(),
      title: z.string().min(1, { message: 'Title must be at least 1 character long' }),
      slug: z.string(),
      body: z.object({
          text: z.string()
  })
})
})

Once you've defined your collection with the Hygraph loader, you can use it in your Astro components just like any other content collection. This seamless integration allows for a smooth development experience and consistent data handling across your project.

Here's an example of how you might use the Hygraph content in an Astro page to get all pages from our collection:

---
import { getCollection } from 'astro:content';

const pages = await getCollection('pages');
---

<ul>
  {pages.map((page) => (
    <li>
      <a href={`/pages/${page.slug}`}>{page.data.title}</a>
    </li>
  ))}
</ul>

Or we can loop through them to create the pages:

---
import Main from "../../layouts/main.astro";
import { getCollection, render } from 'astro:content';

export async function getStaticPaths() {
  const pageEntries = await getCollection('page');

  return pageEntries.map(page => ({
    params: { slug: page.data.slug }, props: { page: page  },
  }));
}

const { page } = Astro.props
---

<!--  Renders the homepage with a title and a page fetched from the Hygraph -->
<Main title={page.data.title}>
  <div class="m-12">
    <h1 class="text-5xl font-bold mb-4">{page.data.title}</h1>
    <p class="text-lg mb-8">{page.data.body.text}</p>  
    <p>
      <a href="/" class="underline">Back to homepage</a>
    </p>
  </div>
</Main>

This approach offers several advantages:

  • Type Safety: With Zod schema validation, you ensure that the data coming from Hygraph matches your expectations, reducing runtime errors.
  • Performance: By default, Astro encourages static page generation. This is great for your site’s overall performance, but Astro also provides server-rendered functionality, as well. Pages built using the Content Layer API are also more performant than using standard fetch methods for each SSR page.
  • Developer Experience: The consistent API across different content sources (local, Hygraph, and other third parties) simplifies the development process and makes your code more maintainable. By configuring, we also have less boilerplate per page required. This increases the efficiency of developers creating new pages from this data.

As the Hygraph Content Loader matures, we anticipate even more features that will enhance your development workflow. Stay tuned for updates on incremental builds and improved caching mechanisms, which will further optimize your build process and site performance.

We're excited to see how the Astro community leverages this new integration with Hygraph. If you have any feedback or feature requests, please don't hesitate to reach out to our team or contribute to the project on GitHub.