0

I want to prefetch (or maybe preload) a JSON file from an URL and use it later on my JS file. How can I send the prefetched JSON to the JS file?

Something like on the HTML head:

<link rel="prefetch" href="https://json.file" as="fetch">

or on the JS file:

fetch("the prefetch file above")
  .then((res) => res.json())
  .then((data) => {
  });

1 Answer 1

-1

you can use this

<link rel="prefetch" href="/data.json" as="fetch">

by this instead of going to the server again and ask for the file when you want to use it, the browser will load the resource from the cache.

fetch('/data.json')
    .then(response => {
    // some code
    })
    .then(json => {
        // some code
    })
    .catch(console.error);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer! Can I do that even if the prefetch is loading an external resource? Like: 'href="www.somewebsite/data.json"'
yes any link to an accessible file will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.