Skip to content

Commit 8419d21

Browse files
committed
move glob imports to each layout
1 parent a8ebd74 commit 8419d21

20 files changed

Lines changed: 205 additions & 190 deletions

File tree

‎apps/codingcatdev/src/content/course/intro-to-svelte/lesson/fundamentals-benefits-of-sveltekit.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ weight: 5
1010
youtube: https://youtu.be/Z_DAxhYbjyM
1111
---
1212

13-
## **Benefits of SvelteKit**
13+
## Benefits of SvelteKit
1414

1515
Now let’s go through, in more detail, some of the benefits of SvelteKit.
1616

17-
Here we have a diagram with some of the reasons why SvelteKit is becoming so popular. First, let’s look at the different rendering options. With SvelteKit, you can choose how you want to render content based on the route it is on. Originally with Svelte, the content was always rendered on the client side and you didn’t have access to the server at all. But with SvelteKit, we have the flexibility to decide what option best fits our application.
17+
Here we have a diagram with some of the reasons why SvelteKit is becoming so popular. First, let’s look at the different rendering options. With SvelteKit, you can choose how you want to render content based on the route it is on. Originally with Svelte, the content was always rendered on the client side and you didn’t have access to the server at all. But with SvelteKit, we have the flexibility to decide what option best fits our application.
1818

19-
You may have seen some of these acronyms before, but if not that’s the thing about web development, there are always room for more acronyms. The options we have in SvelteKit include Client Side Rendering (CSR) like Svelte provided, but also Server Side Rendering (SSR) and Static Site Generation (SSG). With SvelteKit, we can even use both and choose which is best for each route. First, let’s take a look at server-side rendering.
19+
You may have seen some of these acronyms before, but if not that’s the thing about web development, there are always room for more acronyms. The options we have in SvelteKit include Client Side Rendering (CSR) like Svelte provided, but also Server Side Rendering (SSR) and Static Site Generation (SSG). With SvelteKit, we can even use both and choose which is best for each route. First, let’s take a look at server-side rendering.
2020

21-
This means that the content is generated by the server each time a user visits the page, which is ideal for SEO and content that always needs to be up to date. You would use this on sites that require a lot of user interaction or rely on a database for the latest content.
21+
This means that the content is generated by the server each time a user visits the page, which is ideal for SEO and content that always needs to be up to date. You would use this on sites that require a lot of user interaction or rely on a database for the latest content.
2222

23-
Next, there is static site generation where all of the files get generated at build time by the server and are cached at that moment in time. Your browser is fetching that same cached content over and over, making it extremely fast. This is great for content that doesn’t change. A good use case for this would be a blog site, landing page, or a store with products that don’t change very often. And last we have Client Side Rendering or you may have heard the term SPA, Single Page Application. This is where the browser handles most of the work and first load times are slower, but once it is loaded the site can feel more responsive.
23+
Next, there is static site generation where all of the files get generated at build time by the server and are cached at that moment in time. Your browser is fetching that same cached content over and over, making it extremely fast. This is great for content that doesn’t change. A good use case for this would be a blog site, landing page, or a store with products that don’t change very often. And last we have Client Side Rendering or you may have heard the term SPA, Single Page Application. This is where the browser handles most of the work and first load times are slower, but once it is loaded the site can feel more responsive.
2424

25-
Next, we’ll look at the 2nd benefit of SvelteKit, performance.
25+
Next, we’ll look at the 2nd benefit of SvelteKit, performance.

‎apps/codingcatdev/src/content/course/intro-to-svelte/lesson/fundamentals-performance.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ weight: 6
1010
youtube: https://youtu.be/W_3YmcybZKc
1111
---
1212

13-
## **Performance**
13+
## Performance
1414

1515
Welcome back, performance is one of the greatest benefits of SvelteKit. The hard jobs that make creating performant applications so difficult come for free with Sveltekit. It uses a tool called Vite, with a Svelte plugin, to provide instant server starts, lightning-fast development with hot module reloading, bundling, and the code-splitting that optimizes your code for production.
1616

