Cara Mengambil File JSON Menentukan Bagaimana Data Diproses
This article is also available in English _ Pendahuluan Sebagai Frontend Developer... rasanya kita semua pernah membuat project tanpa backend. Entah karena memang tidak memakai backend... atau mungkin backend-nya memang belum sempat dibuat. :) Biasanya data tidak lagi diambil dari API. Melainkan disimpan langsung di dalam project. Ada yang membuat data.ts , data.tsx , ada juga yang lebih suka…
This article, translated from Bahasa Indonesia to English, explores the differences between static and dynamic data in a project, specifically focusing on the use of JSON files. The primary points covered include:
1. Static and dynamic data: Static data remains constant throughout the application's runtime, while dynamic data may change during runtime without the need for a rebuild. The distinction lies in how the data is used and managed within the project.
2. Storing data in JSON files: There are two primary methods for storing data as JSON files:
a. Saving the JSON file in the `@/data/` folder or another location and importing it using `import projects from "@/data/projects.json";`. This method makes the data readily accessible as an object or array within the application.
b. Placing the JSON file in the `public` folder and fetching it when needed using `const response = await fetch("/projects.json"); const projects = await response.json();`. This approach is often used when the project will later incorporate a backend, as the data retrieval process remains largely unchanged.
3. Importing vs. fetching JSON files: When using `import`, the JSON file becomes part of the application's source code and is available for use as soon as the application is built. On the other hand, using `fetch()` initiates an HTTP request when the application is running, allowing the data to be fetched from various sources, such as JSON files, images, videos, or any resource accessible via HTTP.
4. The role of JSON files in the application: Ultimately, the choice between static and dynamic data processing—or whether to use `import` or `fetch()`—depends on how the data is utilized within the project. The JSON file itself and its content play a crucial role in determining how the application interacts with the data.
5. Sensitivity of data: The article also raises the importance of considering the sensitivity of the data stored in JSON files. If the data contains sensitive information, it is advisable to avoid storing it in the frontend. Any data sent to the browser may be publicly accessible, and the frontend should not attempt to manage all aspects of the data.
In conclusion, while JSON files may seem like a simple storage solution, their use can significantly impact how an application handles and processes data. The choice between static and dynamic data processing and the method of data retrieval—whether through `import` or `fetch()`—should be carefully considered based on the project's requirements and the nature of the data being managed.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.