From bed95582dcb9583a08e43fe164207ea606dcde10 Mon Sep 17 00:00:00 2001 From: Yuns Date: Sat, 27 Apr 2024 18:45:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=A6=20refactor:=20search=20params?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helpers/monitors.ts | 3 +-- src/pages/index/+data.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/helpers/monitors.ts b/src/helpers/monitors.ts index a7f2862..46d4546 100644 --- a/src/helpers/monitors.ts +++ b/src/helpers/monitors.ts @@ -3,10 +3,9 @@ import Papa from 'papaparse' import type { Monitor } from '#src/types' import { config } from '#src/config' -import { isWorkerEnv } from '#src/worker/_helpers' export default async function getRemoteMonitors() { - if (!config.monitorsCsvUrl || !isWorkerEnv) { + if (!config.monitorsCsvUrl) { return [] } const response = await fetch(config.monitorsCsvUrl, { diff --git a/src/pages/index/+data.ts b/src/pages/index/+data.ts index 2167048..d9106b0 100644 --- a/src/pages/index/+data.ts +++ b/src/pages/index/+data.ts @@ -5,10 +5,14 @@ import { getAllMonitors, getCoreData } from '#src/worker/_helpers/store' import { isWorkerEnv } from '#src/worker/_helpers' import { getDevKvData } from '#src/helpers/dev-data' +// Use search param `no-remote-monitors` to prevent load remote monitors +// Use search param `no-data` preview no data page export async function data(pageContext: PageContext) { const { urlParsed } = pageContext - // Worker env always use remote monitors - const useRemoteMonitors = isWorkerEnv || !('no-remote-monitors' in urlParsed.search) + const searchParams = new URLSearchParams(urlParsed.search) + const searchParamKeys = Array.from(searchParams.keys()) + // Only use remote monitors in worker env + const useRemoteMonitors = isWorkerEnv try { const { allMonitors, kvData } = await getCoreData(useRemoteMonitors) @@ -43,7 +47,7 @@ export async function data(pageContext: PageContext) { console.debug('Error ignored in non worker env.') const allMonitors = await getAllMonitors(useRemoteMonitors) - if ('no-data' in urlParsed.search) { + if (searchParamKeys.includes('no-data')) { return { allMonitors, kvData: null } } return {