-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
58 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
--- | ||
import LinkCardLayout from "@layouts/LinkCardLayout.astro" | ||
import { getOgpMetaData } from "@lib/getOgpMetaData" | ||
import type { OgpData } from "@type/ogpData-type" | ||
interface Props { | ||
url: string | ||
linkUrl?: string | ||
} | ||
const { url, linkUrl } = Astro.props | ||
const { env } = Astro.locals.runtime | ||
// check if the url is valid | ||
try { | ||
new URL(url) | ||
if (linkUrl) { | ||
new URL(linkUrl) | ||
} | ||
} catch (error) { | ||
throw new Error("Invalid URL") | ||
} | ||
const getOgpData = async (url: string) => { | ||
// cache key is the base64encoded url | ||
const cacheKey = btoa(url) | ||
const ogpCache: OgpData | null | undefined = await env.OGP_DATASTORE.get( | ||
cacheKey, | ||
{ type: "json" } | ||
) | ||
if (ogpCache) { | ||
return ogpCache | ||
} else { | ||
const ogpMetaData = await getOgpMetaData(url) | ||
await env.OGP_DATASTORE.put(cacheKey, JSON.stringify(ogpMetaData), { | ||
expirationTtl: 60 * 60 * 24 | ||
}) | ||
return ogpMetaData | ||
} | ||
} | ||
const ogpData = await getOgpData(url) | ||
--- | ||
|
||
<LinkCardLayout | ||
title={ogpData.ogpTitle ?? url} | ||
siteName={ogpData.ogpSiteName ?? new URL(url).hostname} | ||
description={ogpData.ogpDescription ?? ""} | ||
imageSrc={ogpData.ogpImageUrl} | ||
url={linkUrl ?? url} | ||
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { expect, test } from "vitest" | ||
import { fetchOgp } from "~/lib/fetcher/fetchOgp" | ||
import { getOgpMetaData } from "@lib/getOgpMetaData" | ||
import { normalLinkUrl, normalLinkDataExpectedResponse } from "./testData" | ||
|
||
const encodedUrl = encodeURIComponent(normalLinkUrl) | ||
|
||
test("URIエンコードされたURLからOGPデータを取得する", async () => { | ||
const res = await fetchOgp(encodedUrl) | ||
const res = await getOgpMetaData(encodedUrl) | ||
expect(res).deep.equal(normalLinkDataExpectedResponse) | ||
}) |