question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch does not work with local urls on Now

See original GitHub issue

The following is the code…

In .src/routes/index.svelte

<script context="module"> 
    export async function preload({ params, query }) { 
        const res = await this.fetch(`/data`);
        const data = await res.json();
        return data;//{"title":data.title};
    }
</script>   
<script>  
export let title;
</script>  
<h1>Great success! - {title}</h1>  

created .src/routes/data.js file and have the following

const superagent = require('superagent'); 
// GET /data
export function get(req, res, next) { 
  superagent.get("https://external/api/json/get/path/xyz")
    .type("json")
    .accept("json")
    .query(req.query)
    .pipe(res);
}
  1. {title} in the above code only works when we click on other tabs and back but not when we refresh the index.svelte page
  2. The same code works fine locally ( localhost ) but not when we deploy to now ( production )

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
jnvcommented, Nov 1, 2019

Thanks for the tips! You can also use host property in the preload function so you don’t need to hardcode domain URL. This is the workaround I have arrived to:

  export function preload({host, params, query}) {
    let baseUrl = ''
    if (!process.browser && process.env.NOW_REGION) {
      baseUrl = `https://${host}`
    }
    
    return this.fetch(`${baseUrl}/data`)
    // ...
}
0reactions
thghcommented, Feb 9, 2020

Fix has been released in v0.39.0! Thanks @fpaboim

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fetch request to local file not working - Stack Overflow
Try to place your json file in the public folder like so : public/movies.json. and then fetch using fetch('./movies.json').
Read more >
fetch() a local file · Issue #2150 · denoland/deno - GitHub
I'd like to condense the most important reasons I believe URLs of file scheme should not be implemented in node-fetch. I would of...
Read more >
Fetch Standard
A URL is local if its scheme is a local scheme. ... mode is assumed to be " include " and fetch does...
Read more >
Fetch - The Modern JavaScript Tutorial
JavaScript can send network requests to the server and load new information whenever it's needed. For example, we can use a network request ......
Read more >
Fetch API - MDN Web Docs
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found