17-
Code-splitting is breaking up your app to improve the page loading time by only using what is needed for the current page. Instead of a single JavaScript bundle, it splits each route into it’s own chunk. It is essentially a fancy name for dynamic imports. This creates an optimized, production ready version of your application. Now, let’s move on to file system based routing.
17+
Code-splitting is breaking up your app to improve the page loading time by only using what is needed for the current page. Instead of a single JavaScript bundle, it splits each route into it’s own chunk. It is essentially a fancy name for dynamic imports. This creates an optimized, production ready version of your application. Now, let’s move on to file system based routing.

‎apps/codingcatdev/src/content/course/intro-to-svelte/lesson/fundamentals-what-is-svelte.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ title: Fundamentals - What is Svelte
1010
weight: 3
1111
youtube: https://youtu.be/dZ1CI3u1MYA
1212
---
13-
## **What is Svelte?**
13+
14+
## What is Svelte?\*\*
1415

1516
Svelte is a component framework that allows you to break up your application into reusable chunks and provides some sugary syntax, making your code base easy to understand and very graceful.
1617

1718
Svelte is similar to React & Vue, with one key difference, there is NO VIRTUAL DOM. Instead of shipping the framework to the browser and letting it do the work, Svelte compiles itself away during a build step into vanilla html, css, and js. Making it one of the leanest frameworks out there
1819

19-
If you have experience with HTML, CSS, and JavaScript, you will feel right at home with Svelte. Having just a basic understanding is all that you need and we’ll cover everything else in this course. Next let’s take a look at what we’ll be using to write our applications.
20+
If you have experience with HTML, CSS, and JavaScript, you will feel right at home with Svelte. Having just a basic understanding is all that you need and we’ll cover everything else in this course. Next let’s take a look at what we’ll be using to write our applications.
Lines changed: 107 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,121 @@
11
import { ContentType, ContentPublished } from '$lib/types';
2-
import type { Content, Course, Podcast } from '$lib/types';
2+
import type { Content, Course } from '$lib/types';
33

44
const LIMIT = 20;
55

6-
// In Dev Mode read directly from content directory
7-
const markdownFiles: {
8-
course: Course[];
9-
framework: Content[];
10-
language: Content[];
11-
page: Content[];
12-
podcast: Podcast[];
13-
post: Content[];
14-
tutorial: Content[];
15-
} = {
16-
course: [],
17-
framework: [],
18-
language: [],
19-
page: [],
20-
podcast: [],
21-
post: [],
22-
tutorial: []
23-
};
24-
25-
console.log('Building top metadata level');
26-
const modules = import.meta.glob(['../../content/course/*/*.md', '../../content/*/*.md']);
27-
for (const path in modules) {
28-
await modules[path]().then((mod) => {
29-
const splitPath = path.replace('../../content/', '').split('/');
30-
const type = splitPath.at(0);
31-
const slug = splitPath.at(1);
32-
33-
console.log(splitPath);
34-
35-
if (!type || !slug) {
36-
console.error('Missing name or type');
37-
return;
38-
}
39-
40-
const mdsvx = mod as {
41-
default: {
42-
render: () => { html: string };
6+
export const parseModules = async (modules: Record<string, () => Promise<unknown>>) => {
7+
const contentList: Content[] = [];
8+
for (const path in modules) {
9+
await modules[path]().then((mod) => {
10+
const splitPath = path.split('/');
11+
const type = splitPath.at(-2);
12+
const slug = splitPath.at(-1);
13+
14+
console.log(`Precompiling: ${type}/${slug}`);
15+
16+
if (!type || !slug) {
17+
console.error('Missing name or type');
18+
return;
19+
}
20+
21+
const mdsvx = mod as {
22+
default: {
23+
render: () => { html: string };
24+
};
25+
metadata: Content;
26+
};
27+
const { html } = mdsvx.default.render();
28+
/**
29+
* This needs to match the function that adds the
30+
* same data to Firestore
31+
*/
32+
const content = {
33+
...mdsvx?.metadata,
34+
cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
35+
type: type as ContentType,
36+
html,
37+
weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
38+
published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : 'draft',
39+
start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900'),
4340
};
44-
metadata: Content;
45-
};
46-
const { html } = mdsvx.default.render();
47-
/**
48-
* This needs to match the function that adds the
49-
* same data to Firestore
50-
*/
51-
const content = {
52-
...mdsvx?.metadata,
53-
cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
54-
type: type as ContentType,
55-
html,
56-
weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
57-
published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : 'draft',
58-
start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900')
59-
};
60-
markdownFiles[type as keyof typeof markdownFiles].push(content);
61-
});
41+
contentList.push(content);
42+
});
43+
}
44+
return contentList;
6245
}
6346

6447
console.log('Add Lessons to courses');
65-
const lessonModules = import.meta.glob('../../content/course/*/lesson/*.md');
66-
for (const path in lessonModules) {
67-
await lessonModules[path]().then((mod) => {
68-
const splitPath = path.replace('../../content/', '').split('/');
69-
const type = splitPath.at(0);
70-
const slug = splitPath.at(1);
71-
const lessonSlug = splitPath?.at(3)?.replace(/\.[^/.]+$/, '');
72-
73-
console.log(splitPath);
74-
75-
if (!type || !slug || !lessonSlug) {
76-
console.error('Lesson Param missing');
77-
return;
78-
}
79-
80-
const mdsvx = mod as {
81-
default: {
82-
render: () => { html: string };
48+
49+
export const parseLessonModules = async ({ lessonModules, courses }: { lessonModules: Record<string, () => Promise<unknown>>, courses: Course[] }) => {
50+
for (const path in lessonModules) {
51+
await lessonModules[path]().then((mod) => {
52+
const splitPath = path.split('/');
53+
const type = splitPath.at(-4);
54+
const slug = splitPath.at(-3);
55+
const lessonSlug = splitPath?.at(-1)?.replace(/\.[^/.]+$/, '');
56+
57+
console.log(`Precompiling Lesson: ${type}/${slug}`);
58+
59+
if (!type || !slug || !lessonSlug) {
60+
console.error('Lesson Param missing');
61+
return;
62+
}
63+
64+
const mdsvx = mod as {
65+
default: {
66+
render: () => { html: string };
67+
};
68+
metadata: Content;
8369
};
84-
metadata: Content;
85-
};
86-
const { html } = mdsvx.default.render();
87-
/**
88-
* This needs to match the function that adds the
89-
* same data to Firestore
90-
*/
91-
const content = {
92-
...mdsvx?.metadata,
93-
cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
94-
type: ContentType.lesson,
95-
slug: lessonSlug,
96-
courseSlug: slug,
97-
html,
98-
weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
99-
published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : 'draft',
100-
start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900')
101-
};
102-
103-
markdownFiles.course
104-
.filter((c) => c.slug === slug)
105-
.map((c) => {
106-
c?.lesson ? c.lesson.push(content) : (c['lesson'] = [content]);
107-
});
108-
});
70+
const { html } = mdsvx.default.render();
71+
/**
72+
* This needs to match the function that adds the
73+
* same data to Firestore
74+
*/
75+
const content = {
76+
...mdsvx?.metadata,
77+
cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
78+
type: ContentType.lesson,
79+
slug: lessonSlug,
80+
courseSlug: slug,
81+
html,
82+
weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
83+
published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : 'draft',
84+
start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900')
85+
};
86+
87+
courses
88+
.filter((c) => c.slug === slug)
89+
.map((c) => {
90+
c?.lesson ? c.lesson.push(content) : (c['lesson'] = [content]);
91+
});
92+
});
93+
}
94+
return courses;
10995
}
11096

97+
11198
/**
11299
* List all content from specified content type
113100
* allows for optionally sending after object
114101
* */
115102
export const listContent = async ({
116-
contentType,
103+
contentItems,
117104
after,
118105
limit,
119106
contentFilter = (c) => c.published === ContentPublished.published
120107
}: {
121-
contentType: ContentType;
108+
contentItems: Content[];
122109
after?: number;
123110
limit?: number;
124111
contentFilter?: (c: Content) => boolean;
125112
}) => {
126113
const theLimit = limit || LIMIT;
127114
const theAfter = after || 0;
128115

129-
console.log(`List for type: ${contentType}, limit of ${theLimit}`);
116+
console.log(`List limit of ${theLimit}`);
130117

131-
const fullContent = markdownFiles[contentType as keyof typeof markdownFiles]
118+
const fullContent = contentItems
132119
.filter(contentFilter)
133120
.sort((a, b) => new Date(b.start).valueOf() - new Date(a.start).valueOf());
134121

@@ -141,10 +128,16 @@ export const listContent = async ({
141128
};
142129
};
143130

144-
export const getContentBySlug = async (contentType: ContentType, slug: string) => {
145-
console.debug(`Searching for content type: ${contentType} slug: ${slug}`);
131+
export const getContentBySlug = async ({
132+
contentItems,
133+
slug
134+
}: {
135+
contentItems: Content[];
136+
slug: string
137+
}) => {
138+
console.debug(`Searching for slug: ${slug} in ${contentItems.length} items`);
146139

147-
const doc = markdownFiles[contentType as keyof typeof markdownFiles]
140+
const doc = contentItems
148141
.filter(
149142
(c) =>
150143
c.slug == slug &&
@@ -175,38 +168,22 @@ export const getContentBySlug = async (contentType: ContentType, slug: string) =
175168
/**
176169
* Get lesson by course and slug
177170
* */
178-
export const getLessonFromCourseSlug = async (courseSlug: string, slug: string) => {
171+
export const getLessonFromCourseSlug = async ({ courseSlug, slug, courseItems }:
172+
{
173+
courseSlug: string, slug: string,
174+
courseItems: Content[];
175+
}) => {
179176
console.debug(`Searching for course: ${courseSlug}`);
180177

181-
const course = markdownFiles.course
182-
.filter(
183-
(c) =>
184-
c.slug == courseSlug &&
185-
new Date(c.start) <= new Date() &&
186-
c.published === ContentPublished.published
187-
)
188-
.sort((a, b) => new Date(b.start).valueOf() - new Date(a.start).valueOf())
189-
.slice(0, 1)
190-
.map((c: Course) => {
191-
return {
192-
...c,
193-
lesson: c?.lesson
194-
?.filter(
195-
(l) => new Date(l.start) <= new Date() && l.published === ContentPublished.published
196-
)
197-
.sort((a, b) => b.weight || 99 - (a.weight || 1))
198-
};
199-
})
200-
.at(0);
178+
const course = await getContentBySlug({ contentItems: courseItems, slug: courseSlug });
201179
if (!course) {
202180
console.debug(`course not found`);
203181
return null;
204182
}
205183
console.debug(`Searching within ${course.slug} for lesson slug: ${slug}`);
206184

207-
const doc = markdownFiles.course
208-
.filter((c) => c.slug === course.slug)
209-
?.at(0)
185+
// TODO: ADD Pro check?
186+
const doc = course
210187
?.lesson?.filter(
211188
(l) =>
212189
l.slug == slug &&
@@ -223,11 +200,6 @@ export const getLessonFromCourseSlug = async (courseSlug: string, slug: string)
223200

224201
return {
225202
...doc,
226-
// Reduce html shipped for links
227-
lesson: course?.lesson?.map((l) => {
228-
// delete l.html;
229-
return l;
230-
}),
231203
courseSlug: course.slug
232204
};
233205
};
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { listContent } from '$lib/server/content';
1+
import { listContent, parseModules } from '$lib/server/content';
22
import { ContentType } from '$lib/types';
33

44

55
const contentType = ContentType.post;
66

77
export const load = (async () => {
8+
const modules = import.meta.glob(['../../../content/post/*.md']);
9+
const contentItems = await parseModules(modules);
810
return {
911
contentType,
10-
...(await listContent({ contentType }))
12+
...(await listContent({ contentItems }))
1113
};
1214
});
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { listContent } from '$lib/server/content';
1+
import { listContent, parseModules } from '$lib/server/content';
22
import { ContentType } from '$lib/types';
33

44
const contentType = ContentType.course;
55

66
export const load = (async () => {
7+
const modules = await import.meta.glob(['../../../content/course/*/*.md']);
8+
const contentItems = await parseModules(modules);
79
return {
810
contentType,
9-
...(await listContent({ contentType }))
11+
...(await listContent({ contentItems }))
1012
};
11-
});
13+
});

0 commit comments

Comments
 (0)