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

HEAD reported as an invalid request handler #9468

Closed
poppa opened this issue Mar 21, 2023 · 8 comments · Fixed by #9753
Closed

HEAD reported as an invalid request handler #9468

poppa opened this issue Mar 21, 2023 · 8 comments · Fixed by #9753
Labels
feature / enhancement New feature or request

Comments

@poppa
Copy link

poppa commented Mar 21, 2023

Describe the bug

VS Code is reporting exporting a HEAD request handler with the following error:

Invalid export 'HEAD' (valid exports are prerender, trailingSlash, config, GET, POST, PUT, PATCH, DELETE, OPTIONS, or anything with a '_' prefix)ts(71001)

Screenshot 2023-03-21 at 10 18 11

The HEAD handler is properly called when a HEAD request is issued, and svelte-check doesn't complain about it either, so my guess is that this is an error related to the language tools.

I also noted that HEAD is not listed as a valid verb at the documentation (https://kit.svelte.dev/docs/routing#server)

Reproduction

export const HEAD = (() => new Response(null, { status: 200 })) satisfies RequestHandler

Expected behaviour

I don't expect an error here

System Info

  • Svelte v3.57.0
  • SvelteKit v1.13
  • Svelte Check v3.1.4
  • Svelte for VS Code v107.2.5
  • VSCode
    Version: 1.76.2
    Commit: ee2b180d582a7f601fa6ecfdad8d9fd269ab1884
    Date: 2023-03-14T17:54:09.061Z
    Electron: 19.1.11
    Chromium: 102.0.5005.196
    Node.js: 16.14.2
    V8: 10.2.154.26-electron.0
    OS: Darwin x64 22.1.0
    Sandboxed: Yes

Which package is the issue about?

Svelte for VS Code extension

Additional Information, eg. Screenshots

No response

@poppa poppa added the bug Something isn't working label Mar 21, 2023
@dummdidumm
Copy link
Member

The error is correct, HEAD is not a valid export. During build or when it's invoked during dev you'll also see that SvelteKit throws an error saying that HEAD is not a valid export.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Mar 21, 2023
@poppa
Copy link
Author

poppa commented Mar 21, 2023

No, it doesn't throw an error during build, nor when invoked during dev.

@dummdidumm
Copy link
Member

If I create a src/routes/foo/+server.js with a HEAD in it and then visit localhost:../foo I see the error message inside the terminal where I started the dev server.

@poppa
Copy link
Author

poppa commented Mar 21, 2023

Hm, I created a new route where I get the same behaviour. My old route still works though, which is super-concerning!

Well, I guess this can be labelled as a bug in SvelteKit then, so I'm perfectly fine with this issue being closed.

Screenshot 2023-03-21 at 11 33 12

@dummdidumm dummdidumm reopened this Mar 21, 2023
@dummdidumm
Copy link
Member

dummdidumm commented Mar 21, 2023

Transfering to the SvelteKit - it shouldn't happen that HEAD is somehow called IMO. Could you provide a minimum reproducible where HEAD is called?

@dummdidumm dummdidumm transferred this issue from sveltejs/language-tools Mar 21, 2023
@poppa
Copy link
Author

poppa commented Mar 21, 2023

Then the question is: Why isn't HEAD a valid request handler?

(this issue discusses the HEAD verb as if nothing strange about it?!: #9164)

@eltigerchino
Copy link
Member

eltigerchino commented Mar 21, 2023

it shouldn't happen that HEAD is somehow called IMO

The current logic seems to allow for any exported endpoint to be called as long as it matches the request method name.
All the checks are bypassed.

export async function render_endpoint(event, mod, state) {
const method = /** @type {import('types').HttpMethod} */ (event.request.method);
let handler = mod[method];
if (!handler && method === 'HEAD') {
handler = mod.GET;
}
if (!handler) {
return method_not_allowed(mod, method);
}

I think the exported endpoint is only validated when a +page co-exists with the +server.js file.

} else if (route.endpoint && (!route.page || is_endpoint_request(event))) {
response = await render_endpoint(event, await route.endpoint(), state);

and even then it seems as if it would return true in most cases after negotiating the content type
export function is_endpoint_request(event) {
const { method, headers } = event.request;
if (method === 'PUT' || method === 'PATCH' || method === 'DELETE' || method === 'OPTIONS') {
// These methods exist exclusively for endpoints
return true;
}
// use:enhance uses a custom header to disambiguate
if (method === 'POST' && headers.get('x-sveltekit-action') === 'true') return false;
// GET/POST requests may be for endpoints or pages. We prefer endpoints if this isn't a text/html request
const accept = event.request.headers.get('accept') ?? '*/*';
return negotiate(accept, ['*', 'text/html']) !== 'text/html';
}

@dummdidumm dummdidumm added feature / enhancement New feature or request and removed bug Something isn't working awaiting submitter labels Mar 23, 2023
@dummdidumm
Copy link
Member

Giving this the enhancement label - I think we should add official support for having a HEAD export in +server.js

dummdidumm pushed a commit that referenced this issue Jul 4, 2023
closes #9468

Allows exporting the HEAD handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature / enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants