npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flyo/nitro-astro

v2.0.11

Published

Connecting Flyo Headless Content Hub into your Astro project.

Downloads

516

Readme

Flyo Nitro Astro Framework Integration

The Flyo Nitro Astro Framework Integration provides a comprehensive solution for implementing the Flyo Nitro CMS within the Astro (astro.build) environment. This guide details the integration process, emphasizing the use of Nitro configurations within the Astro layout framework. Key highlights include:

  • Nitro Configuration in Astro: This section explores methods for incorporating Nitro configurations into the Astro layout, offering step-by-step instructions for seamless integration that leverages the strengths of both systems.
  • Page Resolution and Block Integration: Learn to manage and resolve pages within the Astro framework, including integrating Nitro's dynamic blocks. This part provides insights into creating responsive and interactive web pages using Nitro block technology.
  • Fetching Entity Details: Focus on techniques for retrieving and displaying detailed information about entities within Astro. This segment covers data fetching, handling, and presentation methods.
  • Image Service Integration: Understand the integration of Flyo Storage's image service, as detailed in Flyo Storage Documentation. This section delves into working with images in Astro, including uploading, retrieving, and displaying images from Flyo Storage.
  • Meta Information Extraction: The guide concludes with extracting and utilizing meta information, discussing the importance of meta tags for SEO and user engagement within the Astro framework.

This guide targets developers and web designers aiming to combine Flyo Nitro CMS and Astro framework capabilities to create dynamic, efficient, and feature-rich websites.

Installation

To install the @flyo/nitro-astro package, execute the following command:

npm install @flyo/nitro-astro
# yarn add @flyo/nitro-astro

Then, revise and adjust the configuration in your astro.config.mjs:

import flyoNitroIntegration from "@flyo/nitro-astro";

export default defineConfig({
  site: "https://myflyowebsite.com", // required to make the sitemap.xml work
  integrations: [
    flyoNitroIntegration({
      accessToken: "ADD_YOUR_TOKEN_HERE", // Switch between dev and prod tokens depending on the environment
      liveEdit: true, // Enable on dev and preview systems for application reloading in the Flyo preview frame upon changes
      components: {
        // Define where the Flyo components are located. The suffix .astro is not required. The object key is the value from Flyo, while the object value is the component in the Astro components folder
        // [!] Adding new elements requires restarting the development process
        FlyoElementName: "AstroElementName",
        AnotherFlyoElement: "subfolder/AnotherFlyoElement",
      },
    }),
  ],
  output: "server",
});

[!WARNING] The nitro astro integration requires an SSR setup which is done by using output: 'server'.

Pages

Add a [...slug].astro file in the pages directory with the following example content as a catch-all CMS handler:

---
import Layout from "../layouts/Layout.astro";
import { usePagesApi, useConfig } from "@flyo/nitro-astro";
import FlyoNitroPage from "@flyo/nitro-astro/FlyoNitroPage.astro";
import MetaInfoPage from "@flyo/nitro-astro/MetaInfoPage.astro";

const { slug } = Astro.params;
const resolveSlug = slug === undefined ? "" : slug;
const config = await useConfig(Astro);
let page;

try {
  if (!config.pages.includes(resolveSlug)) {
    return new Response("Not Found", {
      status: 404,
      statusText: "Page Not Found",
    });
  }

  page = await usePagesApi().page({ slug: resolveSlug });
} catch (e) {
  return new Response(e.body.name, {
    status: 404,
    statusText: "Page Not Found",
  });
}
---

<Layout title={page.title}>
  <MetaInfoPage page={page} slot="head" />
  <FlyoNitroPage page={page} />
</Layout>

To receive the config in the layout in src/layouts/Layout.astro:

---
import { useConfig } from "@flyo/nitro-astro";

const config = await useConfig(Astro);
const { title } = Astro.props;
const currentPath = Astro.url.pathname;
---

