You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since fetch from the Fetch API is a global variable, using the exact same name for the special context-specific SvelteKit fetch is error prone and unfriendly to beginners. For example, say we meant to write the following code in routes/+page.server.ts:
The compiler will not complain, and we will be none the wiser about anything being wrong until encountering a run-time error. Then, when trying to debug the source of said error, it will likely take some time and/or previous experience with this type of error to find where the problem is. To make matters worse, there is a chance the error will be intermittent, making it even harder to catch.
The docs are also unclear about this. Take for example the documentation for handleFetch. It states:
This function allows you to modify (or replace) a fetch request that happens inside a load or action function that runs on the server (or during pre-rendering).
There is no mention that this fetch request cannot come from the global fetch.
Describe the proposed solution
There are a few ways to easily remedy the issue. My preferred is to simply rename the SvelteKit fetch to something else, for example svetch. Then the following will lead to a compiler error:
And the docs will refer to svetch rather than fetch, removing all ambiguity from code and documentation alike.
Alternatives considered
Some other form of compile-time error or warning could be used. For example, there could be an eslint rule to error/warn whenever the global fetch is referenced within a load or action function. With this solution, I'm not sure if it's possible to manage the cases where such a reference is made within a helper function, so perhaps a clear runtime error/warning in these cases might be more realistic. Perhaps instead eslint can complain whenever global fetch appears in any server file at all.
Alternatively, global fetch could be disabled by default within SvelteKit projects, requiring an explicit import to use. While this should be enough for more experienced users of SvelteKit to avoid the mistake, it does not necessarily discourage beginners from importing global fetch when learning from the docs. I'm also not sure if this is even possible to do on a browser.
Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered:
felipesantoz
changed the title
Change the name or handling of the special fetch function
Change the name of the special fetch function
Jul 4, 2024
Describe the problem
Since
fetch
from the Fetch API is a global variable, using the exact same name for the special context-specific SvelteKitfetch
is error prone and unfriendly to beginners. For example, say we meant to write the following code inroutes/+page.server.ts
:But instead, we accidentally wrote the code as such:
The compiler will not complain, and we will be none the wiser about anything being wrong until encountering a run-time error. Then, when trying to debug the source of said error, it will likely take some time and/or previous experience with this type of error to find where the problem is. To make matters worse, there is a chance the error will be intermittent, making it even harder to catch.
The docs are also unclear about this. Take for example the documentation for handleFetch. It states:
There is no mention that this
fetch
request cannot come from the globalfetch
.Describe the proposed solution
There are a few ways to easily remedy the issue. My preferred is to simply rename the SvelteKit
fetch
to something else, for examplesvetch
. Then the following will lead to a compiler error:And the docs will refer to
svetch
rather thanfetch
, removing all ambiguity from code and documentation alike.Alternatives considered
Some other form of compile-time error or warning could be used. For example, there could be an
eslint
rule to error/warn whenever the globalfetch
is referenced within aload
oraction
function. With this solution, I'm not sure if it's possible to manage the cases where such a reference is made within a helper function, so perhaps a clear runtime error/warning in these cases might be more realistic. Perhaps instead eslint can complain whenever globalfetch
appears in anyserver
file at all.Alternatively, global
fetch
could be disabled by default within SvelteKit projects, requiring an explicit import to use. While this should be enough for more experienced users of SvelteKit to avoid the mistake, it does not necessarily discourage beginners from importing globalfetch
when learning from the docs. I'm also not sure if this is even possible to do on a browser.Importance
would make my life easier
Additional Information
No response
The text was updated successfully, but these errors were encountered: