From e105efe6e7aee1397b465d65c6ad4a0e9bee3890 Mon Sep 17 00:00:00 2001 From: Fuxing Loh Date: Thu, 16 Nov 2023 20:57:37 +0800 Subject: [PATCH 1/2] chore(website): remove tracing and use fetch assets --- packages/crypto-frontmatter/src/cli.ts | 38 ++++++++++++++++++++------ website/app/[caip2]/[asset]/page.tsx | 24 ++++++---------- website/next.config.mjs | 5 ---- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/packages/crypto-frontmatter/src/cli.ts b/packages/crypto-frontmatter/src/cli.ts index 20418f795..2654ec8f6 100644 --- a/packages/crypto-frontmatter/src/cli.ts +++ b/packages/crypto-frontmatter/src/cli.ts @@ -3,27 +3,47 @@ import { join } from 'node:path'; import { Command, Option, runExit } from 'clipanion'; -import { getFrontmatterIndexArray, getInstalledFrontmatterCollection, getNodeModulesPath } from './index'; +import { + FrontmatterCollection, + getFrontmatterIndexArray, + getInstalledFrontmatterCollection, + getNodeModulesPath, +} from './index'; export class MirrorCommand extends Command { static override paths = [[`mirror`]]; private target = Option.String(); + private include = Option.Array(`--include`, { required: false }) ?? ['index.json', 'frontmatter.json', 'images']; + + private async mirrorFile(collection: FrontmatterCollection, file: string): Promise { + const from = getNodeModulesPath(collection.caip2, collection.namespace, file); + const to = join(this.target, file); + await copyFile(from, to); + } async execute(): Promise { await mkdir(this.target, { recursive: true }); - const collections = await getInstalledFrontmatterCollection(); - for (const collection of collections) { + for (const collection of await getInstalledFrontmatterCollection()) { let count = 0; - const indexArray = await getFrontmatterIndexArray(collection.caip2, collection.namespace); - for (const index of indexArray) { - for (const image of index.fields.images) { - const from = getNodeModulesPath(collection.caip2, collection.namespace, image.path); - const to = join(this.target, image.path); - await copyFile(from, to); + if (this.include.includes('index.json')) { + await this.mirrorFile(collection, 'index.json'); + count++; + } + + for (const index of await getFrontmatterIndexArray(collection.caip2, collection.namespace)) { + if (this.include.includes('frontmatter.json')) { + await this.mirrorFile(collection, index.fileId + '.json'); count++; } + + if (this.include.includes('images')) { + for (const image of index.fields.images) { + await this.mirrorFile(collection, image.path); + count++; + } + } } this.context.stdout.write(`Mirrored ${count} files for "${collection.caip2}/${collection.namespace}"\n`); diff --git a/website/app/[caip2]/[asset]/page.tsx b/website/app/[caip2]/[asset]/page.tsx index 6a5d6db72..a0d3ea096 100644 --- a/website/app/[caip2]/[asset]/page.tsx +++ b/website/app/[caip2]/[asset]/page.tsx @@ -1,4 +1,4 @@ -import { FrontmatterContent, getFrontmatterContent as getUsingFs } from 'crypto-frontmatter'; +import { computeFileId, FrontmatterContent } from 'crypto-frontmatter'; import { Metadata } from 'next'; import Image from 'next/image'; import { notFound } from 'next/navigation'; @@ -7,22 +7,20 @@ import type { ReactElement } from 'react'; import { ContentedProse } from '@/components/contented/ContentedProse'; import { renderHighlighterHtml } from '@/components/contented/ShikiHighlighter'; -async function getFrontmatterContent(params: { - caip2: string; - asset: string; -}): Promise { +async function getFrontmatterContent(params: { caip2: string; asset: string }): Promise { const caip19 = `${decodeURIComponent(params.caip2)}/${decodeURIComponent(params.asset)}`; - return getUsingFs(caip19); + const fileId = computeFileId(caip19); + const response = await fetch(`${process.env.BASE_URL}/_crypto-frontmatter/${fileId}.json`); + if (!response.ok) { + return notFound(); + } + return await response.json(); } export async function generateMetadata(props: Parameters[0]): Promise { const frontmatter = await getFrontmatterContent(props.params); - if (frontmatter === undefined) { - return notFound(); - } const title = frontmatter.fields.title ?? frontmatter.fields.symbol; - return { title: title, description: frontmatter.fields.description, @@ -45,10 +43,6 @@ export default async function Page(props: { }; }): Promise { const frontmatter = await getFrontmatterContent(props.params); - if (frontmatter === undefined) { - return notFound(); - } - const image = frontmatter.fields.images?.find((image) => image.type === 'logo'); return ( @@ -69,7 +63,7 @@ export default async function Page(props: {
-
Frontmatter.json
+
frontmatter.json