Prerender tutorial the smart way

pull/8432/head
Puru Vijay 3 years ago
parent 3e3d61462f
commit 05ab116546

@ -2,12 +2,12 @@
import fs from 'node:fs'; import fs from 'node:fs';
import { extract_frontmatter } from '../markdown/index.js'; import { extract_frontmatter } from '../markdown/index.js';
const base = '../../site/content/tutorial/'; const BASE = '../../site/content/tutorial/';
/** /**
* @returns {import('./types').TutorialData} * @returns {import('./types').TutorialData}
*/ */
export function get_tutorial_data() { export function get_tutorial_data(base = BASE) {
const tutorials = []; const tutorials = [];
for (const subdir of fs.readdirSync(base)) { for (const subdir of fs.readdirSync(base)) {

@ -1,5 +1,5 @@
import { get_parsed_tutorial } from '$lib/server/tutorial'; import { get_parsed_tutorial } from '$lib/server/tutorial';
import { get_tutorial_data, get_tutorial_list } from '$lib/server/tutorial/get-tutorial-data'; import { get_tutorial_data, get_tutorial_list } from '$lib/server/tutorial/get-tutorial';
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit';
export const prerender = true; export const prerender = true;

@ -153,16 +153,6 @@
{/if} {/if}
</div> </div>
<!-- HACK to prerender -->
<p style="display: none;">
{#each data.tutorials_list as { tutorials }}
{#each tutorials as { slug }}
<!-- svelte-ignore a11y-missing-content -->
<a href="/tutorial/{slug}" />
{/each}
{/each}
</p>
<style> <style>
.tutorial-outer { .tutorial-outer {
position: relative; position: relative;

@ -1,6 +1,7 @@
// @ts-check // @ts-check
import adapter from '@sveltejs/adapter-vercel'; import adapter from '@sveltejs/adapter-vercel';
import { get_examples_data, get_examples_list } from './src/lib/server/examples/get-examples.js'; import { get_examples_data, get_examples_list } from './src/lib/server/examples/get-examples.js';
import { get_tutorial_data, get_tutorial_list } from './src/lib/server/tutorial/get-tutorial.js';
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
export default { export default {
@ -9,7 +10,7 @@ export default {
prerender: { prerender: {
// TODO: REMOVE // TODO: REMOVE
handleMissingId: 'ignore', handleMissingId: 'ignore',
entries: ['*', ...replJsonEntries()], entries: ['*', ...replJsonEntries(), ...tutorialEntries()],
}, },
}, },
}; };
@ -21,3 +22,11 @@ function replJsonEntries() {
get_examples_data(new URL('../../site/content/examples', import.meta.url).pathname) get_examples_data(new URL('../../site/content/examples', import.meta.url).pathname)
).flatMap(({ examples }) => examples.map(({ slug }) => `/repl/${slug}.json`)); ).flatMap(({ examples }) => examples.map(({ slug }) => `/repl/${slug}.json`));
} }
/** @returns {('*' | `/${string}`)[]} */
function tutorialEntries() {
// @ts-ignore
return get_tutorial_list(
get_tutorial_data(new URL('../../site/content/tutorial', import.meta.url).pathname)
).flatMap(({ tutorials }) => tutorials.map(({ slug }) => `/tutorial/${slug}`));
}

Loading…
Cancel
Save