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

Change the name of the special fetch function #12435

Open
felipesantoz opened this issue Jul 4, 2024 · 0 comments
Open

Change the name of the special fetch function #12435

felipesantoz opened this issue Jul 4, 2024 · 0 comments

Comments

@felipesantoz
Copy link

felipesantoz commented 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 SvelteKit fetch is error prone and unfriendly to beginners. For example, say we meant to write the following code in routes/+page.server.ts:

export const load: PageServerLoad = ({ fetch }) => {
 fetch('/example');
};

But instead, we accidentally wrote the code as such:

export const load: PageServerLoad = () => {
 fetch('/example');
};

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:

export const load: PageServerLoad = () => {
 svetch('/example');
};

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

@felipesantoz 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant