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

feat: add server endpoint fallback handler #9755

Merged
merged 20 commits into from Sep 7, 2023
Merged

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Apr 23, 2023

closes #9164

Adds a catch-all handler that handles all unimplemented valid server requests. This is done by exporting the fallback function from a +server.js file.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Apr 23, 2023

🦋 Changeset detected

Latest commit: 0ac4a79

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Conduitry
Copy link
Member

Have we thought about the effects this might have on https://kit.svelte.dev/docs/routing#server-content-negotiation ? I haven't thought about that all the way through yet. Also, if we decided (as in #9780) to start adding Vary: Accept to responses for requests with sibling GET/POST endpoints and pages, would this make that more difficult?

@eltigerchino
Copy link
Member Author

eltigerchino commented Apr 27, 2023

Have we thought about the effects this might have on kit.svelte.dev/docs/routing#server-content-negotiation ?

Content negotiation is still in play here, so the accept: text/html always connects the page.

Also, if we decided (as in #9780) to start adding Vary: Accept to responses for requests with sibling GET/POST endpoints and pages, would this make that more difficult?

I'm not sure myself. Will need to start reading the MDN docs on what Vary: Accept is.

@eltigerchino
Copy link
Member Author

eltigerchino commented Apr 29, 2023

Also, if we decided (as in #9780) to start adding Vary: Accept to responses for requests with sibling GET/POST endpoints and pages, would this make that more difficult?

I'm not sure myself. Will need to start reading the MDN docs on what Vary: Accept is.

After reading MDN, it seems we would only need to return Vary: Accept, right? If so, it shouldn't impact this PR.

The server uses the Vary header to indicate which headers it actually used for content negotiation (or more precisely, the associated request headers), so that caches can work optimally.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation#server-driven_content_negotiation

@benmccann
Copy link
Member

@s3812497 it looks like this PR will need a rebase

@dominikg
Copy link
Member

dominikg commented Jul 7, 2023

all is still misleading or ambiguous. If you have POST and all, which one is handling an incoming post request? Isn't clear from the word all, which implies all requests are handled by it.

@ghostdevv
Copy link
Member

all is still misleading or ambiguous. If you have POST and all, which one is handling an incoming post request? Isn't clear from the word all, which implies all requests are handled by it.

I think it's probably fine, esp since the way people would learn about it is via docs which can explain it - however there could still be better words out there for it?

@karimfromjordan
Copy link
Contributor

Other name suggestions were:

  • fallback
  • other
  • using the default export

@eltigerchino
Copy link
Member Author

Name suggestion: any - matches any request method not already exported.

@gtm-nayan
Copy link
Contributor

I like fallback more for your description.

@matthewrobb
Copy link

Why not just support this?

/** @type {import('@sveltejs/kit').[Handle](https://kit.svelte.dev/docs/types#public-types-handle)} */
export async function handle({ event, resolve }) {
    if (event.url.pathname.startsWith('/custom')) {
        return new Response('custom response');
    }

    const response = await resolve(event);
    return response;
}

@eltigerchino eltigerchino changed the title feat: add catch all handler feat: add server endpoint fallback handler Jul 21, 2023
@eltigerchino
Copy link
Member Author

Why not just support this?

/** @type {import('@sveltejs/kit').[Handle](https://kit.svelte.dev/docs/types#public-types-handle)} */
export async function handle({ event, resolve }) {
    if (event.url.pathname.startsWith('/custom')) {
        return new Response('custom response');
    }

    const response = await resolve(event);
    return response;
}

Handling this case in the handle hook is already supported but would be made easier by this PR (directly exporting the fallback in the server endpoint and not cluttering the handle hook).

@eltigerchino
Copy link
Member Author

The export all handler has been renamed to fallback.

@matthewrobb
Copy link

Why not just support this?

/** @type {import('@sveltejs/kit').[Handle](https://kit.svelte.dev/docs/types#public-types-handle)} */
export async function handle({ event, resolve }) {
    if (event.url.pathname.startsWith('/custom')) {
        return new Response('custom response');
    }

    const response = await resolve(event);
    return response;
}

Handling this case in the handle hook is already supported but would be made easier by this PR (directly exporting the fallback in the server endpoint and not cluttering the handle hook).

Right, no my suggestion was to allow +server.js to export a handler hook that is only hit within that route scope. It can be used like an all OR a fallback OR an after etc. without any ambiguity compared to the other exports in that module.

Another benefit would be scenarios where you have a common resolution you want to do once up front for a whole routing scope, almost like what you get with layouts in view routes.

@eltigerchino
Copy link
Member Author

eltigerchino commented Jul 23, 2023

Right, no my suggestion was to allow +server.js to export a handler hook that is only hit within that route scope. It can be used like an all OR a fallback OR an after etc. without any ambiguity compared to the other exports in that module.

Sorry, I misunderstood. I'd definitely be in favour of subdirectory handle hooks replacing this as also mentioned by Ben

@matthewrobb
Copy link

Right, no my suggestion was to allow +server.js to export a handler hook that is only hit within that route scope. It can be used like an all OR a fallback OR an after etc. without any ambiguity compared to the other exports in that module.

Sorry, I misunderstood. I'd definitely be in favour of subdirectory handle hooks replacing this as also mentioned by Ben

Ah I missed that earlier comment. I also don't see any replies to it. Is there some other location that subdirectory hooks has/is being discussed?

@matthewrobb
Copy link

Honestly after thinking about it a bit I think there's certainly room for both a fallback and subdirectory hooks. 👍 this PR.

@eltigerchino
Copy link
Member Author

Ah I missed that earlier comment. I also don't see any replies to it. Is there some other location that subdirectory hooks has/is being discussed?

There doesn't seem to be from what I've seen (probably just talked about internally?). You may open an issue for it.

@karimfromjordan
Copy link
Contributor

Astro 3.0 is moving to capitalized function names too. I forwarded some feedback from here and they may rename the ALL function to fallback in which case both Astro and SvelteKit would share the same API (if this PR gets merged).

@karimfromjordan
Copy link
Contributor

Can this be... merged?

@eltigerchino
Copy link
Member Author

I'm still not sure if we should go ahead with this if we're planning to implement per directory handle hooks. This is something @dominikg might be looking into soon.

@dominikg
Copy link
Member

dominikg commented Sep 7, 2023

fallback is different from hooks as it would only be called if no other handler is defined. hooks would always be called. so i think both can make sense even if you could be able to implement fallback with a hook, it may be complicated to figure out which handlers are defined.

@eltigerchino eltigerchino merged commit cac42de into master Sep 7, 2023
13 of 14 checks passed
@eltigerchino eltigerchino deleted the feat-catch-all-handler branch September 7, 2023 17:14
@github-actions github-actions bot mentioned this pull request Sep 7, 2023
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

Successfully merging this pull request may close these issues.

catch-all method for +server.js
9 participants