Prerender, use vercel adapter, disable PUT action for now

pull/8432/head
Puru Vijay 3 years ago
parent fd41afadfb
commit 593b7945de

@ -9,3 +9,4 @@
/src/routes/_components/Supporters/contributors.js
/src/routes/_components/Supporters/donors.jpg
/src/routes/_components/Supporters/donors.js
.vercel

File diff suppressed because it is too large Load Diff

@ -17,6 +17,7 @@
},
"dependencies": {
"@supabase/supabase-js": "^2.11.0",
"@sveltejs/adapter-vercel": "^2.4.1",
"@sveltejs/repl": "^0.2.0",
"cookie": "^0.5.0",
"devalue": "^4.3.0",
@ -28,7 +29,6 @@
},
"devDependencies": {
"@resvg/resvg-js": "^2.4.1",
"@sveltejs/adapter-auto": "^2.0.0",
"@sveltejs/kit": "^1.12.0",
"@sveltejs/site-kit": "^3.3.6",
"@sveltejs/vite-plugin-svelte": "^2.0.3",

@ -1,12 +1,12 @@
// @ts-check
import fs from 'node:fs';
const base = '../../site/content/examples/';
const BASE = '../../site/content/examples/';
/**
* @returns {import('./types').ExamplesData}
*/
export function get_examples_data() {
export function get_examples_data(base = BASE) {
const examples = [];
for (const subdir of fs.readdirSync(base)) {

@ -1,4 +1,4 @@
import { dev } from '$app/environment';
import { building, dev } from '$app/environment';
import { client } from '$lib/db/client';
import * as gist from '$lib/db/gist';
import * as session from '$lib/db/session';
@ -6,16 +6,14 @@ import { get_example } from '$lib/server/examples';
import { get_examples_data, get_examples_list } from '$lib/server/examples/get-examples';
import { error, json } from '@sveltejs/kit';
export const prerender = 'auto';
const UUID_REGEX = /^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$/;
const examples_data = get_examples_data();
const examples = new Set(
get_examples_list(examples_data)
.map((category) => category.examples)
.flat()
.map((example) => example.slug)
);
/** @type {Set<string>} */
let examples;
/** @param {import('$lib/server/examples/types').ExamplesData[number]['examples'][number]['files'][number][]} files */
function munge(files) {
@ -40,16 +38,28 @@ function munge(files) {
}
export async function GET({ params }) {
if (examples.has(params.id)) {
const example = get_example(examples_data, params.id);
return json({
id: params.id,
name: example.title,
owner: null,
relaxed: false, // TODO is this right? EDIT: It was example.relaxed before, which no example return to my knowledge. By @PuruVJ
components: munge(example.files),
});
// Currently, these pages(that are in examples/) are prerendered. To avoid making any FS requests,
// We prerender examples pages during build time. That means, when something like `/repl/hello-world.json`
// is accessed, this function won't be run at all, as it will be served from the filesystem
if (building || dev) {
examples = new Set(
get_examples_list(examples_data)
.map((category) => category.examples)
.flat()
.map((example) => example.slug)
);
if (examples.has(params.id)) {
const example = get_example(examples_data, params.id);
return json({
id: params.id,
name: example.title,
owner: null,
relaxed: false, // TODO is this right? EDIT: It was example.relaxed before, which no example return to my knowledge. By @PuruVJ
components: munge(example.files),
});
}
}
if (dev && !client) {
@ -78,13 +88,13 @@ export async function GET({ params }) {
});
}
// TODO reimplement as an action
export async function PUT({ params, request }) {
const user = await session.from_cookie(request.headers.get('cookie'));
if (!user) throw error(401, 'Unauthorized');
// // TODO reimplement as an action
// export async function PUT({ params, request }) {
// const user = await session.from_cookie(request.headers.get('cookie'));
// if (!user) throw error(401, 'Unauthorized');
const body = await request.json();
await gist.update(user, params.id, body);
// const body = await request.json();
// await gist.update(user, params.id, body);
return new Response(undefined, { status: 204 });
}
// return new Response(undefined, { status: 204 });
// }

@ -1,16 +0,0 @@
import { error } from '@sveltejs/kit';
export async function load({ fetch, params, url }) {
const res = await fetch(`/repl/${params.id}.json`);
if (!res.ok) {
throw error(res.status);
}
const gist = await res.json();
return {
gist,
version: url.searchParams.get('version') || '3'
};
}

@ -0,0 +1,30 @@
import * as gist from '$lib/db/gist';
import * as session from '$lib/db/session';
import { error } from '@sveltejs/kit';
export async function load({ fetch, params, url }) {
const res = await fetch(`/repl/${params.id}.json`);
if (!res.ok) {
throw error(res.status);
}
const gist = await res.json();
return {
gist,
version: url.searchParams.get('version') || '3',
};
}
export const actions = {
default: async ({ params, request }) => {
const user = await session.from_cookie(request.headers.get('cookie'));
if (!user) throw error(401, 'Unauthorized');
const body = await request.json();
await gist.update(user, params.id, body);
return new Response(undefined, { status: 204 });
},
};

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

Loading…
Cancel
Save