patwasalinguistP
Deno3y ago
10 replies
patwasalinguist

Is inspecting import.meta.url a reliable way to distinguish browser and deno contexts?

I have been trying to figure out a good pattern to import JSON that will work whether called in deno or the browser. This is what I was thinking:

let data

if(import.meta.url.startsWith('file')){ // we’re in deno
  let jsonstring = await Deno.readTextFile('delete.json')
  data = JSON.parse(jsonstring)  
} else if(import.meta.url.startsWith('http')){ // we’re in the browser
  let response = await fetch('delete.json')
  data = await response.json()
}

export {data}


I would welcome advice as to whether this is a good approach to this problem.

thanks 🙏
Was this page helpful?