Skip to content

Commit

Permalink
📦 refactor: search params
Browse files Browse the repository at this point in the history
  • Loading branch information
yunsii committed Apr 27, 2024
1 parent d42f652 commit bed9558
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/helpers/monitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
10 changes: 7 additions & 3 deletions src/pages/index/+data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit bed9558

Please sign in to comment.