Improve map loading functions
This commit is contained in:
@@ -1,27 +1,30 @@
|
|||||||
import type { PageLoad } from '/$types';
|
import type { PageLoad } from '/$types';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
|
|
||||||
export const load: PageLoad = async ({ params, fetch }) => {
|
const mapDataFiles = import.meta.glob('$lib/assets/route/*.json', { query: '?json' });
|
||||||
|
|
||||||
|
export const load: PageLoad = async ({ params }) => {
|
||||||
const { slug } = params;
|
const { slug } = params;
|
||||||
|
|
||||||
try {
|
const path = `/src/lib/assets/route/${slug}.json`;
|
||||||
const res = await fetch(`/mapFiles/json/${slug}.json`);
|
console.log(`Finding path: ${path}`);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!(path in mapDataFiles)) {
|
||||||
throw error(404, {
|
throw error(404, {
|
||||||
message: `Route ${slug} not found`
|
message: `Route ${slug} not found`
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const rawData = await res.json();
|
try {
|
||||||
|
const rawData = await mapDataFiles[path]();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
route: rawData,
|
route: rawData,
|
||||||
slug: slug
|
slug: slug,
|
||||||
};
|
};
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error loading map ${slug}: `, err);
|
console.error(`Error loading map ${slug}: `, err);
|
||||||
|
|
||||||
throw error(500, {
|
throw error(500, {
|
||||||
message: `Failed to parse map data for ${slug}`
|
message: `Failed to parse map data for ${slug}`
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user