diff --git a/app/src/app.d.ts b/app/src/app.d.ts index 743f07b..d47a314 100644 --- a/app/src/app.d.ts +++ b/app/src/app.d.ts @@ -3,7 +3,9 @@ declare global { namespace App { // interface Error {} - // interface Locals {} + interface Locals { + sessionId: string; + } // interface PageData {} // interface PageState {} // interface Platform {} diff --git a/app/src/hooks.server.ts b/app/src/hooks.server.ts new file mode 100644 index 0000000..9497475 --- /dev/null +++ b/app/src/hooks.server.ts @@ -0,0 +1,30 @@ +import type { HandleServerError, Handle } from '@sveltejs/kit'; +import { sequence } from '@sveltejs/kit/hooks'; +import ejs from 'ejs'; +import { uuid } from '$lib/utils/uuid'; + +const baseHandle: Handle = async ({ event, resolve }) => { + return resolve(event, { + transformPageChunk: ({ html }) => + ejs.render(html, { + env: { COMMIT_SHA: process.env.COMMIT_SHA } + }) + }); +}; + +const handleSession: Handle = async ({ event, resolve }) => { + event.locals.sessionId = uuid(); + return resolve(event); +}; + +export const handle = sequence(baseHandle, handleSession); + +export const handleError: HandleServerError = ({ error, status, event, message }) => { + console.error('handleServerError', { error, event }); + const errorId = crypto.randomUUID(); + return { + status, + message: `Internal Server Errors.\n${message}`, + errorId + }; +}; diff --git a/app/src/routes/+layout.server.ts b/app/src/routes/+layout.server.ts index dfc9440..b5bfe23 100644 --- a/app/src/routes/+layout.server.ts +++ b/app/src/routes/+layout.server.ts @@ -1,10 +1,9 @@ -import { uuid } from '$lib/utils/uuid'; import type { LayoutServerLoad } from './$types'; export const ssr = true; export const prerender = false; -export const load: LayoutServerLoad = () => { +export const load: LayoutServerLoad = ({ locals }) => { return { - sessionId: uuid() + sessionId: locals.sessionId }; }; diff --git a/app/src/routes/[sessionId=sessionId]/+page.server.ts b/app/src/routes/[sessionId=sessionId]/+page.server.ts deleted file mode 100644 index 8f7085c..0000000 --- a/app/src/routes/[sessionId=sessionId]/+page.server.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { error } from '@sveltejs/kit'; -import type { PageServerLoad } from './$types'; - -export const load: PageServerLoad = ({ url }) => { - const category = url.searchParams.get('category'); - if (!category) error(400, 'Audio category was not found'); - - return { - category - }; -}; diff --git a/app/src/routes/[sessionId=sessionId]/+page.ts b/app/src/routes/[sessionId=sessionId]/+page.ts new file mode 100644 index 0000000..8e5a07f --- /dev/null +++ b/app/src/routes/[sessionId=sessionId]/+page.ts @@ -0,0 +1,14 @@ +import { error, redirect } from '@sveltejs/kit'; +import type { PageLoad } from './$types'; + +export const load: PageLoad = async ({ url, parent }) => { + const category = url.searchParams.get('category'); + if (!category) error(400, 'Audio category was not found'); + + const { sessionId } = await parent(); + if (url.pathname !== `/${sessionId}`) redirect(307, '/'); + + return { + category + }; +};