-
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
1 parent
9f507fa
commit f0fd65f
Showing
4 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const prerender = true; |
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,4 @@ | ||
|
||
<svelte:head> | ||
<title>metatag test</title> | ||
<meta name="title" content="metatag test" /> | ||
|
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,55 @@ | ||
import { error } from '@sveltejs/kit'; | ||
import { JSDOM } from 'jsdom'; | ||
|
||
// TODO: to make this work on vercel, adjust like this: https://github.com/Automattic/node-canvas/issues/1779 | ||
|
||
|
||
let baseUrl = 'http://ogimage.ghwb/'; | ||
let baseUrlPostfix = '?gh_beta=ogimage'; | ||
|
||
|
||
/** @type {import('./$types').PageServerLoad} */ | ||
export async function load({}) { | ||
let dom = await fetchPageAsDom(baseUrl); | ||
|
||
// console.log(dom.window.document.querySelector('.secondary-nav-item.deals-link').textContent); // "Hello world" | ||
let top10links = dom.window.document.querySelectorAll('#gh_m_top .line-clamp a'); | ||
|
||
let images = await getImages(top10links); | ||
|
||
images = images.map(i => { | ||
return i.replace('https://geizhals-og-image.vercel.app', 'http://localhost:5173') | ||
}); | ||
|
||
return { | ||
images | ||
}; | ||
|
||
throw error(404, 'Not found'); | ||
} | ||
|
||
async function getImages(links) { | ||
let collectedImages = []; | ||
|
||
let promises = [...links].map((link) => { | ||
return fetchPageAsDom(baseUrl + link + baseUrlPostfix); | ||
}); | ||
|
||
let results = await Promise.all(promises); | ||
|
||
results.forEach((pageDOM) => { | ||
let image = pageDOM.window.document | ||
.querySelector('meta[property="og:image"]') | ||
.getAttribute('content'); | ||
collectedImages.push(image); | ||
}); | ||
|
||
return collectedImages; | ||
} | ||
|
||
async function fetchPageAsDom(url) { | ||
let r = await fetch(url); | ||
let text = await r.text(); | ||
let dom = new JSDOM(text); | ||
return dom; | ||
} |
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,20 @@ | ||
<script> | ||
export let data; | ||
</script> | ||
|
||
<ul> | ||
{#each data.images as image} | ||
<li><img alt="" src={image} /></li> | ||
{/each} | ||
</ul> | ||
|
||
<style> | ||
ul { | ||
list-style: none; | ||
display: grid; | ||
grid-template-columns: repeat(3, 500px); | ||
gap: 1rem; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
</style> |
f0fd65f
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.
Successfully deployed to the following URLs:
geizhals-og-image – ./
geizhals-og-image.vercel.app
geizhals-og-image-git-main-grooovinger.vercel.app
geizhals-og-image-grooovinger.vercel.app