html-examples icon indicating copy to clipboard operation
html-examples copied to clipboard

preload JSON

Open suyizhang opened this issue 1 year ago • 1 comments

Hello, do you have any examples of preloading JSON

suyizhang avatar Jan 31 '24 14:01 suyizhang

Hi there @suyizhang thanks for asking.

You should be able to do something like this:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>Load JSON Example</title>
    <link href="style.css" rel="stylesheet" />
    <link rel="preload" href="data.json" as="fetch" />
  </head>
  <body>
    <h1>Loading JSON Example</h1>

    <script>
      async function loadData() {
        try {
          const res = await fetch("data.json");
          const data = await res.json();
          console.log(data);
        } catch (err) {
          console.error(err);
        }
      }

      loadData();
    </script>
  </body>
</html>

Which assumes that you have these resources:

├── data.json
├── index.html
└── style.css

Does that answer the question?

bsmth avatar Feb 06 '24 09:02 bsmth

Closing for the moment as I think the question is answered

bsmth avatar Jul 09 '24 09:07 bsmth