Skip to content

Commit

Permalink
set session_id in hooks.server
Browse files Browse the repository at this point in the history
  • Loading branch information
nwaughachukwuma committed Nov 7, 2024
1 parent 6280a00 commit 22966f1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
4 changes: 3 additions & 1 deletion app/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
declare global {
namespace App {
// interface Error {}
// interface Locals {}
interface Locals {
sessionId: string;
}
// interface PageData {}
// interface PageState {}
// interface Platform {}
Expand Down
30 changes: 30 additions & 0 deletions app/src/hooks.server.ts
Original file line number Diff line number Diff line change
@@ -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
};
};
5 changes: 2 additions & 3 deletions app/src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -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
};
};
11 changes: 0 additions & 11 deletions app/src/routes/[sessionId=sessionId]/+page.server.ts

This file was deleted.

14 changes: 14 additions & 0 deletions app/src/routes/[sessionId=sessionId]/+page.ts
Original file line number Diff line number Diff line change
@@ -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
};
};

0 comments on commit 22966f1

Please sign in to comment.