<!doctype html>
<html lang="en">
  <head>
    <title>{title}</title>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <!-- Auto-inject meta information for pages and entities -->
    <slot name="head" />
  </head>
  <body>
    {
      config.containers.nav.items.map((item: object) => (
        <a
          style="background-color: red; color: white"
          href={item.href}
          class={`nav-class ${currentPath === item.href ? "text-red" : ""}`}
        >
          {item.label}
          <br />
        </a>
      ))
    }
    <div class="container">
      <slot />
    </div>
  </body>
</html>

Blocks

Block Component Example (which are mostly located in src/components/flyo):

---
import { Image } from "astro:assets";
import { editableBlock } from "@flyo/nitro-astro";
import BlockSlot from "@flyo/nitro-astro/BlockSlot.astro";
const { block } = Astro.props;
---

<!-- Make the block editable if necessary -->
<div {...editableBlock(block)}>
  <!-- Content variable -->
  <div set:html={block.content.content.html} />

  <!-- Handling items -->
  {
    block.items.map((item: object) => (
      <div>
        {item.title}
        <a href={item.link.routes.detail}>Go to Detail</a>
      </div>
    ))
  }

  <!-- Image Proxy -->
  <Image
    src={block.content.image.source}
    alt={block.content.alt ?? ""}
    width={1920}
    height={768}
  />

  <!-- Handling slots -->
  <BlockSlot slot={block.slots.mysuperslotname} />
</div>

Entities

The Entity Details API provides all the information about an entity and the associated model data configured in the Flyo interface. You can request detail pages either by using a slug (with an additional schema ID) or by a unique ID.

Example: Request by slug (type ID 9999)

For a blog post, use src/pages/blog/[slug].astro with entityBySlug. Since slugs can be unique within an entity but may not be unique across the entire system, it’s recommended to include the schema ID to fetch the correct entity.

---
import Layout from "../../layouts/Layout.astro";
import { useEntitiesApi } from "@flyo/nitro-astro";
import MetaInfoEntity from "@flyo/nitro-astro/MetaInfoEntity.astro";

const { slug } = Astro.params;
let response = null;
try {
  response = await useEntitiesApi().entityBySlug({ 
    slug,
    lang: Astro.currentLocale,
    typeId: 9999,
  });
} catch (e) {
  return new Response(e.body, {
    status: 404,
    statusText: "Entity Not Found",
  });
}
const isProd = import.meta.env.PROD;
---

<Layout>
  <MetaInfoEntity response={response} slot="head" />
  <h1>{slug}</h1>
  <img src={response.model.image.source} style="width:100%" />
</Layout>
{
  isProd && (
    <script is:inline define:vars={{ api: response.entity.entity_metric.api }}>
      fetch(api)
    </script>
  )
}

Example: Request by unique ID

For fetching by unique ID, use src/pages/blog/[uniqueid].astro with entityByUniqueid. The unique ID is globally unique across the entire system, making it reliable for fetching specific entities.

const { uniqueid } = Astro.params;
// ....
await useEntitiesApi().entityByUniqueid({ 
    uniqueid,
    lang: Astro.currentLocale,
  });

Multiple Languages (i18n)

Refer to the Astro internationalization documentation to configure languages. Ensure that the languages used in Flyo are defined in the locales array, and set the default language in defaultLocale in astro.config.mjs.

import { defineConfig } from "astro/config"
export default defineConfig({
  i18n: {
    defaultLocale: "en",
    locales: ["en", "fr"],
  }
})

All endpoints accept a lang parameter to retrieve data in the desired language. The Nitro Astro package handles this automatically. However, since the Entity Details page is custom-built, you need to manually pass the language parameter.

  • For slug-based requests:
    await useEntitiesApi().entityBySlug({ slug, lang: Astro.currentLocale });
  • For unique ID-based requests:
    await useEntitiesApi().entityByUniqueid({ uniqueid, lang: Astro.currentLocale });

[!NOTE]
If your entity details are internationalized (i18n), you need to create separate detail pages for each language.

.
├── de
│   └── detail
│       └── [slug].astro
├── fr
│   └── detail
│       └── [slug].astro

The above structure would be /de/detail/[slug].astro and /fr/detail/[slug].astro.