Skip to content

Commit a17672d

Browse files
committed
Skip url.searchParams during prerendering to avoid SvelteKit build error
1 parent cc5a23f commit a17672d

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

docs/src/routes/api/charts/renderChartEndpoint.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { building } from '$app/environment';
12
import { createCanvas, Path2D } from '@napi-rs/canvas';
23
import { renderChart } from 'layerchart/server';
34
import type { CanvasFactory } from 'layerchart/server';
@@ -11,6 +12,11 @@ if (typeof globalThis.Path2D === 'undefined') {
1112
const createNodeCanvas: CanvasFactory = (canvasWidth, canvasHeight) =>
1213
createCanvas(canvasWidth, canvasHeight) as unknown as ReturnType<CanvasFactory>;
1314

15+
function getParam(url: URL, name: string): string | null {
16+
if (building) return null;
17+
return url.searchParams.get(name);
18+
}
19+
1420
type RenderChartResponseOptions = {
1521
component: Component<any>;
1622
props: Record<string, any>;
@@ -22,10 +28,10 @@ export function renderChartResponse({
2228
props,
2329
url
2430
}: RenderChartResponseOptions): Response {
25-
const width = Number(url.searchParams.get('width') ?? 800);
26-
const height = Number(url.searchParams.get('height') ?? 400);
27-
const format = url.searchParams.get('format') === 'jpeg' ? 'jpeg' : 'png';
28-
const background = url.searchParams.get('background') ?? 'white';
31+
const width = Number(getParam(url, 'width') ?? 800);
32+
const height = Number(getParam(url, 'height') ?? 400);
33+
const format = getParam(url, 'format') === 'jpeg' ? 'jpeg' : 'png';
34+
const background = getParam(url, 'background') ?? 'white';
2935

3036
const buffer = renderChart(component, {
3137
width,

0 commit comments

Comments
 (0)