-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Cannot use relative URL with global fetch in .svelte file #8536
Comments
This is not reproducible given the code snippet. Looking at your error log, it sounds like you eagerly use That said, the error is confusing since it's not called inside a Could you please provide the code of your |
Could one of you please provide a minimum reproducible with code that worked before this change and now no longer doesn't? I'm having a hard time seing how this worked before; I thought that there would be a "invalid URL" failure then. |
Not sure what change you are refering to, but if relative URLs is not supposed to work, does that mean the documentation refered to in the original post should be changed? There seems to be a mismatch. |
I can not, i justed with svelte yesterday and assumed, from reading the docs, that this should work, since this is basically the example of: https://kit.svelte.dev/docs/routing#server-receiving-data Nonetheless, here is my <script lang="ts">
let searchTerm = ""
let results = []
async function getSearchResult() {
const response = await fetch(`/search?query=${searchTerm}`, {
method: 'GET',
headers: {
'content-type': 'application/json',
'accept': 'application/json'
}
});
results = await response.json();
}
let timer
async function debounce(func) {
clearTimeout(timer);
timer = setTimeout(async () => {
await func();
}, 750);
}
$: {
debounce(getSearchResult)
}
</script>
<div class="md:container md:mx-auto flex py-3 gap-2">
<img src="logo.png" alt="logo" class="h-10" />
<input class="flex-1 bg-slate-500 h-10 rounded-lg drop-shadow-md p-2 text-slate-800" type="text" bind:value={searchTerm} placeholder="Search for a software"/>
</div>
<div class="md:container md:mx-auto flex py-3 gap-2">
<div class="text-medium text-2xl text-slate-200">
{#if searchTerm !== ""}
Results for '{searchTerm}'
{/if}
{#each results as d}
<p>{d}</p>
{/each}
</div>
</div>
<div><slot/></div>
</div> and my /** @type {import('./$types').RequestHandler} */
export async function GET({ request }) {
return json({})
} |
Can confirm i get the same error inside a load-function as well |
@adriandersen please provide a code snippet of the load function |
@kegesch judging from the code snippet this could never have worked, you would have gotten a different error (which is generic and therefore we added a more descriptive error in #8370), but you would have gotten an error. @amr3k your case is interesting. Using Both of your use cases sound like you maybe are not interested in server side rendering at all? In this case you can turn your SvelteKit app into an SPA by adding |
Okay, but just for the clarification. If I understood it correctly, the example in https://kit.svelte.dev/docs/routing#server-receiving-data is wrong. Rather, fetch with relative paths should be done in the |
As I load the data initially with PageServerLoad (https://kit.svelte.dev/docs/load#page-data), I for my part do not need to do a fetch in a svelte component while it is rendered server side. But I do need to do a fetch later on, which will only be on client side. My solution was to wrap it with the browser environment variable from sveltekit. import { browser } from "$app/environment";
if (browser) {
const res = await fetch(
// url
);
data = await res.json();
} With this I did not get the error "Cannot use relative URL (...) with global fetch". Just writing it as this may help some others reading this. |
I loaded a small set of items via page.js and then tried to load more on intersecting when i run in to this error.
This was the solution for me as well, thank you Zerdayne |
@dummdidumm Thanks, that explains a lot, but I don't want to disable ssr for seo reasons, and I don't want to keep using environment variables because the development branch build will break. |
The thing is running
|
* fix: adjust fetch error message on the server closes #8536 * make it possible to still use them in #await blocks * undo #await dance * put additional dev time check inside render.js instead * Create five-pumpkins-join.md * prevent corrupted state * Delete nice-mayflies-compete.md --------- Co-authored-by: Rich Harris <richard.a.harris@gmail.com> Co-authored-by: Rich Harris <hello@rich-harris.dev>
how to fetch in the load function is described here: |
I had this same problem and just didn't understand. It seems like the conversation was over my head. The error message says 'use export async function load({ params, fetch }) { // <--- event.params and event.fetch
fetch('/realative/url') // parameter hides normal fetch() function
} |
is this fixed? or is it on different issue since this issue is closed |
Yes you forgot to import the fetch in the load function |
Describe the bug
According to the documentation, I should be able to create an endpoint in a
+server.js
file and call that endpoint with a relative url viafetch
from+page.svelte
(https://kit.svelte.dev/docs/routing#server-receiving-data).This is not possible anymore. I get an error message that was introduced with #8370 :
Hower, the referenced documentation does not even mention
event.fetch
and also does not explain why this is not possible in.svelte
files.Reproduction
The example of https://kit.svelte.dev/docs/routing#server-receiving-data .
Logs
Severity
blocking an upgrade
Additional Information
No response
The text was updated successfully, but these errors were encountered: