Skip to content

Commit

Permalink
prerender single route
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrubinger committed Nov 11, 2022
1 parent 9f507fa commit f0fd65f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/routes/single/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const prerender = true;
1 change: 1 addition & 0 deletions src/routes/single/+page.svelte
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" />
Expand Down
55 changes: 55 additions & 0 deletions test/+page.server.js
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;
}
20 changes: 20 additions & 0 deletions test/+page.svelte
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>

1 comment on commit f0fd65f

@vercel
Copy link

@vercel vercel bot commented on f0fd65f Nov 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.