Skip to content

Commit

Permalink
Merge pull request #26 from BohdanOne/25-home-page-do-not-load-gpx-data
Browse files Browse the repository at this point in the history
fix: prevent premature data fetching
  • Loading branch information
BohdanOne authored Apr 22, 2024
2 parents b7b8ef8 + 71d83ad commit b842b54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
18 changes: 18 additions & 0 deletions src/lib/utils/getGeoJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { DOMParser } from 'xmldom';
// @ts-expect-error - no types
import togeojson from '@mapbox/togeojson';

import type { GeoJson } from '$lib/types';

/**
* Parses a GPX string and converts it to GeoJSON format.
* @param gpx - The GPX string to parse.
* @returns A promise that resolves to a GeoJSON object.
*/
export default async function getGeoJson(gpx: string): Promise<GeoJson> {
const parser = new DOMParser();

const parsedGPX = parser.parseFromString(gpx, 'application/gpx+xml');

return togeojson.gpx(parsedGPX);
}
21 changes: 2 additions & 19 deletions src/routes/api/peaks/+server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { json } from '@sveltejs/kit';
import { DOMParser } from 'xmldom';
// @ts-expect-error - no types
import togeojson from '@mapbox/togeojson';

import type { Peak, GeoJson } from '$lib/types';
import type { Peak } from '$lib/types';
import getGeoJson from '$lib/utils/getGeoJson';

export const prerender = true;

Expand Down Expand Up @@ -37,7 +34,6 @@ async function getPeaks(): Promise<Peak[]> {
if (gpx) {
const geoJson = await getGeoJson(gpx);
peak.visitDate = new Date(geoJson?.features[0]?.properties?.time).toLocaleDateString();
peak.geoJson = geoJson;
}
peaks.push(peak);
}
Expand All @@ -46,16 +42,3 @@ async function getPeaks(): Promise<Peak[]> {

return peaks;
}

/**
* Parses a GPX string and converts it to GeoJSON format.
* @param gpx - The GPX string to parse.
* @returns A promise that resolves to a GeoJSON object.
*/
async function getGeoJson(gpx: string): Promise<GeoJson> {
const parser = new DOMParser();

const parsedGPX = parser.parseFromString(gpx, 'application/gpx+xml');

return togeojson.gpx(parsedGPX);
}

0 comments on commit b842b54

Please sign in to comment.