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

Not clear that fetch is available in hooks #2916

Closed
illright opened this issue Nov 25, 2021 · 5 comments
Closed

Not clear that fetch is available in hooks #2916

illright opened this issue Nov 25, 2021 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@illright
Copy link
Contributor

Describe the bug

The docs section on the hooks doesn't mention if fetch is available on the server side to use inside a hook (handle(), for example) and how to obtain a reference to it. A reference to some fetch() is present in the sample code for the externalFetch() hook, but seeing as it's not a complete file, it leads to reader to believe this fetch might have to come from elsewhere, such as node-fetch installed additionally.

I see several solutions to this issue:

  1. Add a note to the documentation on hooks saying that fetch is globally available to use in any custom hook.

  2. Rework the way the references to fetch are obtained. For example, create an internal module that you could import fetch from, so that hooks.ts would look like this:

    import { fetch } from '@sveltejs/kit/hooks';
    
    export function handle() {
      console.log(await fetch('https://kit.svelte.dev/'));
    }

I would prefer the second solution as it allows explicit reference to fetch as well as explicit typings for TypeScript. As a user of SvelteKit writing hooks, I would be more confident knowing that I can import fetch from somewhere to use in hooks instead of hoping there's a global one.

Reproduction

There is no reproduction

Logs

No response

System Info

Irrelevant

Severity

annoyance

Additional Information

No response

@illright
Copy link
Contributor Author

My story behind this bug is the following:

I was writing a handle hook that, in some cases, would make a request to the backend in order to authenticate a user. For that, I wanted to use fetch, but found no note of it in the documentation. VS Code was showing that there was a global fetch coming from typescript/lib/lib.dom.d.ts, so I assumed it was a mistake of TypeScript to believe that hooks.ts had access to DOM APIs. I tried installing node-fetch, but for some reason, the dev server wouldn't start up when 'node-fetch' was imported due to peculiar node:* imports in its source code.

Frustrated, I cloned a fresh demo app, attempted to use a global fetch and it worked.

@iwnow
Copy link

iwnow commented Nov 28, 2021

the same problem wanna use fetch in the handle hook for a token refresh, any workaround?

@illright
Copy link
Contributor Author

@iwnow it's already available as a global variable

@bluwy
Copy link
Member

bluwy commented Nov 30, 2021

Probably the main reason that it's global is so that it's closer to browser behaviour, and arbitrary code from other files can use fetch directly too. This is also the behaviour for endpoints (which is documented). So for now, I think we can document this for hooks first.

@Conduitry Conduitry added the documentation Improvements or additions to documentation label Nov 30, 2021
@Rich-Harris
Copy link
Member

Now documented here: https://kit.svelte.dev/docs/web-standards#fetch-apis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

5 participants