-
-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: make the open graph tags dynamic based on the base64
and url
query parameters
#1122
Open
helios2003
wants to merge
23
commits into
asyncapi:master
Choose a base branch
from
helios2003:fix/dynamic-og
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
730df15
refactor
helios2003 fdc4295
change the route
helios2003 9c9b3fd
Merge branch 'master' into fix/dynamic-og
KhudaDad414 2d3c6e0
add tests
helios2003 e319a47
Merge branch 'master' into testing
asyncapi-bot 4cd0d6f
testing the breaking points
helios2003 7eb005a
Merge branch 'fix/dynamic-og' of https://github.com/helios2003/studio…
helios2003 1f718ba
add logging for testing purposes
helios2003 1367312
changes in parser
helios2003 fc7da7e
fix certain edge cases
helios2003 f6c3113
minor fix
helios2003 a4b3581
add vercel for image generator service
helios2003 ce5d906
set up configuration for tests
helios2003 67d5960
add og tests
helios2003 032a980
change lock file
helios2003 37f5b2d
configure open graph tags to work with normal tests
helios2003 2efc878
Merge branch 'fix/dynamic-og' into testing
helios2003 945a0b8
Merge pull request #5 from helios2003/testing
helios2003 16eea66
change the imports based on the new converter version
helios2003 b7da6cc
change the website URL
helios2003 1cdb9b8
change jest-env version
helios2003 1363905
update
helios2003 19858da
Merge branch 'master' into fix/dynamic-og
helios2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,105 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import parseURL from "@/helpers/parser"; | ||
import { DocumentInfo } from "@/types"; | ||
import axios from "axios"; | ||
import { metadata } from "@/app/page"; | ||
|
||
export async function GET(request: NextRequest) { | ||
const Base64searchParams = request.nextUrl.searchParams.get('base64'); | ||
const URLsearchParams = request.nextUrl.searchParams.get('url'); | ||
console.log('Base64searchParams:', Base64searchParams); | ||
console.log('URLsearchParams:', URLsearchParams); | ||
try { | ||
if (!Base64searchParams && !URLsearchParams) return new NextResponse(null, { status: 200 }); | ||
let info: DocumentInfo | null = null; | ||
|
||
if (Base64searchParams) { | ||
// directly run the parsing function | ||
info = await parseURL(Base64searchParams); | ||
} | ||
if (URLsearchParams) { | ||
// fetch the document information from the URL | ||
try { | ||
const response = await axios.get(URLsearchParams); | ||
if (response.status === 200) { | ||
info = await parseURL(response.data); | ||
} else { | ||
return new NextResponse("Not a valid URL", { status: 500 }); | ||
} | ||
} catch (error) { | ||
return new NextResponse("Not a valid URL", { status: 500 }); | ||
} | ||
} | ||
|
||
if (!info) { | ||
const ogImage = "https://raw.githubusercontent.com/asyncapi/studio/master/apps/studio-next/public/img/meta-studio-og-image.jpeg"; | ||
|
||
const crawlerInfo = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>"${metadata.openGraph?.title}"</title> | ||
<meta property="og:title" content="${metadata.openGraph?.title}" /> | ||
<meta property="og:description" content="${metadata.openGraph?.description}" /> | ||
<meta property="og:url" content="${metadata.openGraph?.url}" /> | ||
<meta property="og:image" content="${ogImage}" /> | ||
` | ||
return new NextResponse(crawlerInfo, { | ||
headers: { | ||
'Content-Type': 'text/html', | ||
}, | ||
}) | ||
} | ||
|
||
let ogImageParams = new URLSearchParams(); | ||
|
||
if (info.title) { | ||
ogImageParams.append('title', info.title.toString()); | ||
} | ||
if (info.description) { | ||
ogImageParams.append('description', info.description.toString()); | ||
} | ||
if (info.numServers) { | ||
ogImageParams.append('numServers', info.numServers.toString()); | ||
} | ||
if (info.numChannels) { | ||
ogImageParams.append('numChannels', info.numChannels.toString()); | ||
} | ||
if (info.numOperations) { | ||
ogImageParams.append('numOperations', info.numOperations.toString()); | ||
} | ||
if (info.numMessages) { | ||
ogImageParams.append('numMessages', info.numMessages.toString()); | ||
} | ||
|
||
const ogImageurl = `https://ogp-studio.netlify.app/og?${ogImageParams.toString()}`; | ||
|
||
const crawlerInfo = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>${info.title}</title> | ||
${info.title ? `<meta property="og:title" content="${info.title}" />` : ''} | ||
${info.description ? `<meta property="og:description" content="${info.description}" />` : ''} | ||
<meta property="og:image" content=${ogImageurl} /> | ||
</head> | ||
</html> | ||
`; | ||
console.log(crawlerInfo); | ||
return new NextResponse(crawlerInfo, { | ||
status: 200, | ||
headers: { | ||
'Content-Type': 'text/html', | ||
}, | ||
}); | ||
} catch (err) { | ||
return new NextResponse("Not a valid URL", { status: 500 }); | ||
} | ||
} | ||
|
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,52 @@ | ||
import { Input, Parser } from '@asyncapi/parser'; | ||
import { DocumentInfo } from '@/types'; | ||
|
||
export default async function parseURL(asyncapiDocument: string): Promise<DocumentInfo | null> { | ||
const parser = new Parser(); | ||
|
||
let decodedDocument: Input = ""; | ||
|
||
if (asyncapiDocument.startsWith('https') || asyncapiDocument.startsWith('http')) { | ||
decodedDocument = asyncapiDocument; | ||
} else { | ||
decodedDocument = Buffer.from(asyncapiDocument, "base64").toString("utf-8"); | ||
} | ||
|
||
const { document, diagnostics } = await parser.parse(decodedDocument); | ||
console.log(document); | ||
if (diagnostics.some(diagnostic => diagnostic.severity != 0)) { | ||
return null; | ||
} | ||
|
||
let title = document?.info().title(); | ||
if (title) { | ||
title = title.length <= 20 ? title : title.slice(0, 20) + "..."; | ||
} | ||
const version = document?.info().version(); | ||
|
||
let description = document?.info().description(); | ||
if (description) { | ||
description = description.length <= 100 ? description : description.slice(0, 100) + "..."; | ||
} | ||
|
||
const servers = document?.allServers(); | ||
const channels = document?.allChannels(); | ||
const operations = document?.allOperations(); | ||
const messages = document?.allMessages(); | ||
|
||
const numServers = servers?.length; | ||
const numChannels = channels?.length; | ||
const numOperations = operations?.length; | ||
const numMessages = messages?.length; | ||
|
||
const response = { | ||
title, | ||
version, | ||
description, | ||
numServers, | ||
numChannels, | ||
numOperations, | ||
numMessages | ||
}; | ||
return response; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The configuration and this middleware achieve the purpose, right? I don't understand why we are adding both. |
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,27 @@ | ||
import { NextRequest, NextResponse, userAgent } from "next/server"; | ||
import crawlers from 'crawler-user-agents'; | ||
|
||
export async function middleware(request: NextRequest) { | ||
const userAgents = crawlers.map(crawler => crawler.pattern); | ||
const requestInfo = userAgent(request); | ||
const res = NextResponse.next(); | ||
|
||
// for (const ua of userAgents) { | ||
// if (requestInfo.ua.toLowerCase().includes(ua.toLowerCase())) { | ||
|
||
const documentURL = request.nextUrl.searchParams.get("url"); | ||
const encodedDocument = request.nextUrl.searchParams.get("base64"); | ||
|
||
if (!encodedDocument && !documentURL) { | ||
return res; | ||
} | ||
if (encodedDocument) { | ||
return NextResponse.rewrite(new URL(`/api/crawler?base64=${encodedDocument}`, request.url)); | ||
} | ||
if (documentURL) { | ||
return NextResponse.rewrite(new URL(`/api/crawler?url=${documentURL}`, request.url)); | ||
} | ||
// } | ||
// } | ||
return res; | ||
} |
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,3 +1,13 @@ | ||
import type specs from '@asyncapi/specs'; | ||
|
||
export type SpecVersions = keyof typeof specs.schemas; | ||
|
||
export interface DocumentInfo { | ||
title? : string, | ||
version? : string, | ||
description? : string, | ||
numServers? : number, | ||
numChannels? : number, | ||
numOperations? : number, | ||
numMessages?: number | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.