Skip to content

Commit

Permalink
use endpoints instead of pulling file metadata into browser memory
Browse files Browse the repository at this point in the history
  • Loading branch information
mvasigh committed May 15, 2021
1 parent 14a35ab commit 0715364
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 73 deletions.
33 changes: 1 addition & 32 deletions src/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,32 +1 @@
const slugRegex = /([\w-]+)\.(svelte\.md|md|svx)/i;



export async function getAllPosts(globArr) {
const postPromises = [];

for (let [path, resolver] of Object.entries(globArr)) {
const match = path.match(slugRegex);

if (!match || !match[1]) continue;

const slug = match[1];
const promise = resolver().then((post) => [
slug,
{
...post.metadata,
date: new Date(post.metadata.date)
}
]);
postPromises.push(promise);
}

return Promise.all(postPromises);
}

export function fromEntries(entries) {
return entries.reduce((acc, [key, val]) => {
acc[key] = val;
return acc;
}, {});
}
export const slugFromPath = (path) => path.match(/([\w-]+)\.(svelte\.md|md|svx)/i)?.[1] ?? null;
10 changes: 2 additions & 8 deletions src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<script context="module">
const files = import.meta.glob('./posts/*.{md,svx,svelte.md}');
import { getAllPosts } from '$lib/util';
/**
* @type {import('@sveltejs/kit').Load}
*/
export async function load() {
const allPosts = await getAllPosts(files);
const posts = allPosts.filter(([_, post]) => post.published);
posts.sort(([_slugA, a], [_slugB, b]) => a.date > b.date ? -1 : 1);
export async function load({ fetch }) {
const posts = await fetch('/posts.json').then((res) => res.json());
return {
props: {
Expand Down
28 changes: 28 additions & 0 deletions src/routes/posts/[slug].json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { slugFromPath } from '$lib/util';

/**
* @type {import('@sveltejs/kit').RequestHandler}
*/
export async function get({ params }) {
const modules = import.meta.glob(`./*.{md,svx,svelte.md}`);

let match;
for (const [path, resolver] of Object.entries(modules)) {
if (slugFromPath(path) === params.slug) {
match = [path, resolver];
break;
}
}

if (!match) {
return {
status: 404
};
}

const post = await match[1]();

return {
body: post.metadata
};
}
10 changes: 2 additions & 8 deletions src/routes/posts/__layout.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
<script context="module">
import { getAllPosts, fromEntries } from '$lib/util';
const files = import.meta.glob('./*.{md,svx,svelte.md}');
/**
* @type {import('@sveltejs/kit').Load}
*/
export async function load({ page }) {
const slug = page.path.replace('/posts/', '');
const posts = await getAllPosts(files);
const post = fromEntries(posts)[slug];
export async function load({ page, fetch }) {
const post = await fetch(`${page.path}.json`).then(res => res.json());
if (!post || !post.published) {
return {
Expand Down
40 changes: 15 additions & 25 deletions src/routes/posts/index.json.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
const slug = path => path.match(/([\w-]+)\.(svelte\.md|md|svx)/i)?.[1] ?? null;
import { slugFromPath } from '$lib/util';

export async function getAllPosts(globArr) {
const postPromises = [];
/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get() {
const modules = import.meta.glob('./*.{md,svx,svelte.md}');

for (let [path, resolver] of Object.entries(globArr)) {
const match = path.match(slugRegex);
const postPromises = [];

if (!match || !match[1]) continue;
for (let [path, resolver] of Object.entries(modules)) {
const slug = slugFromPath(path);
const promise = resolver().then((post) => [slug, post.metadata]);

const slug = match[1];
const promise = resolver().then((post) => [
slug,
{
...post.metadata,
date: new Date(post.metadata.date)
}
]);
postPromises.push(promise);
}

return Promise.all(postPromises);
}
const posts = await Promise.all(postPromises);
const publishedPosts = posts.filter(([slug, metadata]) => metadata.published);

publishedPosts.sort(([_slugA, a], [_slugB, b]) => (new Date(a.date) > new Date(b.date) ? -1 : 1));

/** @type {import('@sveltejs/kit').RequestHandler} */
export async function get(req) {
const files = import.meta.glob('./posts/*.{md,svx,svelte.md}');



console.log(req);

}
return {
body: publishedPosts
};
}

0 comments on commit 0715364

Please sign in to comment.