Skip to content

Commit ae38d64

Browse files
PuruVJRich Harris
andauthored
fix(site-2): Prerender nav data (#8747)
* simplify nav * updates * fix link * Dont need this anymore * Push * minor re-arrangement --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 312f944 commit ae38d64

File tree

2 files changed

+89
-81
lines changed

2 files changed

+89
-81
lines changed
Lines changed: 8 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { get_blog_data, get_blog_list } from '$lib/server/blog/index.js';
2-
import { get_docs_data, get_docs_list } from '$lib/server/docs/index.js';
3-
import { get_examples_data, get_examples_list } from '$lib/server/examples/index.js';
4-
import { get_tutorial_data, get_tutorial_list } from '$lib/server/tutorial/index.js';
1+
export const load = async ({ url, fetch }) => {
2+
const nav_list = await fetch('/nav.json').then((r) => r.json());
3+
4+
return {
5+
nav_title: get_nav_title(url),
6+
nav_links: nav_list
7+
};
8+
};
59

610
/** @param {URL} url */
711
function get_nav_title(url) {
@@ -23,80 +27,3 @@ function get_nav_title(url) {
2327

2428
return '';
2529
}
26-
27-
/**
28-
* @returns {Promise<import('@sveltejs/site-kit').NavigationLink[]>}
29-
*/
30-
async function get_nav_context_list() {
31-
const docs_list = get_docs_list(get_docs_data());
32-
const processed_docs_list = docs_list.map(({ title, pages }) => ({
33-
title,
34-
sections: pages.map(({ title, path }) => ({ title, path }))
35-
}));
36-
37-
const blog_list = get_blog_list(get_blog_data());
38-
const processed_blog_list = [
39-
{
40-
title: 'Blog',
41-
sections: blog_list.map(({ title, slug, date }) => ({
42-
title,
43-
path: '/blog/' + slug,
44-
// Put a NEW badge on blog posts that are less than 14 days old
45-
badge: (+new Date() - +new Date(date)) / (1000 * 60 * 60 * 24) < 14 ? 'NEW' : undefined
46-
}))
47-
}
48-
];
49-
50-
const tutorial_list = get_tutorial_list(get_tutorial_data());
51-
const processed_tutorial_list = tutorial_list.map(({ title, tutorials }) => ({
52-
title,
53-
sections: tutorials.map(({ title, slug }) => ({ title, path: '/tutorial/' + slug }))
54-
}));
55-
56-
const examples_list = get_examples_list(get_examples_data());
57-
const processed_examples_list = examples_list
58-
.map(({ title, examples }) => ({
59-
title,
60-
sections: examples.map(({ title, slug }) => ({ title, path: '/examples/' + slug }))
61-
}))
62-
.filter(({ title }) => title !== 'Embeds');
63-
64-
return [
65-
{
66-
title: 'Tutorial',
67-
prefix: 'tutorial',
68-
pathname: '/tutorial',
69-
sections: processed_tutorial_list
70-
},
71-
{
72-
title: 'Docs',
73-
prefix: 'docs',
74-
pathname: '/docs/introduction',
75-
sections: processed_docs_list
76-
},
77-
{
78-
title: 'Examples',
79-
prefix: 'examples',
80-
pathname: '/examples',
81-
sections: processed_examples_list
82-
},
83-
{
84-
title: 'REPL',
85-
prefix: 'repl',
86-
pathname: '/repl'
87-
},
88-
{
89-
title: 'Blog',
90-
prefix: 'blog',
91-
pathname: '/blog',
92-
sections: processed_blog_list
93-
}
94-
];
95-
}
96-
97-
export const load = async ({ url }) => {
98-
return {
99-
nav_title: get_nav_title(url),
100-
nav_links: get_nav_context_list()
101-
};
102-
};
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { get_blog_data, get_blog_list } from '$lib/server/blog/index.js';
2+
import { get_docs_data, get_docs_list } from '$lib/server/docs/index.js';
3+
import { get_examples_data, get_examples_list } from '$lib/server/examples/index.js';
4+
import { get_tutorial_data, get_tutorial_list } from '$lib/server/tutorial/index.js';
5+
import { json } from '@sveltejs/kit';
6+
7+
export const prerender = true;
8+
9+
export const GET = async () => {
10+
return json(await get_nav_list());
11+
};
12+
13+
/**
14+
* @returns {Promise<import('@sveltejs/site-kit').NavigationLink[]>}
15+
*/
16+
async function get_nav_list() {
17+
const docs_list = get_docs_list(get_docs_data());
18+
const processed_docs_list = docs_list.map(({ title, pages }) => ({
19+
title,
20+
sections: pages.map(({ title, path }) => ({ title, path }))
21+
}));
22+
23+
const blog_list = get_blog_list(get_blog_data());
24+
const processed_blog_list = [
25+
{
26+
title: 'Blog',
27+
sections: blog_list.map(({ title, slug, date }) => ({
28+
title,
29+
path: '/blog/' + slug,
30+
// Put a NEW badge on blog posts that are less than 14 days old
31+
badge: (+new Date() - +new Date(date)) / (1000 * 60 * 60 * 24) < 14 ? 'NEW' : undefined
32+
}))
33+
}
34+
];
35+
36+
const tutorial_list = get_tutorial_list(get_tutorial_data());
37+
const processed_tutorial_list = tutorial_list.map(({ title, tutorials }) => ({
38+
title,
39+
sections: tutorials.map(({ title, slug }) => ({ title, path: '/tutorial/' + slug }))
40+
}));
41+
42+
const examples_list = get_examples_list(get_examples_data());
43+
const processed_examples_list = examples_list
44+
.map(({ title, examples }) => ({
45+
title,
46+
sections: examples.map(({ title, slug }) => ({ title, path: '/examples/' + slug }))
47+
}))
48+
.filter(({ title }) => title !== 'Embeds');
49+
50+
return [
51+
{
52+
title: 'Tutorial',
53+
prefix: 'tutorial',
54+
pathname: '/tutorial',
55+
sections: processed_tutorial_list
56+
},
57+
{
58+
title: 'Docs',
59+
prefix: 'docs',
60+
pathname: '/docs/introduction',
61+
sections: processed_docs_list
62+
},
63+
{
64+
title: 'Examples',
65+
prefix: 'examples',
66+
pathname: '/examples',
67+
sections: processed_examples_list
68+
},
69+
{
70+
title: 'REPL',
71+
prefix: 'repl',
72+
pathname: '/repl'
73+
},
74+
{
75+
title: 'Blog',
76+
prefix: 'blog',
77+
pathname: '/blog',
78+
sections: processed_blog_list
79+
}
80+
];
81+
}

0 commit comments

Comments
 (0)