Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preload JSON #27

Closed
suyizhang opened this issue Jan 31, 2024 · 2 comments
Closed

preload JSON #27

suyizhang opened this issue Jan 31, 2024 · 2 comments

Comments

@suyizhang
Copy link

Hello, do you have any examples of preloading JSON

@bsmth
Copy link
Member

bsmth commented Feb 6, 2024

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
Copy link
Member

bsmth commented Jul 9, 2024

Closing for the moment as I think the question is answered

@bsmth bsmth closed this as completed Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants